SAML 2.0 迁移

<saml2:LogoutRequest> 验证失败时,预期 <saml2:LogoutResponse>

SAML 身份提供者要求服务提供者在无法处理 <saml2:LogoutRequest> 时返回一个错误 <saml2:LogoutResponse>

Spring Security 的早期版本在某些情况下会返回 401 错误,这会中断每个依赖方的注销请求和响应链。

在 Spring Security 7 中,此行为已修复,您无需执行任何操作。

但是,如果这给您带来麻烦,您可以通过发布一个 Saml2LogoutRequestResolver 来恢复旧行为,该解析器在需要错误 <saml2:LogoutRequest> 时返回 null。您可以像这样创建一个委托

  • Java

  • Kotlin

@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
        }
    }
}

倾向于使用 Saml2ResponseAuthenticationAccessor 而不是 Saml2AuthenticatedPrincipal

Spring Security 7 将 <saml2:Assertion> 详细信息从主体中分离出来。这允许 Spring Security 检索所需的断言详细信息以执行单点注销。

这会弃用 Saml2AuthenticatedPrincipal。您不再需要实现它来使用 Saml2Authentication

相反,凭据实现了 Saml2ResponseAssertionAccessor,Spring Security 7 在根据身份验证确定适当操作时会优先使用它。

当使用默认值时,此更改会自动为您完成。

如果在升级时这给您带来麻烦,您可以发布一个自定义的 ResponseAuhenticationConverter 来返回 Saml2Authentication,而不是像这样返回 Saml2AssertionAuthentication

  • Java

  • Kotlin

@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,以获得与当前默认值相同的益处。

不要使用 Saml2AuthenticationTokenConverter 处理 <saml2:Response> GET 请求

Spring Security 不支持通过 GET 处理 <saml2:Response> 有效载荷,因为 SAML 2.0 规范不支持此操作。

为了更好地遵守这一点,从 Spring Security 8 开始,Saml2AuthenticationTokenConverterOpenSaml5AuthenticationTokenConverter 默认将不处理 GET 请求。为了为此做准备,提供了属性 shouldConvertGetRequests。要使用它,请像这样发布您自己的转换器

  • Java

  • Kotlin

@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 请求,您可以将 setShouldConvertGetRequests 设置为 true

© . This site is unofficial and not affiliated with VMware.