此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Security 6.5.3spring-doc.cadn.net.cn

SAML 2.0 迁移

期望<saml2:LogoutResponse>什么时候<saml2:LogoutRequest>验证失败

SAML 身份提供者希望服务提供商返回错误<saml2:LogoutResponse>如果无法处理<saml2:LogoutRequest>.spring-doc.cadn.net.cn

在某些情况下,过去版本的 Spring Security 返回 401,从而打破了每个信赖方的注销请求和响应链。spring-doc.cadn.net.cn

在 Spring Security 7 中,此行为已修复,您无需执行任何作。spring-doc.cadn.net.cn

但是,如果这给您带来麻烦,您可以通过发布Saml2LogoutRequestResolver返回null当错误时<saml2:LogoutRequest>是需要的。 您可以创建如下所示的委托:spring-doc.cadn.net.cn

@Bean
Saml2LogoutResponseResolver logoutResponseResolver(RelyingPartyRegistrationRepository registrations) {
    OpenSaml5LogoutResponseResolver delegate = new OpenSaml5LogoutResponseResolver(registrations);
    return new Saml2LogoutResponseResolver() {
        @Override
        public void resolve(HttpServletRequest request, Authentication authentication) {
            delegate.resolve(request, authentication);
        }

        @Override
        public void resolve(HttpServletRequest request, Authentication authentication, Saml2AuthenticationException error) {
            return null;
        }
    };
}
@Bean
fun logoutResponseResolver(registrations: RelyingPartyRegistrationRepository?): Saml2LogoutResponseResolver {
    val delegate = OpenSaml5LogoutResponseResolver(registrations)
    return object : Saml2LogoutResponseResolver() {
        override fun resolve(request: HttpServletRequest?, authentication: Authentication?) {
            delegate.resolve(request, authentication)
        }

        override fun resolve(request: HttpServletRequest?, authentication: Authentication?, error: Saml2AuthenticationException?) {
            return null
        }
    }
}

喜爱Saml2ResponseAuthenticationAccessorSaml2AuthenticatedPrincipal

Spring Security 7 分离<saml2:Assertion>校长的详细信息。 这允许 Spring Security 检索所需的断言详细信息以执行单次注销。spring-doc.cadn.net.cn

这弃用了Saml2AuthenticatedPrincipal. 您不再需要实现它即可使用Saml2Authentication.spring-doc.cadn.net.cn

相反,凭据实现Saml2ResponseAssertionAccessor,Spring Security 7 在根据身份验证确定适当的作时会偏爱这一点。spring-doc.cadn.net.cn

使用默认值时,会自动为您进行此更改。spring-doc.cadn.net.cn

如果这导致您在升级时遇到麻烦,您可以发布自定义ResponseAuhenticationConverter返回Saml2Authentication而不是返回Saml2AssertionAuthentication这样:spring-doc.cadn.net.cn

@Bean
OpenSaml5AuthenticationProvider authenticationProvider() {
	OpenSaml5AuthenticationProvider authenticationProvider =
		new OpenSaml5AuthenticationProvider();
	ResponseAuthenticationConverter defaults = new ResponseAuthenticationConverter();
	authenticationProvider.setResponseAuthenticationConverter(
		defaults.andThen((authentication) -> new Saml2Authentication(
			authentication.getPrincipal(),
			authentication.getSaml2Response(),
			authentication.getAuthorities())));
	return authenticationProvider;
}
@Bean
fun authenticationProvider(): OpenSaml5AuthenticationProvider {
	val authenticationProvider = OpenSaml5AuthenticationProvider()
	val defaults = ResponseAuthenticationConverter()
	authenticationProvider.setResponseAuthenticationConverter(
		defaults.andThen { authentication ->
			Saml2Authentication(authentication.getPrincipal(),
				authentication.getSaml2Response(),
				authentication.getAuthorities())
		})
	return authenticationProvider
}

如果您正在构建Saml2Authentication实例化,考虑更改为Saml2AssertionAuthentication以获得与当前默认相同的好处。spring-doc.cadn.net.cn

不处理<saml2:Response>GET 请求与Saml2AuthenticationTokenConverter

Spring Security 不支持处理<saml2:Response>有效负载,因为 SAML 2.0 规范不支持这一点。spring-doc.cadn.net.cn

为了更好地遵守这一点,Saml2AuthenticationTokenConverterOpenSaml5AuthenticationTokenConverter默认情况下不会处理 GET 请求,从 Spring Security 8 开始。 为此,该物业shouldConvertGetRequests可用。 要使用它,请发布您自己的转换器,如下所示:spring-doc.cadn.net.cn

@Bean
OpenSaml5AuthenticationTokenConverter authenticationConverter(RelyingPartyRegistrationRepository registrations) {
	OpenSaml5AuthenticationTokenConverter authenticationConverter = new OpenSaml5AuthenticationTokenConverter(registrations);
	authenticationConverter.setShouldConvertGetRequests(false);
	return authenticationConverter;
}
@Bean
fun authenticationConverter(val registrations: RelyingPartyRegistrationRepository): Saml2AuthenticationTokenConverter {
	val authenticationConverter = Saml2AuthenticationTokenConverter(registrations)
	authenticationConverter.setShouldConvertGetRequests(false)
	return authenticationConverter
}

如果您必须继续使用Saml2AuthenticationTokenConverterOpenSaml5AuthenticationTokenConverter要处理 GET 请求,您可以调用setShouldConvertGetRequeststrue.spring-doc.cadn.net.cn