协议端点
OAuth2 授权端点
OAuth2AuthorizationEndpointConfigurer 提供了定制 OAuth2 授权端点 的能力。它定义了扩展点,让您可以定制 OAuth2 授权请求 的预处理、主处理和后处理逻辑。
OAuth2AuthorizationEndpointConfigurer 提供了以下配置选项:
@Bean
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
new OAuth2AuthorizationServerConfigurer();
http.apply(authorizationServerConfigurer);
authorizationServerConfigurer
.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(预处理器),用于尝试从 HttpServletRequest 中提取 OAuth2 授权请求(或同意)并转换为 OAuth2AuthorizationCodeRequestAuthenticationToken 或 OAuth2AuthorizationConsentAuthenticationToken 实例。 |
| 2 | authorizationRequestConverters(): 设置提供访问默认和(可选)添加的 AuthenticationConverter 列表的 Consumer,允许添加、移除或自定义特定的 AuthenticationConverter。 |
| 3 | authenticationProvider(): 添加一个 AuthenticationProvider(主处理器),用于对 OAuth2AuthorizationCodeRequestAuthenticationToken 或 OAuth2AuthorizationConsentAuthenticationToken 进行身份验证。 |
| 4 | authenticationProviders(): 设置提供访问默认和(可选)添加的 AuthenticationProvider 列表的 Consumer,允许添加、移除或自定义特定的 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 是用于验证授权码授权中使用的特定 OAuth2 授权请求参数的默认验证器。默认实现验证 redirect_uri 和 scope 参数。如果验证失败,将抛出一个 OAuth2AuthorizationCodeRequestAuthenticationException。
OAuth2AuthorizationCodeRequestAuthenticationProvider 提供了通过提供类型为 Consumer<OAuth2AuthorizationCodeRequestAuthenticationContext> 的自定义身份验证器来覆盖默认授权请求验证的能力,方法是调用 setAuthenticationValidator()。
OAuth2AuthorizationCodeRequestAuthenticationContext 包含 OAuth2AuthorizationCodeRequestAuthenticationToken,其中包含 OAuth2 授权请求参数。 |
如果验证失败,身份验证器 必须 抛出 OAuth2AuthorizationCodeRequestAuthenticationException。 |
在开发生命周期阶段的常见用例是允许在 redirect_uri 参数中使用 localhost。
以下示例展示了如何配置 OAuth2AuthorizationCodeRequestAuthenticationProvider,使用允许在 redirect_uri 参数中使用 localhost 的自定义身份验证器:
@Bean
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
new OAuth2AuthorizationServerConfigurer();
http.apply(authorizationServerConfigurer);
authorizationServerConfigurer
.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 =
// 覆盖默认的 redirect_uri 验证器
new CustomRedirectUriValidator()
// 重用默认的 scope 验证器
.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();
// 在比较客户端重定向 URI 与预注册 URI 时使用精确字符串匹配
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 =
new OAuth2AuthorizationServerConfigurer();
http.apply(authorizationServerConfigurer);
authorizationServerConfigurer
.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(预处理器),用于尝试从 HttpServletRequest 中提取一个 OAuth2 设备授权请求 到一个 OAuth2DeviceAuthorizationRequestAuthenticationToken 实例。 |
| 2 | deviceAuthorizationRequestConverters(): 设置提供访问默认和(可选)添加的 AuthenticationConverter 列表的 Consumer,允许添加、删除或自定义特定的 AuthenticationConverter。 |
| 3 | authenticationProvider(): 添加一个 AuthenticationProvider(主处理器),用于对 OAuth2DeviceAuthorizationRequestAuthenticationToken 进行身份验证。 |
| 4 | authenticationProviders(): 设置提供访问默认和(可选)添加的 AuthenticationProvider 列表的 Consumer,允许添加、删除或自定义特定的 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 =
new OAuth2AuthorizationServerConfigurer();
http.apply(authorizationServerConfigurer);
authorizationServerConfigurer
.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(预处理器),用于尝试从HttpServletRequest中提取一个OAuth2设备验证请求(或同意)到OAuth2DeviceVerificationAuthenticationToken或OAuth2DeviceAuthorizationConsentAuthenticationToken的实例。 |
| 2 | deviceVerificationRequestConverters(): 设置提供访问默认和(可选)添加的AuthenticationConverter列表的Consumer,允许添加、删除或自定义特定的AuthenticationConverter。 |
| 3 | authenticationProvider(): 添加一个AuthenticationProvider(主处理器),用于对OAuth2DeviceVerificationAuthenticationToken或OAuth2DeviceAuthorizationConsentAuthenticationToken进行身份验证。 |
| 4 | authenticationProviders(): 设置提供访问默认和(可选)添加的AuthenticationProvider列表的Consumer,允许添加、删除或自定义特定的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— 处理“经过身份验证”的OAuth2DeviceVerificationAuthenticationToken并将用户重定向到成功页面(/?success)的SimpleUrlAuthenticationSuccessHandler。 -
AuthenticationFailureHandler— 使用与OAuth2AuthenticationException相关联的OAuth2Error并返回OAuth2Error响应的内部实现。
OAuth2令牌端点
OAuth2TokenEndpointConfigurer提供了自定义OAuth2令牌端点的能力。它定义了扩展点,让您可以自定义OAuth2访问令牌请求的预处理、主处理和后处理逻辑。
OAuth2TokenEndpointConfigurer提供了以下配置选项:
@Bean
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
new OAuth2AuthorizationServerConfigurer();
http.apply(authorizationServerConfigurer);
authorizationServerConfigurer
.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(预处理器),用于尝试从HttpServletRequest中提取一个OAuth2访问令牌请求到OAuth2AuthorizationGrantAuthenticationToken的实例。 |
| 2 | accessTokenRequestConverters():设置提供访问默认和(可选)添加的AuthenticationConverter列表的Consumer,允许添加、删除或自定义特定的AuthenticationConverter。 |
| 3 | authenticationProvider():添加一个AuthenticationProvider(主处理器),用于对OAuth2AuthorizationGrantAuthenticationToken进行身份验证。 |
| 4 | authenticationProviders():设置提供访问默认和(可选)添加的AuthenticationProvider列表的Consumer,允许添加、删除或自定义特定的AuthenticationProvider。 |
| 5 | accessTokenResponseHandler():用于处理OAuth2AccessTokenAuthenticationToken并返回OAuth2AccessTokenResponse的AuthenticationSuccessHandler(后处理器)。 |
| 6 | errorResponseHandler():用于处理OAuth2AuthenticationException并返回OAuth2错误响应的AuthenticationFailureHandler(后处理器)。 |
OAuth2TokenEndpointConfigurer配置了OAuth2TokenEndpointFilter并将其注册到OAuth2授权服务器SecurityFilterChain的@Bean中。 OAuth2TokenEndpointFilter是处理OAuth2访问令牌请求的Filter。
支持的授权授予类型包括authorization_code、refresh_token、client_credentials和urn:ietf:params:oauth:grant-type:device_code。
OAuth2TokenEndpointFilter配置了以下默认值:
-
AuthenticationConverter— 由OAuth2AuthorizationCodeAuthenticationConverter、OAuth2RefreshTokenAuthenticationConverter、OAuth2ClientCredentialsAuthenticationConverter和OAuth2DeviceCodeAuthenticationConverter组成的DelegatingAuthenticationConverter。 -
AuthenticationManager— 由OAuth2AuthorizationCodeAuthenticationProvider、OAuth2RefreshTokenAuthenticationProvider、OAuth2ClientCredentialsAuthenticationProvider和OAuth2DeviceCodeAuthenticationProvider组成的AuthenticationManager。 -
AuthenticationSuccessHandler— 一个内部实现,处理OAuth2AccessTokenAuthenticationToken并返回OAuth2AccessTokenResponse。 -
AuthenticationFailureHandler— 一个OAuth2ErrorAuthenticationFailureHandler。
OAuth2令牌内省端点
OAuth2TokenIntrospectionEndpointConfigurer提供了自定义OAuth2令牌内省端点的能力。它定义了扩展点,让您可以自定义OAuth2内省请求的预处理、主处理和后处理逻辑。
OAuth2TokenIntrospectionEndpointConfigurer提供了以下配置选项:
@Bean
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
new OAuth2AuthorizationServerConfigurer();
http.apply(authorizationServerConfigurer);
authorizationServerConfigurer
.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(预处理器),用于尝试从HttpServletRequest中提取OAuth2内省请求并转换为OAuth2TokenIntrospectionAuthenticationToken实例。 |
| 2 | introspectionRequestConverters():设置提供访问默认和(可选)添加的AuthenticationConverter列表的Consumer,允许添加、删除或自定义特定的AuthenticationConverter。 |
| 3 | authenticationProvider():添加一个AuthenticationProvider(主处理器),用于对OAuth2TokenIntrospectionAuthenticationToken进行身份验证。 |
| 4 | authenticationProviders():设置提供访问默认和(可选)添加的AuthenticationProvider列表的Consumer,允许添加、删除或自定义特定的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令牌撤销端点
OAuth2TokenRevocationEndpointConfigurer提供了自定义OAuth2令牌撤销端点的能力。它定义了扩展点,让您可以自定义OAuth2撤销请求的预处理、主处理和后处理逻辑。
OAuth2TokenRevocationEndpointConfigurer提供了以下配置选项:
@Bean
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
new OAuth2AuthorizationServerConfigurer();
http.apply(authorizationServerConfigurer);
authorizationServerConfigurer
.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(预处理器),用于尝试从HttpServletRequest中提取OAuth2撤销请求并转换为OAuth2TokenRevocationAuthenticationToken实例。 |
| 2 | revocationRequestConverters():设置提供访问默认和(可选)添加的AuthenticationConverter列表的Consumer,允许添加、删除或自定义特定的AuthenticationConverter。 |
| 3 | authenticationProvider():添加一个AuthenticationProvider(主处理器),用于对OAuth2TokenRevocationAuthenticationToken进行身份验证。 |
| 4 | authenticationProviders():设置提供访问默认和(可选)添加的AuthenticationProvider列表的Consumer,允许添加、删除或自定义特定的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 =
new OAuth2AuthorizationServerConfigurer();
http.apply(authorizationServerConfigurer);
authorizationServerConfigurer
.authorizationServerMetadataEndpoint(authorizationServerMetadataEndpoint ->
authorizationServerMetadataEndpoint
.authorizationServerMetadataCustomizer(authorizationServerMetadataCustomizer)); (1)
return http.build();
}
| 1 | authorizationServerMetadataCustomizer():提供访问OAuth2AuthorizationServerMetadata.Builder的Consumer,允许自定义授权服务器配置的声明。 |
OAuth2AuthorizationServerMetadataEndpointConfigurer配置了OAuth2AuthorizationServerMetadataEndpointFilter并将其注册到OAuth2授权服务器的SecurityFilterChain @Bean中。OAuth2AuthorizationServerMetadataEndpointFilter是返回OAuth2AuthorizationServerMetadata响应的Filter。
OpenID Connect 1.0提供者配置端点
OidcProviderConfigurationEndpointConfigurer提供了自定义OpenID Connect 1.0提供者配置端点的能力。它定义了一个扩展点,让您可以自定义OpenID提供者配置响应。
OidcProviderConfigurationEndpointConfigurer提供了以下配置选项:
@Bean
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
new OAuth2AuthorizationServerConfigurer();
http.apply(authorizationServerConfigurer);
authorizationServerConfigurer
.oidc(oidc ->
oidc
.providerConfigurationEndpoint(providerConfigurationEndpoint ->
providerConfigurationEndpoint
.providerConfigurationCustomizer(providerConfigurationCustomizer) (1)
)
);
return http.build();
}
| 1 | providerConfigurationCustomizer():提供访问OidcProviderConfiguration.Builder的Consumer,允许自定义OpenID提供者配置的声明。 |
OidcProviderConfigurationEndpointConfigurer配置了OidcProviderConfigurationEndpointFilter并将其注册到OAuth2授权服务器的SecurityFilterChain @Bean中。OidcProviderConfigurationEndpointFilter是返回OidcProviderConfiguration响应的Filter。
OpenID Connect 1.0 注销端点
OidcLogoutEndpointConfigurer提供了定制OpenID Connect 1.0 注销端点的能力。它定义了扩展点,让您可以定制RP-Initiated注销请求的预处理、主处理和后处理逻辑。
OidcLogoutEndpointConfigurer提供了以下配置选项:
@Bean
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
new OAuth2AuthorizationServerConfigurer();
http.apply(authorizationServerConfigurer);
authorizationServerConfigurer
.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():设置提供访问默认和(可选)添加的AuthenticationConverter列表的Consumer,允许添加、删除或自定义特定的AuthenticationConverter。 |
| 3 | authenticationProvider():添加一个AuthenticationProvider(主处理器),用于对OidcLogoutAuthenticationToken进行身份验证。 |
| 4 | authenticationProviders():设置提供访问默认和(可选)添加的AuthenticationProvider列表的Consumer,允许添加、删除或自定义特定的AuthenticationProvider。 |
| 5 | logoutResponseHandler():用于处理“已认证”OidcLogoutAuthenticationToken并执行注销的AuthenticationSuccessHandler(后处理器)。 |
| 6 | errorResponseHandler():用于处理OAuth2AuthenticationException并返回错误响应的AuthenticationFailureHandler(后处理器)。 |
OidcLogoutEndpointConfigurer配置了OidcLogoutEndpointFilter并将其注册到OAuth2授权服务器的SecurityFilterChain @Bean中。OidcLogoutEndpointFilter是处理RP-Initiated注销请求并执行用户注销的Filter。
OidcLogoutEndpointFilter配置了以下默认值:
-
AuthenticationConverter— 一个OidcLogoutAuthenticationConverter。 -
AuthenticationManager— 由OidcLogoutAuthenticationProvider组成的AuthenticationManager。 -
AuthenticationSuccessHandler— 一个内部实现,处理“已认证”OidcLogoutAuthenticationToken并执行注销。 -
AuthenticationFailureHandler— 一个内部实现,使用与OAuth2AuthenticationException相关联的OAuth2Error并返回OAuth2Error响应。
OidcLogoutAuthenticationProvider使用一个SessionRegistry来查找与请求注销的用户关联的SessionInformation实例。 |
OidcClientInitiatedLogoutSuccessHandler是Spring Security OAuth2客户端支持中配置OpenID Connect 1.0 RP-Initiated注销的对应配置。 |
OpenID Connect 1.0 用户信息端点
OidcUserInfoEndpointConfigurer 提供了自定义OpenID Connect 1.0 用户信息端点的能力。它定义了扩展点,让您可以自定义用户信息请求的预处理、主处理和后处理逻辑。
OidcUserInfoEndpointConfigurer 提供了以下配置选项:
@Bean
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
new OAuth2AuthorizationServerConfigurer();
http.apply(authorizationServerConfigurer);
authorizationServerConfigurer
.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():在尝试从HttpServletRequest中提取OidcUserInfoAuthenticationToken实例时使用的AuthenticationConverter(预处理器)。 |
| 2 | userInfoRequestConverters():设置提供对默认和(可选)添加的AuthenticationConverter列表的访问权限的Consumer,允许添加、删除或自定义特定的AuthenticationConverter。 |
| 3 | authenticationProvider():用于对OidcUserInfoAuthenticationToken进行身份验证的AuthenticationProvider(主处理器)。 |
| 4 | authenticationProviders():设置提供对默认和(可选)添加的AuthenticationProvider列表的访问权限的Consumer,允许添加、删除或自定义特定的AuthenticationProvider。 |
| 5 | userInfoResponseHandler():用于处理“已验证”OidcUserInfoAuthenticationToken并返回UserInfo响应的AuthenticationSuccessHandler(后处理器)。 |
| 6 | errorResponseHandler():用于处理OAuth2AuthenticationException并返回UserInfo错误响应的AuthenticationFailureHandler(后处理器)。 |
| 7 | userInfoMapper():用于从OidcUserInfoAuthenticationContext中提取声明到OidcUserInfo实例的Function。 |
OidcUserInfoEndpointConfigurer配置了OidcUserInfoEndpointFilter并将其注册到OAuth2授权服务器SecurityFilterChain的@Bean中。OidcUserInfoEndpointFilter是处理用户信息请求并返回OidcUserInfo响应的Filter。
OidcUserInfoEndpointFilter配置了以下默认值:
-
AuthenticationConverter— 一个内部实现,从SecurityContext获取Authentication并使用主体创建OidcUserInfoAuthenticationToken。 -
AuthenticationManager— 由OidcUserInfoAuthenticationProvider组成的AuthenticationManager,与内部实现的userInfoMapper相关联,根据授权期间请求的请求的范围从ID Token中提取标准声明。 -
AuthenticationSuccessHandler— 一个内部实现,处理“已验证”OidcUserInfoAuthenticationToken并返回OidcUserInfo响应。 -
AuthenticationFailureHandler— 一个内部实现,使用与OAuth2AuthenticationException相关联的OAuth2Error并返回OAuth2Error响应。
您可以通过提供一个OAuth2TokenCustomizer<JwtEncodingContext> @Bean来自定义ID Token。 |
OpenID Connect 1.0 用户信息端点是一个受OAuth2保护的资源,需要将访问令牌作为持有者令牌发送到用户信息请求中。以下示例显示如何启用OAuth2资源服务器配置:
@Bean
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
new OAuth2AuthorizationServerConfigurer();
http.apply(authorizationServerConfigurer);
...
http.oauth2ResourceServer(resourceServer -> resourceServer.jwt(Customizer.withDefaults()));
return http.build();
}
@Bean
public JwtDecoder jwtDecoder(JWKSource<SecurityContext> jwkSource) {
return OAuth2AuthorizationServerConfiguration.jwtDecoder(jwkSource);
}
OpenID Connect 1.0 用户信息端点需要一个JwtDecoder @Bean。 |
| 指南如何:自定义OpenID Connect 1.0用户信息响应包含了自定义用户信息端点的示例。 |
OpenID Connect 1.0客户端注册端点
OidcClientRegistrationEndpointConfigurer提供了定制OpenID Connect 1.0客户端注册端点的能力。它定义了扩展点,让您可以定制客户端注册请求或客户端读取请求的预处理、主处理和后处理逻辑。
OidcClientRegistrationEndpointConfigurer提供以下配置选项:
@Bean
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
new OAuth2AuthorizationServerConfigurer();
http.apply(authorizationServerConfigurer);
authorizationServerConfigurer
.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():设置提供访问默认和(可选)添加的AuthenticationConverter列表的Consumer,允许添加、删除或自定义特定的AuthenticationConverter。 |
| 3 | authenticationProvider():添加一个AuthenticationProvider(主处理器),用于对OidcClientRegistrationAuthenticationToken进行身份验证。 |
| 4 | authenticationProviders():设置提供访问默认和(可选)添加的AuthenticationProvider列表的Consumer,允许添加、删除或自定义特定的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范围client.create。 |
客户端读取请求中的访问令牌需要OAuth2范围client.read。 |
以下示例显示了如何启用OAuth2资源服务器配置:
@Bean
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
new OAuth2AuthorizationServerConfigurer();
http.apply(authorizationServerConfigurer);
...
http.oauth2ResourceServer(resourceServer -> resourceServer.jwt(Customizer.withDefaults()));
return http.build();
}
@Bean
public JwtDecoder jwtDecoder(JWKSource<SecurityContext> jwkSource) {
return OAuth2AuthorizationServerConfiguration.jwtDecoder(jwkSource);
}
OpenID Connect 1.0客户端注册端点需要一个JwtDecoder @Bean。 |