我的工作,以Spring Security SAML Extension与Spring Boot整合。春季安全上Wildfly /暗潮:错误执行过滤器链
我开发了一个完整的示例应用程序,所有的源代码发布在GitHub上:
通过运行Web应用程序如春启动的应用程序(通过弹簧工具集,由使用嵌入式应用服务器),它工作正常。 不幸的是,验证过程不能在Undertow/WildFly(我必须使用它作为生产AS)。
通过日志,我可以看到的IdP执行AuthN过程和我的自定义UserDetails
执行指令正确执行。尽管Spring没有为当前用户设置权限。
@Component
public class SAMLUserDetailsServiceImpl implements SAMLUserDetailsService {
// Logger
private static final Logger LOG = LoggerFactory.getLogger(SAMLUserDetailsServiceImpl.class);
@Override
public Object loadUserBySAML(SAMLCredential credential)
throws UsernameNotFoundException, SSOUserAccountNotExistsException {
String userID = credential.getNameID().getValue();
if (userID.compareTo("[email protected]") != 0) { // We're simulating the data access.
LOG.warn("SSO User Account not found into the system");
throw new SSOUserAccountNotExistsException("SSO User Account not found into the system", userID);
}
LOG.info(userID + " is logged in");
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
GrantedAuthority authority = new SimpleGrantedAuthority("ROLE_USER");
authorities.add(authority);
ExtUser userDetails = new ExtUser(userID, "password", true, true, true,
true, authorities, "John", "Doe");
return userDetails;
}
}
通过调试,我检查了问题从FilterChainProxy
类开始。 当我在WildFly上运行webapp时,可以看到ServletRequest
的属性FILTER_APPLIED
是null,因此Spring会清除SecurityContextHolder
。
private final static String FILTER_APPLIED = FilterChainProxy.class.getName().concat(".APPLIED");
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
boolean clearContext = request.getAttribute(FILTER_APPLIED) == null;
if (clearContext) {
try {
request.setAttribute(FILTER_APPLIED, Boolean.TRUE);
doFilterInternal(request, response, chain);
} finally {
SecurityContextHolder.clearContext();
request.removeAttribute(FILTER_APPLIED);
}
} else {
doFilterInternal(request, response, chain);
}
}
在的VMware的vFabric TC Sever的和Tomcat的这不会发生。 有没有办法解决这个问题?
在大多数情况下后,'SecurityContextHolder'应请求后清除。该代码的唯一目的是在过滤器链在同一请求期间多次应用(在这种情况下,只有原始链应清除上下文)。所以我不认为这是一个问题。 –
顺便说一下,这种行为每次都会阻止登录进程。有没有办法解决它,例如通过正确配置我的软件的AS? – vdenotaris
不确定你的意思。什么行为,以及它如何使登录无效?当线程完成处理请求时清除上下文是正常行为 - 防止将线程本地数据泄漏回线程池是非常重要的。此时,上下文通常应该缓存在用户的会话中。所以它不应该使登录无效。 –