【问题标题】:Unable to change default "reply Url" (assertion consumer service Location) in Spring security SAML SSO无法更改 Spring 安全 SAML SSO 中的默认“回复 URL”(断言消费者服务位置)
【发布时间】:2021-07-09 21:54:31
【问题描述】:

我正在将 spring security Saml 2.0 与 Spring Boot 一起用于 SSO(单点登录),并将 azure 作为身份提供者。

Spring security 使用“{baseUrl}/login/saml2/sso/{registrationId}”作为默认的“Reply Url”,

但我想使用“{baseUrl}/login/{registrationId}”

所以关注Official documentation 我写了

RelyingPartyRegistration relyingPartyRegistration = RelyingPartyRegistrations
                .fromMetadataLocation("https://login.microsoftonline.com/<metadata url>")
                .registrationId("azure")
                .entityId("{baseUrl}")
                .assertionConsumerServiceLocation("{baseUrl}/login/{registrationId}")
                .build();

通过这个我进入登录页面,但之后有无限循环登录......

Spring boot 无法 POST 到 /login/azure

o.s.security.web.FilterChainProxy        : Securing POST /login/azure
s.s.w.c.SecurityContextPersistenceFilter : Set SecurityContextHolder to empty SecurityContext
o.s.security.web.csrf.CsrfFilter         : Invalid CSRF token found for http://localhost:8080/login/azure
o.s.s.w.access.AccessDeniedHandlerImpl   : Responding with 403 status code

我已尝试允许此端点的 CSRF 并允许所有访问,但是它无法解析元数据。

我发现它是在过滤器“Saml2WebSsoAuthenticationFilter”中实现的

【问题讨论】:

    标签: spring spring-boot spring-security spring-saml spring-security-saml2


    【解决方案1】:

    查看源代码我找到了解决方案。

    原来你必须在 2 个地方更新“回复 URL”链接

    1. RelyingPartyRegistrationapplication.properties 中,就像我在问题中所做的那样。

    这将告诉 Spring 成功登录后页面将被重定向到哪里,在此 URL 上 IP(身份提供者)将提供 XML 格式的 SAML 响应。

    1. WebSecurityConfigurerAdapter

    所以我们必须明确地告诉 Spring 期待这个 URL 上的 SAML 响应并解析它。

    @EnableWebSecurity
    public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            
            http
                    .authorizeRequests(authorize -> authorize
                            .anyRequest().authenticated()
                    )
                    .saml2Login(h -> h.loginProcessingUrl("/login/{registrationId}"));
       }
    }
    

    这将更新 Saml2WebSsoAuthenticationFilter 以使用 /login/{registrationId} 而非 /login/saml2/sso/{registrationId} 进行 SAML 解析

    【讨论】:

      猜你喜欢
      • 2017-06-26
      • 1970-01-01
      • 2021-11-02
      • 2013-02-03
      • 2018-06-18
      • 1970-01-01
      • 2016-01-25
      • 1970-01-01
      • 2014-11-13
      相关资源
      最近更新 更多