配置confluence使用httpHeader的方式进行登录(目标版本:atlassian-confluence-6.3.3)
前提是已经安装好了Confluence,并且前端使用apache或者nginx拦截对应的地址进行认证,认证之后访问对应的应用的时候,返回一个HTTP HEADER 给应用,标识是哪个用户
关于配置apache的mod_auth_cas以及添加相应的HTTP Header,请参考这篇文章
步骤:
1.实现自己的Authenticator
新建java项目,导入confluence相关依赖,新建java文件如下:
package cn.cmri.atlassian.confluence;
/*
* Created with Intellij IDEA
* USER: JiaoYiping
* Mail: jiaoyiping@gmail.com
* Date: 2017/8/24
* Time: 16:48
* To change this template use File | Settings | Editor | File and Code Templates
*/
import com.atlassian.confluence.user.ConfluenceAuthenticator;
import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
public class ConfluenceRemoteUserAuthenticator extends ConfluenceAuthenticator {
private static final String REMOTE_USER_HEADER = "remote_user";
@Override
public Principal getUser(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) {
String remoteUserHeader = request.getHeader(REMOTE_USER_HEADER);
Principal remoteUser = this.getUser(remoteUserHeader);
if (this.getUserFromSession(request) != null) {
Principal currentUser = getUserFromSession(request);
if (remoteUserHeader != null && !currentUser.getName().equals(remoteUserHeader)) {
setSessionAttribute(remoteUser, request);
return remoteUser;
}
return getUserFromSession(request);
}
if (remoteUserHeader != null && remoteUser != null) {
setSessionAttribute(remoteUser, request);
return remoteUser;
}
return super.getUser(request, response);
}
private void setSessionAttribute(Principal user, HttpServletRequest request) {
if (user != null && request != null) {
request.getSession().setAttribute("seraph_defaultauthenticator_user", user);
request.getSession().setAttribute("seraph_defaultauthenticator_logged_out_user", (Object) null);
}
}
}
编译该文件,并把相应的class文件放到confluence的WEB-INF/classes下的相应位置
2.在confluence的WEB-INF/classes下,修改seraph-config.xml,把之前配置的Authenticator替换为自己实现的Authenticator
<!--<authenticator class="com.atlassian.confluence.user.ConfluenceAuthenticator"/>-->
<authenticator class="cn.cmri.atlassian.confluence.ConfluenceRemoteUserAuthenticator"/>
重启confluence,经过反向代理认证之后,跳转到该应用的请求,如果带上remote_user这个header,就会自动登录