我正在尝试将Spring Security SAML Extension与Spring Boot集成。
关于这件事,我确实开发了一个完整的示例应用程序。其源代码可在 GitHub 上找到:
通过将其作为 Spring Boot 应用程序运行(针对 SDK 内置应用程序服务器运行),WebApp 可以正常工作。
不幸的是,同样的 AuthN 过程在Undertow/WildFly上根本不起作用。
根据日志,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("jdoe@samplemail.com") != 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
类。在运行时, 的属性FILTER_APPLIED
有ServletRequest
一个空值,因此 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上,一切正常。你对解决这个问题有什么想法吗?