协议端点
OAuth2 授权端点
OAuth2AuthorizationEndpointConfigurer
提供自定义 OAuth2 授权端点 的能力。它定义了扩展点,允许您自定义 OAuth2 授权请求 的预处理、主要处理和后处理逻辑。
OAuth2AuthorizationEndpointConfigurer
提供以下配置选项
@Bean
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
OAuth2AuthorizationServerConfigurer.authorizationServer();
http
.securityMatcher(authorizationServerConfigurer.getEndpointsMatcher())
.with(authorizationServerConfigurer, (authorizationServer) ->
authorizationServer
.authorizationEndpoint(authorizationEndpoint ->
authorizationEndpoint
.authorizationRequestConverter(authorizationRequestConverter) (1)
.authorizationRequestConverters(authorizationRequestConvertersConsumer) (2)
.authenticationProvider(authenticationProvider) (3)
.authenticationProviders(authenticationProvidersConsumer) (4)
.authorizationResponseHandler(authorizationResponseHandler) (5)
.errorResponseHandler(errorResponseHandler) (6)
.consentPage("/oauth2/v1/authorize") (7)
)
);
return http.build();
}
1 | authorizationRequestConverter() : 添加一个 AuthenticationConverter (预处理器),用于尝试将 OAuth2 授权请求(或同意)从 HttpServletRequest 中提取到 OAuth2AuthorizationCodeRequestAuthenticationToken 或 OAuth2AuthorizationConsentAuthenticationToken 的实例。 |
2 | authorizationRequestConverters() : 设置 Consumer ,提供对默认和(可选)添加的 AuthenticationConverter 列表的访问权限,允许添加、删除或自定义特定的 AuthenticationConverter 。 |
3 | authenticationProvider() : 添加一个 AuthenticationProvider (主处理器),用于验证 OAuth2AuthorizationCodeRequestAuthenticationToken 或 OAuth2AuthorizationConsentAuthenticationToken 。 |
4 | authenticationProviders() : 设置 Consumer ,提供对默认和(可选)添加的 AuthenticationProvider 列表的访问权限,允许添加、删除或自定义特定的 AuthenticationProvider 。 |
5 | authorizationResponseHandler() : 用于处理已验证的 OAuth2AuthorizationCodeRequestAuthenticationToken 并返回 OAuth2AuthorizationResponse 的 AuthenticationSuccessHandler (后处理器)。 |
6 | errorResponseHandler() : 用于处理 OAuth2AuthorizationCodeRequestAuthenticationException 并返回 OAuth2Error 响应 的 AuthenticationFailureHandler (后处理器)。 |
7 | consentPage() : 自定义同意页面的 URI ,如果在授权请求流程中需要同意,资源所有者将被重定向到此页面。 |
OAuth2AuthorizationEndpointConfigurer
配置 OAuth2AuthorizationEndpointFilter
并将其注册到 OAuth2 授权服务器的 SecurityFilterChain
@Bean
中。OAuth2AuthorizationEndpointFilter
是处理 OAuth2 授权请求(和同意)的 Filter
。
OAuth2AuthorizationEndpointFilter
配置了以下默认值
-
AuthenticationConverter
— 由OAuth2AuthorizationCodeRequestAuthenticationConverter
和OAuth2AuthorizationConsentAuthenticationConverter
组成的DelegatingAuthenticationConverter
。 -
AuthenticationManager
— 由OAuth2AuthorizationCodeRequestAuthenticationProvider
和OAuth2AuthorizationConsentAuthenticationProvider
组成的AuthenticationManager
。 -
AuthenticationSuccessHandler
— 一个内部实现,用于处理已验证的OAuth2AuthorizationCodeRequestAuthenticationToken
并返回OAuth2AuthorizationResponse
。 -
AuthenticationFailureHandler
— 一个内部实现,使用与OAuth2AuthorizationCodeRequestAuthenticationException
关联的OAuth2Error
并返回OAuth2Error
响应。
自定义授权请求验证
OAuth2AuthorizationCodeRequestAuthenticationValidator
是用于验证授权码 Grant 中使用的特定 OAuth2 授权请求参数的默认验证器。默认实现验证 redirect_uri
和 scope
参数。如果验证失败,则抛出 OAuth2AuthorizationCodeRequestAuthenticationException
。
OAuth2AuthorizationCodeRequestAuthenticationProvider
允许通过向 setAuthenticationValidator()
提供一个类型为 Consumer<OAuth2AuthorizationCodeRequestAuthenticationContext>
的自定义身份验证验证器来覆盖默认的授权请求验证。
OAuth2AuthorizationCodeRequestAuthenticationContext 包含 OAuth2AuthorizationCodeRequestAuthenticationToken ,后者包含 OAuth2 授权请求参数。 |
如果验证失败,身份验证验证器必须抛出 OAuth2AuthorizationCodeRequestAuthenticationException 。 |
在开发生命周期阶段的一个常见用例是允许 redirect_uri
参数中使用 localhost
。
以下示例展示了如何使用允许 redirect_uri
参数中使用 localhost
的自定义身份验证验证器配置 OAuth2AuthorizationCodeRequestAuthenticationProvider
@Bean
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
OAuth2AuthorizationServerConfigurer.authorizationServer();
http
.securityMatcher(authorizationServerConfigurer.getEndpointsMatcher())
.with(authorizationServerConfigurer, (authorizationServer) ->
authorizationServer
.authorizationEndpoint(authorizationEndpoint ->
authorizationEndpoint
.authenticationProviders(configureAuthenticationValidator())
)
);
return http.build();
}
private Consumer<List<AuthenticationProvider>> configureAuthenticationValidator() {
return (authenticationProviders) ->
authenticationProviders.forEach((authenticationProvider) -> {
if (authenticationProvider instanceof OAuth2AuthorizationCodeRequestAuthenticationProvider) {
Consumer<OAuth2AuthorizationCodeRequestAuthenticationContext> authenticationValidator =
// Override default redirect_uri validator
new CustomRedirectUriValidator()
// Reuse default scope validator
.andThen(OAuth2AuthorizationCodeRequestAuthenticationValidator.DEFAULT_SCOPE_VALIDATOR);
((OAuth2AuthorizationCodeRequestAuthenticationProvider) authenticationProvider)
.setAuthenticationValidator(authenticationValidator);
}
});
}
static class CustomRedirectUriValidator implements Consumer<OAuth2AuthorizationCodeRequestAuthenticationContext> {
@Override
public void accept(OAuth2AuthorizationCodeRequestAuthenticationContext authenticationContext) {
OAuth2AuthorizationCodeRequestAuthenticationToken authorizationCodeRequestAuthentication =
authenticationContext.getAuthentication();
RegisteredClient registeredClient = authenticationContext.getRegisteredClient();
String requestedRedirectUri = authorizationCodeRequestAuthentication.getRedirectUri();
// Use exact string matching when comparing client redirect URIs against pre-registered URIs
if (!registeredClient.getRedirectUris().contains(requestedRedirectUri)) {
OAuth2Error error = new OAuth2Error(OAuth2ErrorCodes.INVALID_REQUEST);
throw new OAuth2AuthorizationCodeRequestAuthenticationException(error, null);
}
}
}
OAuth2 设备授权端点
OAuth2DeviceAuthorizationEndpointConfigurer
提供自定义 OAuth2 设备授权端点 的能力。它定义了扩展点,允许您自定义 OAuth2 设备授权请求的预处理、主要处理和后处理逻辑。
OAuth2DeviceAuthorizationEndpointConfigurer
提供以下配置选项
@Bean
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
OAuth2AuthorizationServerConfigurer.authorizationServer();
http
.securityMatcher(authorizationServerConfigurer.getEndpointsMatcher())
.with(authorizationServerConfigurer, (authorizationServer) ->
authorizationServer
.deviceAuthorizationEndpoint(deviceAuthorizationEndpoint ->
deviceAuthorizationEndpoint
.deviceAuthorizationRequestConverter(deviceAuthorizationRequestConverter) (1)
.deviceAuthorizationRequestConverters(deviceAuthorizationRequestConvertersConsumer) (2)
.authenticationProvider(authenticationProvider) (3)
.authenticationProviders(authenticationProvidersConsumer) (4)
.deviceAuthorizationResponseHandler(deviceAuthorizationResponseHandler) (5)
.errorResponseHandler(errorResponseHandler) (6)
.verificationUri("/oauth2/v1/device_verification") (7)
)
);
return http.build();
}
1 | deviceAuthorizationRequestConverter() : 添加一个 AuthenticationConverter (预处理器),用于尝试将 OAuth2 设备授权请求 从 HttpServletRequest 中提取到 OAuth2DeviceAuthorizationRequestAuthenticationToken 的实例。 |
2 | deviceAuthorizationRequestConverters() : 设置 Consumer ,提供对默认和(可选)添加的 AuthenticationConverter 列表的访问权限,允许添加、删除或自定义特定的 AuthenticationConverter 。 |
3 | authenticationProvider() : 添加一个 AuthenticationProvider (主处理器),用于验证 OAuth2DeviceAuthorizationRequestAuthenticationToken 。 |
4 | authenticationProviders() : 设置 Consumer ,提供对默认和(可选)添加的 AuthenticationProvider 列表的访问权限,允许添加、删除或自定义特定的 AuthenticationProvider 。 |
5 | deviceAuthorizationResponseHandler() : 用于处理已验证的 OAuth2DeviceAuthorizationRequestAuthenticationToken 并返回 OAuth2DeviceAuthorizationResponse 的 AuthenticationSuccessHandler (后处理器)。 |
6 | errorResponseHandler() : 用于处理 OAuth2AuthenticationException 并返回 OAuth2Error 响应 的 AuthenticationFailureHandler (后处理器)。 |
7 | verificationUri() : 自定义终端用户验证页面的 URI ,用于引导资源所有者在辅助设备上进行操作。 |
OAuth2DeviceAuthorizationEndpointConfigurer
配置 OAuth2DeviceAuthorizationEndpointFilter
并将其注册到 OAuth2 授权服务器的 SecurityFilterChain
@Bean
中。OAuth2DeviceAuthorizationEndpointFilter
是处理 OAuth2 设备授权请求的 Filter
。
OAuth2DeviceAuthorizationEndpointFilter
配置了以下默认值
-
AuthenticationConverter
— 一个OAuth2DeviceAuthorizationRequestAuthenticationConverter
。 -
AuthenticationManager
— 由OAuth2DeviceAuthorizationRequestAuthenticationProvider
组成的AuthenticationManager
。 -
AuthenticationSuccessHandler
— 一个内部实现,用于处理已验证的OAuth2DeviceAuthorizationRequestAuthenticationToken
并返回OAuth2DeviceAuthorizationResponse
。 -
AuthenticationFailureHandler
— 一个OAuth2ErrorAuthenticationFailureHandler
。
OAuth2 设备验证端点
OAuth2DeviceVerificationEndpointConfigurer
提供自定义 OAuth2 设备验证端点(或“用户交互”)的能力。它定义了扩展点,允许您自定义 OAuth2 设备验证请求的预处理、主要处理和后处理逻辑。
OAuth2DeviceVerificationEndpointConfigurer
提供以下配置选项
@Bean
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
OAuth2AuthorizationServerConfigurer.authorizationServer();
http
.securityMatcher(authorizationServerConfigurer.getEndpointsMatcher())
.with(authorizationServerConfigurer, (authorizationServer) ->
authorizationServer
.deviceVerificationEndpoint(deviceVerificationEndpoint ->
deviceVerificationEndpoint
.deviceVerificationRequestConverter(deviceVerificationRequestConverter) (1)
.deviceVerificationRequestConverters(deviceVerificationRequestConvertersConsumer) (2)
.authenticationProvider(authenticationProvider) (3)
.authenticationProviders(authenticationProvidersConsumer) (4)
.deviceVerificationResponseHandler(deviceVerificationResponseHandler) (5)
.errorResponseHandler(errorResponseHandler) (6)
.consentPage("/oauth2/v1/consent") (7)
)
);
return http.build();
}
1 | deviceVerificationRequestConverter() : 添加一个 AuthenticationConverter (预处理器),用于尝试将 OAuth2 设备验证请求(或同意)从 HttpServletRequest 中提取到 OAuth2DeviceVerificationAuthenticationToken 或 OAuth2DeviceAuthorizationConsentAuthenticationToken 的实例。 |
2 | deviceVerificationRequestConverters() : 设置 Consumer ,提供对默认和(可选)添加的 AuthenticationConverter 列表的访问权限,允许添加、删除或自定义特定的 AuthenticationConverter 。 |
3 | authenticationProvider() : 添加一个 AuthenticationProvider (主处理器),用于验证 OAuth2DeviceVerificationAuthenticationToken 或 OAuth2DeviceAuthorizationConsentAuthenticationToken 。 |
4 | authenticationProviders() : 设置 Consumer ,提供对默认和(可选)添加的 AuthenticationProvider 列表的访问权限,允许添加、删除或自定义特定的 AuthenticationProvider 。 |
5 | deviceVerificationResponseHandler() : 用于处理已验证的 OAuth2DeviceVerificationAuthenticationToken 并引导资源所有者返回其设备的 AuthenticationSuccessHandler (后处理器)。 |
6 | errorResponseHandler() : 用于处理 OAuth2AuthenticationException 并返回错误响应的 AuthenticationFailureHandler (后处理器)。 |
7 | consentPage() : 自定义同意页面的 URI ,如果在设备验证请求流程中需要同意,资源所有者将被重定向到此页面。 |
OAuth2DeviceVerificationEndpointConfigurer
配置 OAuth2DeviceVerificationEndpointFilter
并将其注册到 OAuth2 授权服务器的 SecurityFilterChain
@Bean
中。OAuth2DeviceVerificationEndpointFilter
是处理 OAuth2 设备验证请求(和同意)的 Filter
。
OAuth2DeviceVerificationEndpointFilter
配置了以下默认值
-
AuthenticationConverter
— 由OAuth2DeviceVerificationAuthenticationConverter
和OAuth2DeviceAuthorizationConsentAuthenticationConverter
组成的DelegatingAuthenticationConverter
。 -
AuthenticationManager
— 由OAuth2DeviceVerificationAuthenticationProvider
和OAuth2DeviceAuthorizationConsentAuthenticationProvider
组成的AuthenticationManager
。 -
AuthenticationSuccessHandler
— 一个SimpleUrlAuthenticationSuccessHandler
,用于处理已验证的OAuth2DeviceVerificationAuthenticationToken
并将用户重定向到成功页面 (/?success
)。 -
AuthenticationFailureHandler
— 一个内部实现,使用与OAuth2AuthenticationException
关联的OAuth2Error
并返回OAuth2Error
响应。
OAuth2 令牌端点
OAuth2TokenEndpointConfigurer
提供自定义 OAuth2 令牌端点 的能力。它定义了扩展点,允许您自定义 OAuth2 访问令牌请求 的预处理、主要处理和后处理逻辑。
OAuth2TokenEndpointConfigurer
提供以下配置选项
@Bean
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
OAuth2AuthorizationServerConfigurer.authorizationServer();
http
.securityMatcher(authorizationServerConfigurer.getEndpointsMatcher())
.with(authorizationServerConfigurer, (authorizationServer) ->
authorizationServer
.tokenEndpoint(tokenEndpoint ->
tokenEndpoint
.accessTokenRequestConverter(accessTokenRequestConverter) (1)
.accessTokenRequestConverters(accessTokenRequestConvertersConsumer) (2)
.authenticationProvider(authenticationProvider) (3)
.authenticationProviders(authenticationProvidersConsumer) (4)
.accessTokenResponseHandler(accessTokenResponseHandler) (5)
.errorResponseHandler(errorResponseHandler) (6)
)
);
return http.build();
}
1 | accessTokenRequestConverter() : 添加一个 AuthenticationConverter (预处理器),用于尝试将 OAuth2 访问令牌请求 从 HttpServletRequest 中提取到 OAuth2AuthorizationGrantAuthenticationToken 的实例。 |
2 | accessTokenRequestConverters() : 设置 Consumer ,提供对默认和(可选)添加的 AuthenticationConverter 列表的访问权限,允许添加、删除或自定义特定的 AuthenticationConverter 。 |
3 | authenticationProvider() : 添加一个 AuthenticationProvider (主处理器),用于验证 OAuth2AuthorizationGrantAuthenticationToken 。 |
4 | authenticationProviders() : 设置 Consumer ,提供对默认和(可选)添加的 AuthenticationProvider 列表的访问权限,允许添加、删除或自定义特定的 AuthenticationProvider 。 |
5 | accessTokenResponseHandler() : 用于处理 OAuth2AccessTokenAuthenticationToken 并返回 OAuth2AccessTokenResponse 的 AuthenticationSuccessHandler (后处理器)。 |
6 | errorResponseHandler() : 用于处理 OAuth2AuthenticationException 并返回 OAuth2Error 响应 的 AuthenticationFailureHandler (后处理器)。 |
OAuth2TokenEndpointConfigurer
配置 OAuth2TokenEndpointFilter
并将其注册到 OAuth2 授权服务器的 SecurityFilterChain
@Bean
中。OAuth2TokenEndpointFilter
是处理 OAuth2 访问令牌请求的 Filter
。
支持的 授权 Grant 类型 包括 authorization_code
、refresh_token
、client_credentials
、urn:ietf:params:oauth:grant-type:device_code
和 urn:ietf:params:oauth:grant-type:token_exchange
。
OAuth2TokenEndpointFilter
配置了以下默认值
-
AuthenticationConverter
— 由OAuth2AuthorizationCodeAuthenticationConverter
、OAuth2RefreshTokenAuthenticationConverter
、OAuth2ClientCredentialsAuthenticationConverter
、OAuth2DeviceCodeAuthenticationConverter
和OAuth2TokenExchangeAuthenticationConverter
组成的DelegatingAuthenticationConverter
。 -
AuthenticationManager
— 由OAuth2AuthorizationCodeAuthenticationProvider
、OAuth2RefreshTokenAuthenticationProvider
、OAuth2ClientCredentialsAuthenticationProvider
、OAuth2DeviceCodeAuthenticationProvider
和OAuth2TokenExchangeAuthenticationProvider
组成的AuthenticationManager
。 -
AuthenticationSuccessHandler
— 一个OAuth2AccessTokenResponseAuthenticationSuccessHandler
。 -
AuthenticationFailureHandler
— 一个OAuth2ErrorAuthenticationFailureHandler
。
自定义客户端凭据 Grant 请求验证
OAuth2ClientCredentialsAuthenticationValidator
是用于验证特定 OAuth2 客户端凭据 Grant 请求参数的默认验证器。默认实现验证 scope
参数。如果验证失败,则抛出 OAuth2AuthenticationException
。
OAuth2ClientCredentialsAuthenticationProvider
允许通过向 setAuthenticationValidator()
提供一个类型为 Consumer<OAuth2ClientCredentialsAuthenticationContext>
的自定义身份验证验证器来覆盖默认的请求验证。
OAuth2ClientCredentialsAuthenticationContext 包含 OAuth2ClientCredentialsAuthenticationToken ,后者包含 OAuth2 客户端凭据 Grant 请求参数。 |
如果验证失败,身份验证验证器必须抛出 OAuth2AuthenticationException 。 |
以下示例展示了如何使用覆盖默认 scope
验证的自定义身份验证验证器配置 OAuth2ClientCredentialsAuthenticationProvider
@Bean
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
OAuth2AuthorizationServerConfigurer.authorizationServer();
http
.securityMatcher(authorizationServerConfigurer.getEndpointsMatcher())
.with(authorizationServerConfigurer, (authorizationServer) ->
authorizationServer
.tokenEndpoint(tokenEndpoint ->
tokenEndpoint
.authenticationProviders(configureAuthenticationValidator())
)
);
return http.build();
}
private Consumer<List<AuthenticationProvider>> configureAuthenticationValidator() {
return (authenticationProviders) ->
authenticationProviders.forEach((authenticationProvider) -> {
if (authenticationProvider instanceof OAuth2ClientCredentialsAuthenticationProvider) {
Consumer<OAuth2ClientCredentialsAuthenticationContext> authenticationValidator =
new CustomScopeValidator();
// Override default scope validation
((OAuth2ClientCredentialsAuthenticationProvider) authenticationProvider)
.setAuthenticationValidator(authenticationValidator);
}
});
}
static class CustomScopeValidator implements Consumer<OAuth2ClientCredentialsAuthenticationContext> {
@Override
public void accept(OAuth2ClientCredentialsAuthenticationContext authenticationContext) {
OAuth2ClientCredentialsAuthenticationToken clientCredentialsAuthentication =
authenticationContext.getAuthentication();
Set<String> requestedScopes = clientCredentialsAuthentication.getScopes();
RegisteredClient registeredClient = authenticationContext.getRegisteredClient();
Set<String> allowedScopes = registeredClient.getScopes();
// TODO Implement scope validation
}
}
OAuth2 Token 内省端点
OAuth2TokenIntrospectionEndpointConfigurer
提供自定义 OAuth2 Token 内省端点 的能力。它定义了扩展点,允许您自定义 OAuth2 内省请求 的预处理、主要处理和后处理逻辑。
OAuth2TokenIntrospectionEndpointConfigurer
提供以下配置选项
@Bean
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
OAuth2AuthorizationServerConfigurer.authorizationServer();
http
.securityMatcher(authorizationServerConfigurer.getEndpointsMatcher())
.with(authorizationServerConfigurer, (authorizationServer) ->
authorizationServer
.tokenIntrospectionEndpoint(tokenIntrospectionEndpoint ->
tokenIntrospectionEndpoint
.introspectionRequestConverter(introspectionRequestConverter) (1)
.introspectionRequestConverters(introspectionRequestConvertersConsumer) (2)
.authenticationProvider(authenticationProvider) (3)
.authenticationProviders(authenticationProvidersConsumer) (4)
.introspectionResponseHandler(introspectionResponseHandler) (5)
.errorResponseHandler(errorResponseHandler) (6)
)
);
return http.build();
}
1 | introspectionRequestConverter() : 添加一个 AuthenticationConverter (预处理器),用于尝试将 OAuth2 内省请求 从 HttpServletRequest 中提取到 OAuth2TokenIntrospectionAuthenticationToken 的实例。 |
2 | introspectionRequestConverters() : 设置 Consumer ,提供对默认和(可选)添加的 AuthenticationConverter 列表的访问权限,允许添加、删除或自定义特定的 AuthenticationConverter 。 |
3 | authenticationProvider() : 添加一个 AuthenticationProvider (主处理器),用于验证 OAuth2TokenIntrospectionAuthenticationToken 。 |
4 | authenticationProviders() : 设置 Consumer ,提供对默认和(可选)添加的 AuthenticationProvider 列表的访问权限,允许添加、删除或自定义特定的 AuthenticationProvider 。 |
5 | introspectionResponseHandler() : 用于处理已验证的 OAuth2TokenIntrospectionAuthenticationToken 并返回 OAuth2TokenIntrospection 响应 的 AuthenticationSuccessHandler (后处理器)。 |
6 | errorResponseHandler() : 用于处理 OAuth2AuthenticationException 并返回 OAuth2Error 响应 的 AuthenticationFailureHandler (后处理器)。 |
OAuth2TokenIntrospectionEndpointConfigurer
配置 OAuth2TokenIntrospectionEndpointFilter
并将其注册到 OAuth2 授权服务器的 SecurityFilterChain
@Bean
中。OAuth2TokenIntrospectionEndpointFilter
是处理 OAuth2 内省请求的 Filter
。
OAuth2TokenIntrospectionEndpointFilter
配置了以下默认值
-
AuthenticationConverter
— 一个OAuth2TokenIntrospectionAuthenticationConverter
。 -
AuthenticationManager
— 由OAuth2TokenIntrospectionAuthenticationProvider
组成的AuthenticationManager
。 -
AuthenticationSuccessHandler
— 一个内部实现,用于处理已验证的OAuth2TokenIntrospectionAuthenticationToken
并返回OAuth2TokenIntrospection
响应。 -
AuthenticationFailureHandler
— 一个OAuth2ErrorAuthenticationFailureHandler
。
OAuth2 Token 吊销端点
OAuth2TokenRevocationEndpointConfigurer
提供自定义 OAuth2 Token 吊销端点 的能力。它定义了扩展点,允许您自定义 OAuth2 吊销请求 的预处理、主要处理和后处理逻辑。
OAuth2TokenRevocationEndpointConfigurer
提供以下配置选项
@Bean
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
OAuth2AuthorizationServerConfigurer.authorizationServer();
http
.securityMatcher(authorizationServerConfigurer.getEndpointsMatcher())
.with(authorizationServerConfigurer, (authorizationServer) ->
authorizationServer
.tokenRevocationEndpoint(tokenRevocationEndpoint ->
tokenRevocationEndpoint
.revocationRequestConverter(revocationRequestConverter) (1)
.revocationRequestConverters(revocationRequestConvertersConsumer) (2)
.authenticationProvider(authenticationProvider) (3)
.authenticationProviders(authenticationProvidersConsumer) (4)
.revocationResponseHandler(revocationResponseHandler) (5)
.errorResponseHandler(errorResponseHandler) (6)
)
);
return http.build();
}
1 | revocationRequestConverter() : 添加一个 AuthenticationConverter (预处理器),用于尝试将 OAuth2 吊销请求 从 HttpServletRequest 中提取到 OAuth2TokenRevocationAuthenticationToken 的实例。 |
2 | revocationRequestConverters() : 设置 Consumer ,提供对默认和(可选)添加的 AuthenticationConverter 列表的访问权限,允许添加、删除或自定义特定的 AuthenticationConverter 。 |
3 | authenticationProvider() : 添加一个 AuthenticationProvider (主处理器),用于验证 OAuth2TokenRevocationAuthenticationToken 。 |
4 | authenticationProviders() : 设置 Consumer ,提供对默认和(可选)添加的 AuthenticationProvider 列表的访问权限,允许添加、删除或自定义特定的 AuthenticationProvider 。 |
5 | revocationResponseHandler() : 用于处理已验证的 OAuth2TokenRevocationAuthenticationToken 并返回 OAuth2 吊销响应 的 AuthenticationSuccessHandler (后处理器)。 |
6 | errorResponseHandler() : 用于处理 OAuth2AuthenticationException 并返回 OAuth2Error 响应 的 AuthenticationFailureHandler (后处理器)。 |
OAuth2TokenRevocationEndpointConfigurer
配置 OAuth2TokenRevocationEndpointFilter
并将其注册到 OAuth2 授权服务器的 SecurityFilterChain
@Bean
中。OAuth2TokenRevocationEndpointFilter
是处理 OAuth2 吊销请求的 Filter
。
OAuth2TokenRevocationEndpointFilter
配置了以下默认值
-
AuthenticationConverter
— 一个OAuth2TokenRevocationAuthenticationConverter
。 -
AuthenticationManager
— 由OAuth2TokenRevocationAuthenticationProvider
组成的AuthenticationManager
。 -
AuthenticationSuccessHandler
— 一个内部实现,用于处理已验证的OAuth2TokenRevocationAuthenticationToken
并返回 OAuth2 吊销响应。 -
AuthenticationFailureHandler
— 一个OAuth2ErrorAuthenticationFailureHandler
。
OAuth2 授权服务器元数据端点
OAuth2AuthorizationServerMetadataEndpointConfigurer
提供自定义 OAuth2 授权服务器元数据端点 的能力。它定义了一个扩展点,允许您自定义 OAuth2 授权服务器元数据响应。
OAuth2AuthorizationServerMetadataEndpointConfigurer
提供以下配置选项
@Bean
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
OAuth2AuthorizationServerConfigurer.authorizationServer();
http
.securityMatcher(authorizationServerConfigurer.getEndpointsMatcher())
.with(authorizationServerConfigurer, (authorizationServer) ->
authorizationServer
.authorizationServerMetadataEndpoint(authorizationServerMetadataEndpoint ->
authorizationServerMetadataEndpoint
.authorizationServerMetadataCustomizer(authorizationServerMetadataCustomizer) (1)
)
);
return http.build();
}
1 | authorizationServerMetadataCustomizer() : 提供对 OAuth2AuthorizationServerMetadata.Builder 的访问权限的 Consumer ,允许自定义授权服务器配置的 claim。 |
OAuth2AuthorizationServerMetadataEndpointConfigurer
配置 OAuth2AuthorizationServerMetadataEndpointFilter
并将其注册到 OAuth2 授权服务器的 SecurityFilterChain
@Bean
中。OAuth2AuthorizationServerMetadataEndpointFilter
是返回 OAuth2AuthorizationServerMetadata 响应 的 Filter
。
JWK Set 端点
OAuth2AuthorizationServerConfigurer
为 JWK Set 端点 提供支持。
OAuth2AuthorizationServerConfigurer
配置 NimbusJwkSetEndpointFilter
并将其注册到 OAuth2 授权服务器的 SecurityFilterChain
@Bean
中。NimbusJwkSetEndpointFilter
是返回 JWK Set 的 Filter
。
仅当注册了 JWKSource<SecurityContext> @Bean 时,才会配置 JWK Set 端点。 |
OpenID Connect 1.0 Provider 配置端点
OidcProviderConfigurationEndpointConfigurer
提供自定义 OpenID Connect 1.0 Provider 配置端点 的能力。它定义了一个扩展点,允许您自定义 OpenID Provider 配置响应。
OidcProviderConfigurationEndpointConfigurer
提供以下配置选项
@Bean
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
OAuth2AuthorizationServerConfigurer.authorizationServer();
http
.securityMatcher(authorizationServerConfigurer.getEndpointsMatcher())
.with(authorizationServerConfigurer, (authorizationServer) ->
authorizationServer
.oidc(oidc ->
oidc
.providerConfigurationEndpoint(providerConfigurationEndpoint ->
providerConfigurationEndpoint
.providerConfigurationCustomizer(providerConfigurationCustomizer) (1)
)
)
);
return http.build();
}
1 | providerConfigurationCustomizer() : 提供对 OidcProviderConfiguration.Builder 的访问权限的 Consumer ,允许自定义 OpenID Provider 配置的 claim。 |
OidcProviderConfigurationEndpointConfigurer
配置 OidcProviderConfigurationEndpointFilter
并将其注册到 OAuth2 授权服务器的 SecurityFilterChain
@Bean
中。OidcProviderConfigurationEndpointFilter
是返回 OidcProviderConfiguration 响应 的 Filter
。
OpenID Connect 1.0 退出端点
OidcLogoutEndpointConfigurer
提供自定义 OpenID Connect 1.0 退出端点 的能力。它定义了扩展点,允许您自定义 RP 发起的退出请求的预处理、主要处理和后处理逻辑。
OidcLogoutEndpointConfigurer
提供以下配置选项
@Bean
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
OAuth2AuthorizationServerConfigurer.authorizationServer();
http
.securityMatcher(authorizationServerConfigurer.getEndpointsMatcher())
.with(authorizationServerConfigurer, (authorizationServer) ->
authorizationServer
.oidc(oidc ->
oidc
.logoutEndpoint(logoutEndpoint ->
logoutEndpoint
.logoutRequestConverter(logoutRequestConverter) (1)
.logoutRequestConverters(logoutRequestConvertersConsumer) (2)
.authenticationProvider(authenticationProvider) (3)
.authenticationProviders(authenticationProvidersConsumer) (4)
.logoutResponseHandler(logoutResponseHandler) (5)
.errorResponseHandler(errorResponseHandler) (6)
)
)
);
return http.build();
}
1 | logoutRequestConverter() : 添加一个 AuthenticationConverter (预处理器),用于尝试将 退出请求 从 HttpServletRequest 中提取到 OidcLogoutAuthenticationToken 的实例。 |
2 | logoutRequestConverters() : 设置 Consumer ,提供对默认和(可选)添加的 AuthenticationConverter 列表的访问权限,允许添加、删除或自定义特定的 AuthenticationConverter 。 |
3 | authenticationProvider() : 添加一个 AuthenticationProvider (主处理器),用于验证 OidcLogoutAuthenticationToken 。 |
4 | authenticationProviders() : 设置 Consumer ,提供对默认和(可选)添加的 AuthenticationProvider 列表的访问权限,允许添加、删除或自定义特定的 AuthenticationProvider 。 |
5 | logoutResponseHandler() : 用于处理已验证的 OidcLogoutAuthenticationToken 并执行退出的 AuthenticationSuccessHandler (后处理器)。 |
6 | errorResponseHandler() : 用于处理 OAuth2AuthenticationException 并返回错误响应的 AuthenticationFailureHandler (后处理器)。 |
OidcLogoutEndpointConfigurer
配置 OidcLogoutEndpointFilter
并将其注册到 OAuth2 授权服务器的 SecurityFilterChain
@Bean
中。OidcLogoutEndpointFilter
是处理 RP 发起的退出请求 并执行终端用户退出的 Filter
。
OidcLogoutEndpointFilter
配置了以下默认值
-
AuthenticationConverter
— 一个OidcLogoutAuthenticationConverter
。 -
AuthenticationManager
— 由OidcLogoutAuthenticationProvider
组成的AuthenticationManager
。 -
AuthenticationSuccessHandler
— 一个OidcLogoutAuthenticationSuccessHandler
。 -
AuthenticationFailureHandler
— 一个内部实现,使用与OAuth2AuthenticationException
关联的OAuth2Error
并返回OAuth2Error
响应。
OidcLogoutAuthenticationProvider 使用 SessionRegistry 查找与请求退出的终端用户关联的 SessionInformation 实例。 |
OidcClientInitiatedLogoutSuccessHandler 是 Spring Security 的 OAuth2 Client 支持中用于配置 OpenID Connect 1.0 RP 发起的退出 的相应配置。 |
自定义退出请求验证
OidcLogoutAuthenticationValidator
是用于验证特定 OpenID Connect RP 发起的退出请求参数的默认验证器。默认实现验证 post_logout_redirect_uri
参数。如果验证失败,则抛出 OAuth2AuthenticationException
。
OidcLogoutAuthenticationProvider
允许通过向 setAuthenticationValidator()
提供一个类型为 Consumer<OidcLogoutAuthenticationContext>
的自定义身份验证验证器来覆盖默认的退出请求验证。
OidcLogoutAuthenticationContext 包含 OidcLogoutAuthenticationToken ,后者包含退出请求参数。 |
如果验证失败,身份验证验证器必须抛出 OAuth2AuthenticationException 。 |
以下示例展示了如何使用自定义身份验证验证器配置 OidcLogoutAuthenticationProvider
@Bean
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
OAuth2AuthorizationServerConfigurer.authorizationServer();
http
.securityMatcher(authorizationServerConfigurer.getEndpointsMatcher())
.with(authorizationServerConfigurer, (authorizationServer) ->
authorizationServer
.oidc(oidc ->
oidc
.logoutEndpoint(logoutEndpoint ->
logoutEndpoint
.authenticationProviders(configureAuthenticationValidator())
)
)
);
return http.build();
}
private Consumer<List<AuthenticationProvider>> configureAuthenticationValidator() {
return (authenticationProviders) ->
authenticationProviders.forEach((authenticationProvider) -> {
if (authenticationProvider instanceof OidcLogoutAuthenticationProvider oidcLogoutAuthenticationProvider) {
Consumer<OidcLogoutAuthenticationContext> authenticationValidator = new CustomPostLogoutRedirectUriValidator();
oidcLogoutAuthenticationProvider.setAuthenticationValidator(authenticationValidator);
}
});
}
static class CustomPostLogoutRedirectUriValidator implements Consumer<OidcLogoutAuthenticationContext> {
@Override
public void accept(OidcLogoutAuthenticationContext authenticationContext) {
OidcLogoutAuthenticationToken oidcLogoutAuthentication =
authenticationContext.getAuthentication();
RegisteredClient registeredClient = authenticationContext.getRegisteredClient();
// TODO
}
}
OpenID Connect 1.0 UserInfo 端点
OidcUserInfoEndpointConfigurer
提供自定义 OpenID Connect 1.0 UserInfo 端点 的能力。它定义了扩展点,允许您自定义 UserInfo 请求 的预处理、主要处理和后处理逻辑。
OidcUserInfoEndpointConfigurer
提供以下配置选项
@Bean
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
OAuth2AuthorizationServerConfigurer.authorizationServer();
http
.securityMatcher(authorizationServerConfigurer.getEndpointsMatcher())
.with(authorizationServerConfigurer, (authorizationServer) ->
authorizationServer
.oidc(oidc ->
oidc
.userInfoEndpoint(userInfoEndpoint ->
userInfoEndpoint
.userInfoRequestConverter(userInfoRequestConverter) (1)
.userInfoRequestConverters(userInfoRequestConvertersConsumer) (2)
.authenticationProvider(authenticationProvider) (3)
.authenticationProviders(authenticationProvidersConsumer) (4)
.userInfoResponseHandler(userInfoResponseHandler) (5)
.errorResponseHandler(errorResponseHandler) (6)
.userInfoMapper(userInfoMapper) (7)
)
)
);
return http.build();
}
1 | userInfoRequestConverter() : 添加一个 AuthenticationConverter (预处理器),用于尝试将 UserInfo 请求 从 HttpServletRequest 中提取到 OidcUserInfoAuthenticationToken 的实例。 |
2 | userInfoRequestConverters() : 设置 Consumer ,提供对默认和(可选)添加的 AuthenticationConverter 列表的访问权限,允许添加、删除或自定义特定的 AuthenticationConverter 。 |
3 | authenticationProvider() : 添加一个 AuthenticationProvider (主处理器),用于验证 OidcUserInfoAuthenticationToken 。 |
4 | authenticationProviders() : 设置 Consumer ,提供对默认和(可选)添加的 AuthenticationProvider 列表的访问权限,允许添加、删除或自定义特定的 AuthenticationProvider 。 |
5 | userInfoResponseHandler() : 用于处理已验证的 OidcUserInfoAuthenticationToken 并返回 UserInfo 响应 的 AuthenticationSuccessHandler (后处理器)。 |
6 | errorResponseHandler() : 用于处理 OAuth2AuthenticationException 并返回 UserInfo 错误响应 的 AuthenticationFailureHandler (后处理器)。 |
7 | userInfoMapper() : 用于从 OidcUserInfoAuthenticationContext 提取 claim 到 OidcUserInfo 实例的 Function 。 |
OidcUserInfoEndpointConfigurer
配置 OidcUserInfoEndpointFilter
并将其注册到 OAuth2 授权服务器的 SecurityFilterChain
@Bean
中。OidcUserInfoEndpointFilter
是处理 UserInfo 请求 并返回 OidcUserInfo 响应 的 Filter
。
OidcUserInfoEndpointFilter
配置了以下默认值
-
AuthenticationConverter
— 一个内部实现,从SecurityContext
获取Authentication
并使用 principal 创建一个OidcUserInfoAuthenticationToken
。 -
AuthenticationManager
— 由OidcUserInfoAuthenticationProvider
组成的AuthenticationManager
,它与一个内部实现的userInfoMapper
相关联,该userInfoMapper
根据授权期间请求的 scope 从 ID Token 中提取 标准 claim。 -
AuthenticationSuccessHandler
— 一个内部实现,用于处理已验证的OidcUserInfoAuthenticationToken
并返回OidcUserInfo
响应。 -
AuthenticationFailureHandler
— 一个内部实现,使用与OAuth2AuthenticationException
关联的OAuth2Error
并返回OAuth2Error
响应。
您可以通过提供一个 OAuth2TokenCustomizer<JwtEncodingContext> @Bean 来自定义 ID Token。 |
OpenID Connect 1.0 UserInfo 端点是一个 OAuth2 保护的资源,要求在 UserInfo 请求 中发送访问令牌作为 bearer token。
OAuth2 资源服务器支持是自动配置的,但是 OpenID Connect 1.0 UserInfo 端点需要一个 JwtDecoder @Bean 。 |
指南 操作指南:自定义 OpenID Connect 1.0 UserInfo 响应 包含自定义 UserInfo 端点的示例。 |
OpenID Connect 1.0 客户端注册端点
OidcClientRegistrationEndpointConfigurer
提供自定义 OpenID Connect 1.0 客户端注册端点 的能力。它定义了扩展点,允许您自定义 客户端注册请求 或 客户端读取请求 的预处理、主要处理和后处理逻辑。
OidcClientRegistrationEndpointConfigurer
提供以下配置选项
@Bean
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
OAuth2AuthorizationServerConfigurer.authorizationServer();
http
.securityMatcher(authorizationServerConfigurer.getEndpointsMatcher())
.with(authorizationServerConfigurer, (authorizationServer) ->
authorizationServer
.oidc(oidc ->
oidc
.clientRegistrationEndpoint(clientRegistrationEndpoint ->
clientRegistrationEndpoint
.clientRegistrationRequestConverter(clientRegistrationRequestConverter) (1)
.clientRegistrationRequestConverters(clientRegistrationRequestConvertersConsumers) (2)
.authenticationProvider(authenticationProvider) (3)
.authenticationProviders(authenticationProvidersConsumer) (4)
.clientRegistrationResponseHandler(clientRegistrationResponseHandler) (5)
.errorResponseHandler(errorResponseHandler) (6)
)
)
);
return http.build();
}
1 | clientRegistrationRequestConverter() : 添加一个 AuthenticationConverter (预处理器),用于尝试从 HttpServletRequest 中提取 客户端注册请求 或 客户端读取请求 到 OidcClientRegistrationAuthenticationToken 的实例。 |
2 | clientRegistrationRequestConverters() : 设置 Consumer ,提供对默认和(可选)添加的 AuthenticationConverter 列表的访问权限,允许添加、删除或自定义特定的 AuthenticationConverter 。 |
3 | authenticationProvider() : 添加一个 AuthenticationProvider (主处理器),用于验证 OidcClientRegistrationAuthenticationToken 。 |
4 | authenticationProviders() : 设置 Consumer ,提供对默认和(可选)添加的 AuthenticationProvider 列表的访问权限,允许添加、删除或自定义特定的 AuthenticationProvider 。 |
5 | clientRegistrationResponseHandler() : 用于处理“已认证”的 OidcClientRegistrationAuthenticationToken 并返回客户端注册响应或客户端读取响应的 AuthenticationSuccessHandler (后处理器)。 |
6 | errorResponseHandler() : 用于处理 OAuth2AuthenticationException 并返回客户端注册错误响应或客户端读取错误响应的 AuthenticationFailureHandler (后处理器)。 |
OpenID Connect 1.0 客户端注册端点默认是禁用的,因为许多部署不需要动态客户端注册。 |
OidcClientRegistrationEndpointConfigurer
配置 OidcClientRegistrationEndpointFilter
并将其注册到 OAuth2 授权服务器的 SecurityFilterChain
@Bean
中。OidcClientRegistrationEndpointFilter
是处理客户端注册请求并返回OidcClientRegistration
响应的 Filter
。
OidcClientRegistrationEndpointFilter 也处理客户端读取请求并返回OidcClientRegistration 响应。 |
OidcClientRegistrationEndpointFilter
使用以下默认值进行配置
-
AuthenticationConverter
— 一个OidcClientRegistrationAuthenticationConverter
。 -
AuthenticationManager
— 一个由OidcClientRegistrationAuthenticationProvider
和OidcClientConfigurationAuthenticationProvider
组成的AuthenticationManager
。 -
AuthenticationSuccessHandler
— 一个处理“已认证”的OidcClientRegistrationAuthenticationToken
并返回OidcClientRegistration
响应的内部实现。 -
AuthenticationFailureHandler
— 一个内部实现,使用与OAuth2AuthenticationException
关联的OAuth2Error
并返回OAuth2Error
响应。
OpenID Connect 1.0 客户端注册端点是一个OAuth2 受保护资源,它**要求**在客户端注册(或客户端读取)请求中以持有者令牌的形式发送访问令牌。
OAuth2 资源服务器支持是自动配置的,但是,OpenID Connect 1.0 客户端注册端点**需要**一个 JwtDecoder @Bean 。 |
客户端注册请求中的访问令牌**要求** OAuth2 范围是 client.create 。 |
客户端读取请求中的访问令牌**要求** OAuth2 范围是 client.read 。 |