无法更改 Spring security SAML SSO 中的默认“回复 Url”(断言消费者服务位置)

Ket*_*ari 5 spring spring-security spring-boot spring-saml spring-security-saml2

我使用spring security Saml 2.0和 spring boot 进行 SSO(单点登录),并使用 azure 作为身份提供者。

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

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

所以按照 我写的官方文档

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

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

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
Run Code Online (Sandbox Code Playgroud)

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

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

Ket*_*ari 7

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

事实证明,您必须在 2 个地方更新“回复 URL”链接

  1. RelyingPartyRegistration或在application.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}"));
   }
}
Run Code Online (Sandbox Code Playgroud)

这将更新Saml2WebSsoAuthenticationFilter为用于/login/{registrationId}SAML 解析,而不是/login/saml2/sso/{registrationId}