JiraRemoteUserAuth

配置Jira7.x版本使用REMOTE_USER的HTTP Header方式登录:

前提是已经安装好了JIRA,并且前端使用apache或者nginx拦截对应的地址进行认证,认证之后访问对应的应用的时候,返回一个HTTP HEADER 给应用,标识是哪个用户

关于配置apache的mod_auth_cas以及添加相应的HTTP Header,请参考这篇文章

步骤:

1.实现自己的Authenticator

在jiar的 classes目录下的seraph-config.xml文件中配置的默认的Authenticator是com.atlassian.jira.security.login.JiraSeraphAuthenticator,我们需要仿照它的实现,重新实现一个Authenticator,首先新建一个java工程,导入jiar的相关的依赖包(就是jira对应的WEB-INF/lib目录里的jar包和 WEB-INF/classes目录中的class文件,同时,还要导入servlet-api.jar)
新建一个类,代码如下:

package cn.cmri.atlassian.jira.auth;
/*
* Created with Intellij IDEA
* USER: JiaoYping
* Mail: jiaoyiping@gmail.com
* Date: 2017/10/16
* Time: 15:43
* To change this template use File | Settings | Editor | File and Code Templates
*/

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.user.util.UserManager;
import com.atlassian.seraph.auth.AuthenticatorException;
import com.atlassian.seraph.auth.DefaultAuthenticator;

import java.security.Principal;

public class JiraRemoteUserAuth extends DefaultAuthenticator {

private static final String REMOTE_USER_HEADER = "remote_user";


@Override
protected Principal getUser(String userName) {
    return getUserManager().getUserByName(userName);
}

@Override
protected boolean authenticate(Principal principal, String password) throws AuthenticatorException {
    //在能够访问到这个段代码之前,就已经通过了apache的认证了,所以,直接返回true
    return true;
}

@Override
public Principal getUser(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse) {
    String remoteUser = httpServletRequest.getHeader(REMOTE_USER_HEADER);
    if (getUser(remoteUser) != null) {
        return getUser(remoteUser);
    }
    return super.getUser(httpServletRequest, httpServletResponse);
}

private UserManager getUserManager() {
    return ComponentAccessor.getUserManager();
}
}

编译这个java文件为class文件,放到jira的WEB-INF/classes下的对应目录里

2.修改seraph-config.xml文件中的authenticator为自己实现的Authenticator,注释掉原先的配置。

<!--<authenticator class="com.atlassian.jira.security.login.JiraSeraphAuthenticator"/>-->
<authenticator class="cn.cmri.atlassian.jira.auth.JiraRemoteUserAuth"/>

重新启动JIRA,enjoy

原文地址:https://www.cnblogs.com/jiaoyiping/p/7683813.html