public class RequestContextFilter implements Filter {
private boolean ssoEnabled;
@Override
public void init(FilterConfig filterConfig)throws ServletException {
ssoEnabled = Boolean.TRUE.toString().equals(filterConfig.getInitParameter("ssoEnabled"));
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)throws IOException, ServletException {
String remoteUser = ((HttpServletRequest) servletRequest).getRemoteUser();
if (!ssoEnabled || Strings.isNullOrEmpty(remoteUser)) {
remoteUser = CommonConstants.DEFAULT_USER;
}
LocalUserInfo localUserInfo =new LocalUserInfo();
// 将用户信息存放到threadLocal中
// 对于有权限调试的用户,可以修改当前登录用户名
String debugUser = CommonConstants.DEBUG_USER_MAP.get(remoteUser);
if (!Strings.isNullOrEmpty(debugUser)) {
localUserInfo.setUserId(debugUser);
}else {
localUserInfo.setUserId(remoteUser);
}
localUserInfo.setRealUserId(remoteUser);
RequestContextUtil.setLocalUserInfo(localUserInfo);
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() {
}
}