【发布时间】:2021-11-02 11:19:36
【问题描述】:
我尝试在我的项目中集成 Spring Security SAML。我体验过okta的demo。而且我知道 URI /saml/SSO 用于使用来自 Idp 的 SAMLResponse。但我在 Idp 中注册了一个 与 URI /saml/SSO 不同的消费 uri,并且无法更改。
所以我必须想办法改变默认的 SSO processesUrl。
我认为我需要更改一些内容,如下所示:
- 更改过滤器链代理
public FilterChainProxy samlFilter() throws Exception {
List<SecurityFilterChain> chains = new ArrayList<>();
chains.add(new DefaultSecurityFilterChain(new AntPathRequestMatcher("/saml/login/**"),
samlEntryPoint));
chains.add(new DefaultSecurityFilterChain(new AntPathRequestMatcher("/saml/logout/**"),
samlLogoutFilter));
chains.add(new DefaultSecurityFilterChain(new AntPathRequestMatcher("/saml/metadata/**"),
metadataDisplayFilter));
// chains.add(new DefaultSecurityFilterChain(new AntPathRequestMatcher("/saml/SSO/**"),
// samlWebSSOProcessingFilter()));
chains.add(new DefaultSecurityFilterChain(new AntPathRequestMatcher("/saml/consume/**"),
samlWebSSOProcessingFilter()));
chains.add(new DefaultSecurityFilterChain(new AntPathRequestMatcher("/saml/SSOHoK/**"),
samlWebSSOHoKProcessingFilter()));
chains.add(new DefaultSecurityFilterChain(new AntPathRequestMatcher("/saml/SingleLogout/**"),
samlLogoutProcessingFilter));
chains.add(new DefaultSecurityFilterChain(new AntPathRequestMatcher("/saml/discovery/**"),
samlDiscovery));
return new FilterChainProxy(chains);
}
- 创建一个新类来扩展 SAMLProcessingFilter
public class CustomSAMLProcessingFilter extends SAMLProcessingFilter {
public CustomSAMLProcessingFilter() {
this("/saml/consume");
}
public CustomSAMLProcessingFilter(String defaultFilterProcessesUrl) {
super(defaultFilterProcessesUrl);
}
@Override
protected String getProfileName() {
return super.getProfileName();
}
@Override
protected boolean requiresAuthentication(HttpServletRequest request, HttpServletResponse response) {
return super.requiresAuthentication(request, response);
}
@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
return super.attemptAuthentication(request, response);
}
}
- 将 SAMLProcessingFilter 替换为 CustomSAMLProcessingFilter
@Bean
public SAMLProcessingFilter samlWebSSOProcessingFilter() throws Exception {
// SAMLProcessingFilter samlWebSSOProcessingFilter = new SAMLProcessingFilter();
SAMLProcessingFilter samlWebSSOProcessingFilter = new CustomSAMLProcessingFilter("/saml/consume");
samlWebSSOProcessingFilter.setAuthenticationManager(authenticationManager());
samlWebSSOProcessingFilter.setAuthenticationSuccessHandler(samlAuthSuccessHandler);
samlWebSSOProcessingFilter.setAuthenticationFailureHandler(samlAuthFailureHandler);
return samlWebSSOProcessingFilter;
}
- 允许消费 uri
http
.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/pre-auth**").permitAll()
.antMatchers("/form-login**").permitAll()
.antMatchers("/error").permitAll()
.antMatchers("/saml/**").permitAll()
.antMatchers("/saml2/**").permitAll()
.antMatchers("/css/**").permitAll()
.antMatchers("/img/**").permitAll()
.antMatchers("/js/**").permitAll()
.antMatchers("/sw.js").permitAll()
.anyRequest().authenticated();
之后,当我尝试登录时,Filter Chain 中没有任何反应。在控制台中,我的项目收到了 SAMLResponse,但没有更多反应。网页显示错误页面。
当我之前做 Okat 演示时,它会获取我的登录信息并显示在成功页面中。
我想知道如何更改默认 SSO processesUrl 并成功登录。请各位大神给我一些想法或建议来帮助我解决这个问题,非常感谢!
【问题讨论】:
标签: spring-security okta spring-saml