【问题标题】:How to alter the default SP SSO URL of Spring Security SAML?如何更改 Spring Security SAML 的默认 SP SSO URL?
【发布时间】:2021-11-02 11:19:36
【问题描述】:

我尝试在我的项目中集成 Spring Security SAML。我体验过okta的demo。而且我知道 URI /saml/SSO 用于使用来自 Idp 的 SAMLResponse。但我在 Idp 中注册了一个 与 URI /saml/SSO 不同的消费 uri,并且无法更改。

所以我必须想办法改变默认的 SSO processesUrl。

我认为我需要更改一些内容,如下所示:

  1. 更改过滤器链代理
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);
       }
  1. 创建一个新类来扩展 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);
    }
}
  1. 将 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;
    }
  1. 允许消费 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


    【解决方案1】:

    看来您正在使用处于维护模式的旧 spring-security-saml 项目。请改用Spring Security's SAML support。

    在 Spring Security 的 SAML 支持中,assertionConsumerServiceLocation 的默认值为 /login/saml2/sso/{registrationId}。
    应用程序将处理任何包含 SAMLResponse 参数的 POST /login/saml2/sso/{registrationId} 请求。

    您可以在创建 RelyingPartyRegistration 时对其进行自定义。

    如果你使用的是 Spring Boot,你可以在 application.yml 中进行配置。

    spring:
      security:
        saml2:
          relyingparty:
            registration:
              one:
                identityprovider:
                  metadata-uri: https://idp/metadata.php
                acs:
                  location: "{baseUrl}/custom/{registrationId}"
    

    要更新登录处理URL,可以自定义HttpSecurity

    http
        .saml2Login(saml2 -> saml2
                .loginProcessingUrl("/custom/{registrationId}")
        );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-11
      • 1970-01-01
      • 2019-07-25
      • 2014-10-08
      • 2014-10-28
      • 1970-01-01
      • 2021-07-09
      相关资源
      最近更新 更多