此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Authorization Server 1.5.0! |
协议端点
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 提供对List of default 和 (可选) addedAuthenticationConverter 允许添加、删除或自定义特定AuthenticationConverter . |
3 | authenticationProvider() :添加AuthenticationProvider (主处理器)用于验证OAuth2AuthorizationCodeRequestAuthenticationToken 或OAuth2AuthorizationConsentAuthenticationToken . |
4 | authenticationProviders() :设置Consumer 提供对List of default 和 (可选) addedAuthenticationProvider 允许添加、删除或自定义特定AuthenticationProvider . |
5 | authorizationResponseHandler() :这AuthenticationSuccessHandler (后处理器)用于处理 “authenticated”OAuth2AuthorizationCodeRequestAuthenticationToken 并返回 OAuth2AuthorizationResponse。 |
6 | errorResponseHandler() :这AuthenticationFailureHandler (后处理器)用于处理OAuth2AuthorizationCodeRequestAuthenticationException 并返回 OAuth2Error 响应。 |
7 | consentPage() :这URI ,以便在授权请求流程中将资源所有者重定向到是否需要同意。 |
OAuth2AuthorizationEndpointConfigurer
配置OAuth2AuthorizationEndpointFilter
并将其注册到 OAuth2 授权服务器SecurityFilterChain
@Bean
.OAuth2AuthorizationEndpointFilter
是Filter
处理 OAuth2 授权请求(和同意)。
OAuth2AuthorizationEndpointFilter
配置了以下默认值:
-
AuthenticationConverter
— 一个DelegatingAuthenticationConverter
组成OAuth2AuthorizationCodeRequestAuthenticationConverter
和OAuth2AuthorizationConsentAuthenticationConverter
. -
AuthenticationManager
— 一个AuthenticationManager
组成OAuth2AuthorizationCodeRequestAuthenticationProvider
和OAuth2AuthorizationConsentAuthenticationProvider
. -
AuthenticationSuccessHandler
— 处理 “authenticated” 的内部实现OAuth2AuthorizationCodeRequestAuthenticationToken
并返回OAuth2AuthorizationResponse
. -
AuthenticationFailureHandler
— 使用OAuth2Error
与OAuth2AuthorizationCodeRequestAuthenticationException
并返回OAuth2Error
响应。
自定义授权请求验证
OAuth2AuthorizationCodeRequestAuthenticationValidator
是用于验证授权码授予中使用的特定 OAuth2 授权请求参数的默认验证器。
默认实现会验证redirect_uri
和scope
参数。
如果验证失败,则OAuth2AuthorizationCodeRequestAuthenticationException
被抛出。
OAuth2AuthorizationCodeRequestAuthenticationProvider
通过提供Consumer<OAuth2AuthorizationCodeRequestAuthenticationContext>
自setAuthenticationValidator()
.
OAuth2AuthorizationCodeRequestAuthenticationContext 持有OAuth2AuthorizationCodeRequestAuthenticationToken ,其中包含 OAuth2 授权请求参数。 |
如果验证失败,身份验证验证器必须抛出OAuth2AuthorizationCodeRequestAuthenticationException . |
在开发生命周期阶段,一个常见的用例是允许localhost
在redirect_uri
参数。
以下示例显示如何配置OAuth2AuthorizationCodeRequestAuthenticationProvider
使用自定义身份验证验证器,允许localhost
在redirect_uri
参数:
@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 推送的授权请求端点
OAuth2PushedAuthorizationRequestEndpointConfigurer
提供自定义 OAuth2 Push Authorization Request 端点的功能。
它定义了扩展点,允许您自定义 OAuth2 推送授权请求的预处理、主处理和后处理逻辑。
OAuth2PushedAuthorizationRequestEndpointConfigurer
提供以下配置选项:
@Bean
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
OAuth2AuthorizationServerConfigurer.authorizationServer();
http
.securityMatcher(authorizationServerConfigurer.getEndpointsMatcher())
.with(authorizationServerConfigurer, (authorizationServer) ->
authorizationServer
.pushedAuthorizationRequestEndpoint(pushedAuthorizationRequestEndpoint ->
pushedAuthorizationRequestEndpoint
.pushedAuthorizationRequestConverter(pushedAuthorizationRequestConverter) (1)
.pushedAuthorizationRequestConverters(pushedAuthorizationRequestConvertersConsumer) (2)
.authenticationProvider(authenticationProvider) (3)
.authenticationProviders(authenticationProvidersConsumer) (4)
.pushedAuthorizationResponseHandler(pushedAuthorizationResponseHandler) (5)
.errorResponseHandler(errorResponseHandler) (6)
)
);
return http.build();
}
1 | pushedAuthorizationRequestConverter() :添加AuthenticationConverter (预处理器)尝试从 中提取 OAuth2 推送的授权请求时使用HttpServletRequest 添加到OAuth2PushedAuthorizationRequestAuthenticationToken . |
2 | pushedAuthorizationRequestConverters() :设置Consumer 提供对List of default 和 (可选) addedAuthenticationConverter 允许添加、删除或自定义特定AuthenticationConverter . |
3 | authenticationProvider() :添加AuthenticationProvider (主处理器)用于验证OAuth2PushedAuthorizationRequestAuthenticationToken . |
4 | authenticationProviders() :设置Consumer 提供对List of default 和 (可选) addedAuthenticationProvider 允许添加、删除或自定义特定AuthenticationProvider . |
5 | pushedAuthorizationResponseHandler() :这AuthenticationSuccessHandler (后处理器)用于处理 “authenticated”OAuth2PushedAuthorizationRequestAuthenticationToken 并返回 OAuth2 推送的授权响应。 |
6 | errorResponseHandler() :这AuthenticationFailureHandler (后处理器)用于处理OAuth2AuthenticationException 并返回 OAuth2Error 响应。 |
OAuth2PushedAuthorizationRequestEndpointConfigurer
配置OAuth2PushedAuthorizationRequestEndpointFilter
并将其注册到 OAuth2 授权服务器SecurityFilterChain
@Bean
.OAuth2PushedAuthorizationRequestEndpointFilter
是Filter
处理 OAuth2 推送的授权请求。
OAuth2PushedAuthorizationRequestEndpointFilter
配置了以下默认值:
-
AuthenticationConverter
— 一个DelegatingAuthenticationConverter
组成OAuth2AuthorizationCodeRequestAuthenticationConverter
. -
AuthenticationManager
— 一个AuthenticationManager
组成OAuth2PushedAuthorizationRequestAuthenticationProvider
. -
AuthenticationSuccessHandler
— 处理 “authenticated” 的内部实现OAuth2PushedAuthorizationRequestAuthenticationToken
并返回 OAuth2 推送的授权响应。 -
AuthenticationFailureHandler
— 一个OAuth2ErrorAuthenticationFailureHandler
.
自定义推送的授权请求验证
OAuth2AuthorizationCodeRequestAuthenticationValidator
是默认验证器,用于验证授权码授予中使用的特定 OAuth2 推送授权请求参数。
默认实现会验证redirect_uri
和scope
参数。
如果验证失败,则OAuth2AuthorizationCodeRequestAuthenticationException
被抛出。
OAuth2PushedAuthorizationRequestAuthenticationProvider
通过提供Consumer<OAuth2AuthorizationCodeRequestAuthenticationContext>
自setAuthenticationValidator()
.
OAuth2AuthorizationCodeRequestAuthenticationContext 持有OAuth2AuthorizationCodeRequestAuthenticationToken ,其中包含 OAuth2 推送的授权请求参数。 |
如果验证失败,身份验证验证器必须抛出OAuth2AuthorizationCodeRequestAuthenticationException . |
在开发生命周期阶段,一个常见的用例是允许localhost
在redirect_uri
参数。
以下示例显示如何配置OAuth2PushedAuthorizationRequestAuthenticationProvider
使用自定义身份验证验证器,允许localhost
在redirect_uri
参数:
@Bean
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
OAuth2AuthorizationServerConfigurer.authorizationServer();
http
.securityMatcher(authorizationServerConfigurer.getEndpointsMatcher())
.with(authorizationServerConfigurer, (authorizationServer) ->
authorizationServer
.pushedAuthorizationRequestEndpoint(pushedAuthorizationRequestEndpoint ->
pushedAuthorizationRequestEndpoint
.authenticationProviders(configureAuthenticationValidator())
)
);
return http.build();
}
private Consumer<List<AuthenticationProvider>> configureAuthenticationValidator() {
return (authenticationProviders) ->
authenticationProviders.forEach((authenticationProvider) -> {
if (authenticationProvider instanceof OAuth2PushedAuthorizationRequestAuthenticationProvider) {
Consumer<OAuth2AuthorizationCodeRequestAuthenticationContext> authenticationValidator =
// Override default redirect_uri validator
new CustomRedirectUriValidator()
// Reuse default scope validator
.andThen(OAuth2AuthorizationCodeRequestAuthenticationValidator.DEFAULT_SCOPE_VALIDATOR);
((OAuth2PushedAuthorizationRequestAuthenticationProvider) 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 提供对List of default 和 (可选) addedAuthenticationConverter 允许添加、删除或自定义特定AuthenticationConverter . |
3 | authenticationProvider() :添加AuthenticationProvider (主处理器)用于验证OAuth2DeviceAuthorizationRequestAuthenticationToken . |
4 | authenticationProviders() :设置Consumer 提供对List of default 和 (可选) addedAuthenticationProvider 允许添加、删除或自定义特定AuthenticationProvider . |
5 | deviceAuthorizationResponseHandler() :这AuthenticationSuccessHandler (后处理器)用于处理 “authenticated”OAuth2DeviceAuthorizationRequestAuthenticationToken 并返回 OAuth2DeviceAuthorizationResponse。 |
6 | errorResponseHandler() :这AuthenticationFailureHandler (后处理器)用于处理OAuth2AuthenticationException 并返回 OAuth2Error 响应。 |
7 | verificationUri() :这URI 将资源所有者定向到辅助设备上。 |
OAuth2DeviceAuthorizationEndpointConfigurer
配置OAuth2DeviceAuthorizationEndpointFilter
并将其注册到 OAuth2 授权服务器SecurityFilterChain
@Bean
.OAuth2DeviceAuthorizationEndpointFilter
是Filter
处理 OAuth2 设备授权请求。
OAuth2DeviceAuthorizationEndpointFilter
配置了以下默认值:
-
AuthenticationConverter
— 一个OAuth2DeviceAuthorizationRequestAuthenticationConverter
. -
AuthenticationManager
— 一个AuthenticationManager
组成OAuth2DeviceAuthorizationRequestAuthenticationProvider
. -
AuthenticationSuccessHandler
— 处理 “authenticated” 的内部实现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 提供对List of default 和 (可选) addedAuthenticationConverter 允许添加、删除或自定义特定AuthenticationConverter . |
3 | authenticationProvider() :添加AuthenticationProvider (主处理器)用于验证OAuth2DeviceVerificationAuthenticationToken 或OAuth2DeviceAuthorizationConsentAuthenticationToken . |
4 | authenticationProviders() :设置Consumer 提供对List of default 和 (可选) addedAuthenticationProvider 允许添加、删除或自定义特定AuthenticationProvider . |
5 | deviceVerificationResponseHandler() :这AuthenticationSuccessHandler (后处理器)用于处理 “authenticated”OAuth2DeviceVerificationAuthenticationToken 并指示资源所有者返回到其设备。 |
6 | errorResponseHandler() :这AuthenticationFailureHandler (后处理器)用于处理OAuth2AuthenticationException 并返回错误响应。 |
7 | consentPage() :这URI ,将资源所有者重定向到在设备验证请求流程中是否需要同意。 |
OAuth2DeviceVerificationEndpointConfigurer
配置OAuth2DeviceVerificationEndpointFilter
并将其注册到 OAuth2 授权服务器SecurityFilterChain
@Bean
.OAuth2DeviceVerificationEndpointFilter
是Filter
处理 OAuth2 设备验证请求(和同意)。
OAuth2DeviceVerificationEndpointFilter
配置了以下默认值:
-
AuthenticationConverter
— 一个DelegatingAuthenticationConverter
组成OAuth2DeviceVerificationAuthenticationConverter
和OAuth2DeviceAuthorizationConsentAuthenticationConverter
. -
AuthenticationManager
— 一个AuthenticationManager
组成OAuth2DeviceVerificationAuthenticationProvider
和OAuth2DeviceAuthorizationConsentAuthenticationProvider
. -
AuthenticationSuccessHandler
— 一个SimpleUrlAuthenticationSuccessHandler
处理 “authenticated”OAuth2DeviceVerificationAuthenticationToken
并将用户重定向到成功页面 (/?success
). -
AuthenticationFailureHandler
— 使用OAuth2Error
与OAuth2AuthenticationException
并返回OAuth2Error
响应。
OAuth2 Tokens端点
OAuth2TokenEndpointConfigurer
提供自定义 OAuth2 Tokens端点的功能。
它定义了扩展点,允许您自定义 OAuth2 访问Tokens请求的预处理、主处理和后处理逻辑。
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 访问Tokens请求时使用HttpServletRequest 添加到OAuth2AuthorizationGrantAuthenticationToken . |
2 | accessTokenRequestConverters() :设置Consumer 提供对List of default 和 (可选) addedAuthenticationConverter 允许添加、删除或自定义特定AuthenticationConverter . |
3 | authenticationProvider() :添加AuthenticationProvider (主处理器)用于验证OAuth2AuthorizationGrantAuthenticationToken . |
4 | authenticationProviders() :设置Consumer 提供对List of default 和 (可选) addedAuthenticationProvider 允许添加、删除或自定义特定AuthenticationProvider . |
5 | accessTokenResponseHandler() :这AuthenticationSuccessHandler (后处理器)用于处理OAuth2AccessTokenAuthenticationToken 并返回OAuth2AccessTokenResponse . |
6 | errorResponseHandler() :这AuthenticationFailureHandler (后处理器)用于处理OAuth2AuthenticationException 并返回 OAuth2Error 响应。 |
OAuth2TokenEndpointConfigurer
配置OAuth2TokenEndpointFilter
并将其注册到 OAuth2 授权服务器SecurityFilterChain
@Bean
.OAuth2TokenEndpointFilter
是Filter
处理 OAuth2 访问Tokens请求。
支持的授权授权类型包括authorization_code
,refresh_token
,client_credentials
,urn:ietf:params:oauth:grant-type:device_code
和urn:ietf:params:oauth:grant-type:token-exchange
.
OAuth2TokenEndpointFilter
配置了以下默认值:
-
AuthenticationConverter
— 一个DelegatingAuthenticationConverter
组成OAuth2AuthorizationCodeAuthenticationConverter
,OAuth2RefreshTokenAuthenticationConverter
,OAuth2ClientCredentialsAuthenticationConverter
,OAuth2DeviceCodeAuthenticationConverter
和OAuth2TokenExchangeAuthenticationConverter
. -
AuthenticationManager
— 一个AuthenticationManager
组成OAuth2AuthorizationCodeAuthenticationProvider
,OAuth2RefreshTokenAuthenticationProvider
,OAuth2ClientCredentialsAuthenticationProvider
,OAuth2DeviceCodeAuthenticationProvider
和OAuth2TokenExchangeAuthenticationProvider
. -
AuthenticationSuccessHandler
— 一个OAuth2AccessTokenResponseAuthenticationSuccessHandler
. -
AuthenticationFailureHandler
— 一个OAuth2ErrorAuthenticationFailureHandler
.
自定义客户端身份凭证授权请求验证
OAuth2ClientCredentialsAuthenticationValidator
是用于验证特定 OAuth2 客户端凭证授予请求参数的默认验证器。
默认实现会验证scope
参数。
如果验证失败,则OAuth2AuthenticationException
被抛出。
OAuth2ClientCredentialsAuthenticationProvider
通过提供 type 为Consumer<OAuth2ClientCredentialsAuthenticationContext>
自setAuthenticationValidator()
.
OAuth2ClientCredentialsAuthenticationContext 持有OAuth2ClientCredentialsAuthenticationToken ,其中包含 OAuth2 客户端凭证授予请求参数。 |
如果验证失败,身份验证验证器必须抛出OAuth2AuthenticationException . |
以下示例显示如何配置OAuth2ClientCredentialsAuthenticationProvider
替换为覆盖默认scope
验证:
@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
}
}
DPoP 绑定访问Tokens
RFC 9449 OAuth 2.0 演示所有权证明 (DPoP) 是一种应用程序级机制,用于对访问Tokens进行发送者约束。
DPoP 的主要目标是防止未经授权或非法的客户端使用泄露或被盗的访问Tokens,方法是在授权服务器颁发访问Tokens时将访问Tokens绑定到公钥,并要求客户端在资源服务器上使用访问Tokens时证明拥有相应的私钥。
通过 DPoP 受发送方约束的访问Tokens与典型的不记名Tokens形成鲜明对比,后者可由拥有访问Tokens的任何客户端使用。
DPoP 引入了 DPoP 证明的概念,它是由客户端创建并作为 HTTP 请求中的标头发送的 JWT。 客户端使用 DPoP 证明来证明拥有与某个公钥对应的私钥。
当客户端发起访问Tokens请求时,它会在 HTTP 标头中将 DPoP 证明附加到请求。 授权服务器将访问Tokens绑定(发送者约束)到 DPoP 证明中关联的公钥。
当客户端发起受保护资源请求时,它会再次将 DPoP 证明附加到 HTTP 标头中的请求。
资源服务器直接在访问Tokens (JWT) 中或通过 OAuth2 Tokens自省端点获取有关绑定到访问Tokens的公钥的信息。 然后,资源服务器验证绑定到访问Tokens的公钥是否与 DPoP 证明中的公钥匹配。 它还验证 DPoP 证明中的访问Tokens哈希是否与请求中的访问Tokens匹配。
DPoP 访问Tokens请求
要请求使用 DPoP 绑定到公钥的访问Tokens,客户端必须在DPoP
标头。
这适用于所有访问Tokens请求,无论授权授权类型如何(例如authorization_code
,refresh_token
,client_credentials
等)。
以下 HTTP 请求显示了authorization_code
访问Tokens请求,并在DPoP
页眉:
POST /oauth2/token HTTP/1.1
Host: server.example.com
Content-Type: application/x-www-form-urlencoded
DPoP: eyJraWQiOiJyc2EtandrLWtpZCIsInR5cCI6ImRwb3Arand0IiwiYWxnIjoiUlMyNTYiLCJqd2siOnsia3R5IjoiUlNBIiwiZSI6IkFRQUIiLCJraWQiOiJyc2EtandrLWtpZCIsIm4iOiIzRmxxSnI1VFJza0lRSWdkRTNEZDdEOWxib1dkY1RVVDhhLWZKUjdNQXZRbTdYWE5vWWttM3Y3TVFMMU5ZdER2TDJsOENBbmMwV2RTVElOVTZJUnZjNUtxbzJRNGNzTlg5U0hPbUVmem9ST2pRcWFoRWN2ZTFqQlhsdW9DWGRZdVlweDRfMXRmUmdHNmlpNFVoeGg2aUk4cU5NSlFYLWZMZnFoYmZZZnhCUVZSUHl3QmtBYklQNHgxRUFzYkM2RlNObWtoQ3hpTU5xRWd4YUlwWThDMmtKZEpfWklWLVdXNG5vRGR6cEtxSGN3bUI4RnNydW1sVllfRE5WdlVTRElpcGlxOVBiUDRIOTlUWE4xbzc0Nm9SYU5hMDdycTFob0NnTVNTeS04NVNhZ0NveGxteUUtRC1vZjlTc01ZOE9sOXQwcmR6cG9iQnVoeUpfbzVkZnZqS3cifX0.eyJodG0iOiJQT1NUIiwiaHR1IjoiaHR0cHM6Ly9zZXJ2ZXIuZXhhbXBsZS5jb20vb2F1dGgyL3Rva2VuIiwiaWF0IjoxNzQ2ODA2MzA1LCJqdGkiOiI0YjIzNDBkMi1hOTFmLTQwYTUtYmFhOS1kZDRlNWRlYWM4NjcifQ.wq8gJ_G6vpiEinfaY3WhereqCCLoeJOG8tnWBBAzRWx9F1KU5yAAWq-ZVCk_k07-h6DIqz2wgv6y9dVbNpRYwNwDUeik9qLRsC60M8YW7EFVyI3n_NpujLwzZeub_nDYMVnyn4ii0NaZrYHtoGXOlswQfS_-ET-jpC0XWm5nBZsCdUEXjOYtwaACC6Js-pyNwKmSLp5SKIk11jZUR5xIIopaQy521y9qJHhGRwzj8DQGsP7wMZ98UFL0E--1c-hh4rTy8PMeWCqRHdwjj_ry_eTe0DJFcxxYQdeL7-0_0CIO4Ayx5WHEpcUOIzBRoN32RsNpDZc-5slDNj9ku004DA
grant_type=authorization_code\
&client_id=s6BhdRkqt\
&code=SplxlOBeZQQYbYS6WxSbIA\
&redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb\
&code_verifier=bEaL42izcC-o-xBk0K2vuJ6U-y1p9r_wW2dFWIWgjz-
下面显示了 DPoP Proof JWT 标头和声明的表示形式:
{
"typ": "dpop+jwt",
"alg": "RS256",
"jwk": {
"kty": "RSA",
"e": "AQAB",
"n": "3FlqJr5TRskIQIgdE3Dd7D9lboWdcTUT8a-fJR7MAvQm7XXNoYkm3v7MQL1NYtDvL2l8CAnc0WdSTINU6IRvc5Kqo2Q4csNX9SHOmEfzoROjQqahEcve1jBXluoCXdYuYpx4_1tfRgG6ii4Uhxh6iI8qNMJQX-fLfqhbfYfxBQVRPywBkAbIP4x1EAsbC6FSNmkhCxiMNqEgxaIpY8C2kJdJ_ZIV-WW4noDdzpKqHcwmB8FsrumlVY_DNVvUSDIipiq9PbP4H99TXN1o746oRaNa07rq1hoCgMSSy-85SagCoxlmyE-D-of9SsMY8Ol9t0rdzpobBuhyJ_o5dfvjKw"
}
}
{
"htm": "POST",
"htu": "https://server.example.com/oauth2/token",
"iat": 1746806305,
"jti": "4b2340d2-a91f-40a5-baa9-dd4e5deac867"
}
以下代码显示了如何生成 DPoP 证明 JWT 的示例:
RSAKey rsaKey = ...
JWKSource<SecurityContext> jwkSource = (jwkSelector, securityContext) -> jwkSelector
.select(new JWKSet(rsaKey));
NimbusJwtEncoder jwtEncoder = new NimbusJwtEncoder(jwkSource);
JwsHeader jwsHeader = JwsHeader.with(SignatureAlgorithm.RS256)
.type("dpop+jwt")
.jwk(rsaKey.toPublicJWK().toJSONObject())
.build();
JwtClaimsSet claims = JwtClaimsSet.builder()
.issuedAt(Instant.now())
.claim("htm", "POST")
.claim("htu", "https://server.example.com/oauth2/token")
.id(UUID.randomUUID().toString())
.build();
Jwt dPoPProof = jwtEncoder.encode(JwtEncoderParameters.from(jwsHeader, claims));
授权服务器成功验证 DPoP 证明后,DPoP 证明中的公钥将被绑定(发件人约束)到颁发的访问Tokens。
以下访问Tokens响应显示token_type
parameter 指定为DPoP
向客户端发出信号,表明访问Tokens已绑定到其 DPoP 证明公钥:
HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: no-store
{
"access_token": "Kz~8mXK1EalYznwH-LC-1fBAo.4Ljp~zsPE_NeO.gxU",
"token_type": "DPoP",
"expires_in": 2677
}
公钥确认
资源服务器必须能够识别访问Tokens是否受 DPoP 绑定,并验证是否绑定到 DPoP 证明的公钥。 绑定是通过以资源服务器可以访问的方式将公钥与访问Tokens相关联来实现的,例如将公钥哈希直接嵌入访问Tokens (JWT) 或通过Tokens自省。
当访问Tokens表示为 JWT 时,公钥哈希包含在jkt
确认方法 (cnf
) 声明。
以下示例显示了包含cnf
声明中带有jkt
claim,这是 DPoP 证明公钥的 JWK SHA-256 指纹:
{
"sub":"[email protected]",
"iss":"https://server.example.com",
"nbf":1562262611,
"exp":1562266216,
"cnf":
{
"jkt":"CQMknzRoZ5YUi7vS58jck1q8TmZT8wiIiXrCN1Ny4VU"
}
}
OAuth2 Tokens自省端点
OAuth2TokenIntrospectionEndpointConfigurer
提供自定义 OAuth2 Tokens自检端点的功能。
它定义了扩展点,允许您自定义 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 提供对List of default 和 (可选) addedAuthenticationConverter 允许添加、删除或自定义特定AuthenticationConverter . |
3 | authenticationProvider() :添加AuthenticationProvider (主处理器)用于验证OAuth2TokenIntrospectionAuthenticationToken . |
4 | authenticationProviders() :设置Consumer 提供对List of default 和 (可选) addedAuthenticationProvider 允许添加、删除或自定义特定AuthenticationProvider . |
5 | introspectionResponseHandler() :这AuthenticationSuccessHandler (后处理器)用于处理 “authenticated”OAuth2TokenIntrospectionAuthenticationToken 并返回 OAuth2TokenIntrospection 响应。 |
6 | errorResponseHandler() :这AuthenticationFailureHandler (后处理器)用于处理OAuth2AuthenticationException 并返回 OAuth2Error 响应。 |
OAuth2TokenIntrospectionEndpointConfigurer
配置OAuth2TokenIntrospectionEndpointFilter
并将其注册到 OAuth2 授权服务器SecurityFilterChain
@Bean
.OAuth2TokenIntrospectionEndpointFilter
是Filter
处理 OAuth2 自省请求。
OAuth2TokenIntrospectionEndpointFilter
配置了以下默认值:
-
AuthenticationConverter
— 一个OAuth2TokenIntrospectionAuthenticationConverter
. -
AuthenticationManager
— 一个AuthenticationManager
组成OAuth2TokenIntrospectionAuthenticationProvider
. -
AuthenticationSuccessHandler
— 处理 “authenticated” 的内部实现OAuth2TokenIntrospectionAuthenticationToken
并返回OAuth2TokenIntrospection
响应。 -
AuthenticationFailureHandler
— 一个OAuth2ErrorAuthenticationFailureHandler
.
OAuth2 Tokens吊销端点
OAuth2TokenRevocationEndpointConfigurer
提供自定义 OAuth2 Tokens吊销端点的功能。
它定义了扩展点,允许您自定义 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 提供对List of default 和 (可选) addedAuthenticationConverter 允许添加、删除或自定义特定AuthenticationConverter . |
3 | authenticationProvider() :添加AuthenticationProvider (主处理器)用于验证OAuth2TokenRevocationAuthenticationToken . |
4 | authenticationProviders() :设置Consumer 提供对List of default 和 (可选) addedAuthenticationProvider 允许添加、删除或自定义特定AuthenticationProvider . |
5 | revocationResponseHandler() :这AuthenticationSuccessHandler (后处理器)用于处理 “authenticated”OAuth2TokenRevocationAuthenticationToken 并返回 OAuth2 吊销响应。 |
6 | errorResponseHandler() :这AuthenticationFailureHandler (后处理器)用于处理OAuth2AuthenticationException 并返回 OAuth2Error 响应。 |
OAuth2TokenRevocationEndpointConfigurer
配置OAuth2TokenRevocationEndpointFilter
并将其注册到 OAuth2 授权服务器SecurityFilterChain
@Bean
.OAuth2TokenRevocationEndpointFilter
是Filter
处理 OAuth2 吊销请求。
OAuth2TokenRevocationEndpointFilter
配置了以下默认值:
-
AuthenticationConverter
— 一个OAuth2TokenRevocationAuthenticationConverter
. -
AuthenticationManager
— 一个AuthenticationManager
组成OAuth2TokenRevocationAuthenticationProvider
. -
AuthenticationSuccessHandler
— 处理 “authenticated” 的内部实现OAuth2TokenRevocationAuthenticationToken
并返回 OAuth2 吊销响应。 -
AuthenticationFailureHandler
— 一个OAuth2ErrorAuthenticationFailureHandler
.
OAuth2 授权服务器元数据端点
OAuth2AuthorizationServerMetadataEndpointConfigurer
提供自定义 OAuth2 授权服务器元数据端点的功能。
它定义了一个扩展点,允许您自定义 OAuth2 Authorization Server 元数据响应。
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() :这Consumer 提供对OAuth2AuthorizationServerMetadata.Builder 允许自定义 Authorization Server 配置的声明。 |
OAuth2AuthorizationServerMetadataEndpointConfigurer
配置OAuth2AuthorizationServerMetadataEndpointFilter
并将其注册到 OAuth2 授权服务器SecurityFilterChain
@Bean
.OAuth2AuthorizationServerMetadataEndpointFilter
是Filter
返回 OAuth2AuthorizationServerMetadata 响应。
JWK 设置端点
OAuth2AuthorizationServerConfigurer
提供对 JWK Set 端点的支持。
OAuth2AuthorizationServerConfigurer
配置NimbusJwkSetEndpointFilter
并将其注册到 OAuth2 授权服务器SecurityFilterChain
@Bean
.NimbusJwkSetEndpointFilter
是Filter
,这将返回 JWK 集。
仅当 JWK Set 的JWKSource<SecurityContext> @Bean 已注册。 |
OpenID Connect 1.0 提供者配置终端节点
OidcProviderConfigurationEndpointConfigurer
提供自定义 OpenID Connect 1.0 提供程序配置终端节点的功能。
它定义了一个扩展点,允许您自定义 OpenID Provider Configuration 响应。
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() :这Consumer 提供对OidcProviderConfiguration.Builder 允许自定义 OpenID Provider 配置的声明。 |
OidcProviderConfigurationEndpointConfigurer
配置OidcProviderConfigurationEndpointFilter
并将其注册到 OAuth2 授权服务器SecurityFilterChain
@Bean
.OidcProviderConfigurationEndpointFilter
是Filter
,这将返回 OidcProviderConfiguration 响应。
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 提供对List of default 和 (可选) addedAuthenticationConverter 允许添加、删除或自定义特定AuthenticationConverter . |
3 | authenticationProvider() :添加AuthenticationProvider (主处理器)用于验证OidcLogoutAuthenticationToken . |
4 | authenticationProviders() :设置Consumer 提供对List of default 和 (可选) addedAuthenticationProvider 允许添加、删除或自定义特定AuthenticationProvider . |
5 | logoutResponseHandler() :这AuthenticationSuccessHandler (后处理器)用于处理 “authenticated”OidcLogoutAuthenticationToken 并执行注销。 |
6 | errorResponseHandler() :这AuthenticationFailureHandler (后处理器)用于处理OAuth2AuthenticationException 并返回错误响应。 |
OidcLogoutEndpointConfigurer
配置OidcLogoutEndpointFilter
并将其注册到 OAuth2 授权服务器SecurityFilterChain
@Bean
.OidcLogoutEndpointFilter
是Filter
处理 RP 发起的注销请求并执行最终用户的注销。
OidcLogoutEndpointFilter
配置了以下默认值:
-
AuthenticationConverter
— 一个OidcLogoutAuthenticationConverter
. -
AuthenticationManager
— 一个AuthenticationManager
组成OidcLogoutAuthenticationProvider
. -
AuthenticationSuccessHandler
— 一个OidcLogoutAuthenticationSuccessHandler
. -
AuthenticationFailureHandler
— 使用OAuth2Error
与OAuth2AuthenticationException
并返回OAuth2Error
响应。
OidcLogoutAuthenticationProvider 使用SessionRegistry 要查找SessionInformation 实例。 |
OidcClientInitiatedLogoutSuccessHandler 是 Spring Security 的 OAuth2 客户端支持中用于配置 OpenID Connect 1.0 RP 发起的注销的相应配置。 |
自定义注销请求验证
OidcLogoutAuthenticationValidator
是用于验证特定 OpenID Connect RP 发起的注销请求参数的默认验证程序。
默认实现会验证post_logout_redirect_uri
参数。
如果验证失败,则OAuth2AuthenticationException
被抛出。
OidcLogoutAuthenticationProvider
通过提供Consumer<OidcLogoutAuthenticationContext>
自setAuthenticationValidator()
.
OidcLogoutAuthenticationContext 持有OidcLogoutAuthenticationToken ,其中包含 logout 请求参数。 |
如果验证失败,身份验证验证器必须抛出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 提供对List of default 和 (可选) addedAuthenticationConverter 允许添加、删除或自定义特定AuthenticationConverter . |
3 | authenticationProvider() :添加AuthenticationProvider (主处理器)用于验证OidcUserInfoAuthenticationToken . |
4 | authenticationProviders() :设置Consumer 提供对List of default 和 (可选) addedAuthenticationProvider 允许添加、删除或自定义特定AuthenticationProvider . |
5 | userInfoResponseHandler() :这AuthenticationSuccessHandler (后处理器)用于处理 “authenticated”OidcUserInfoAuthenticationToken 并返回 UserInfo 响应。 |
6 | errorResponseHandler() :这AuthenticationFailureHandler (后处理器)用于处理OAuth2AuthenticationException 并返回 UserInfo Error 响应。 |
7 | userInfoMapper() :这Function 用于从OidcUserInfoAuthenticationContext 添加到OidcUserInfo . |
OidcUserInfoEndpointConfigurer
配置OidcUserInfoEndpointFilter
并将其注册到 OAuth2 授权服务器SecurityFilterChain
@Bean
.OidcUserInfoEndpointFilter
是Filter
处理 UserInfo 请求并返回 OidcUserInfo 响应。
OidcUserInfoEndpointFilter
配置了以下默认值:
-
AuthenticationConverter
— 一个内部实现,用于获取Authentication
从SecurityContext
并创建一个OidcUserInfoAuthenticationToken
与校长。 -
AuthenticationManager
— 一个AuthenticationManager
组成OidcUserInfoAuthenticationProvider
,它与userInfoMapper
它根据授权期间请求的范围从 ID Tokens中提取标准声明。 -
AuthenticationSuccessHandler
— 处理 “authenticated” 的内部实现OidcUserInfoAuthenticationToken
并返回OidcUserInfo
响应。 -
AuthenticationFailureHandler
— 使用OAuth2Error
与OAuth2AuthenticationException
并返回OAuth2Error
响应。
您可以通过提供OAuth2TokenCustomizer<JwtEncodingContext> @Bean . |
OpenID Connect 1.0 UserInfo 终端节点是受 OAuth2 保护的资源,它要求在 UserInfo 请求中将访问Tokens作为持有者Tokens发送。
OAuth2 资源服务器支持是自动配置的,但是,一个JwtDecoder @Bean 对于 OpenID Connect 1.0 UserInfo 终端节点是必需的。 |
指南作方法:自定义 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 提供对List of default 和 (可选) addedAuthenticationConverter 允许添加、删除或自定义特定AuthenticationConverter . |
3 | authenticationProvider() :添加AuthenticationProvider (主处理器)用于验证OidcClientRegistrationAuthenticationToken . |
4 | authenticationProviders() :设置Consumer 提供对List of default 和 (可选) addedAuthenticationProvider 允许添加、删除或自定义特定AuthenticationProvider . |
5 | clientRegistrationResponseHandler() :这AuthenticationSuccessHandler (后处理器)用于处理 “authenticated”OidcClientRegistrationAuthenticationToken 并返回 Client Registration 响应或 Client Read 响应。 |
6 | errorResponseHandler() :这AuthenticationFailureHandler (后处理器)用于处理OAuth2AuthenticationException 并返回 Client Registration Error 响应或 Client Read Error 响应。 |
默认情况下,OpenID Connect 1.0 客户端注册终端节点处于禁用状态,因为许多部署不需要动态客户端注册。 |
OidcClientRegistrationEndpointConfigurer
配置OidcClientRegistrationEndpointFilter
并将其注册到 OAuth2 授权服务器SecurityFilterChain
@Bean
.OidcClientRegistrationEndpointFilter
是Filter
处理客户端注册请求并返回 OidcClientRegistration 响应。
OidcClientRegistrationEndpointFilter 还会处理 Client Read 请求并返回 OidcClientRegistration 响应。 |
OidcClientRegistrationEndpointFilter
配置了以下默认值:
-
AuthenticationConverter
— 一个OidcClientRegistrationAuthenticationConverter
. -
AuthenticationManager
— 一个AuthenticationManager
组成OidcClientRegistrationAuthenticationProvider
和OidcClientConfigurationAuthenticationProvider
. -
AuthenticationSuccessHandler
— 处理 “authenticated” 的内部实现OidcClientRegistrationAuthenticationToken
并返回OidcClientRegistration
响应。 -
AuthenticationFailureHandler
— 使用OAuth2Error
与OAuth2AuthenticationException
并返回OAuth2Error
响应。
OpenID Connect 1.0 客户端注册终端节点是受 OAuth2 保护的资源,它要求在客户端注册(或客户端读取)请求中将访问Tokens作为持有者Tokens发送。
OAuth2 资源服务器支持是自动配置的,但是,一个JwtDecoder @Bean 对于 OpenID Connect 1.0 客户端注册终端节点是必需的。 |
客户端注册请求中的访问Tokens需要 OAuth2 范围client.create . |
客户端读取请求中的访问Tokens需要 OAuth2 范围client.read . |