| 本站(springdoc.cn)中的内容来源于 spring.io ,原始版权归属于 spring.io。由 springboot.io - Spring Boot中文社区 进行翻译,整理。可供个人学习、研究,未经许可,不得进行任何转载、商用或与之相关的行为。 商标声明:Spring 是 Pivotal Software, Inc. 在美国以及其他国家的商标。 |
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授权请求(或consent)到 OAuth2AuthorizationCodeRequestAuthenticationToken 或 OAuth2AuthorizationConsentAuthenticationToken 的实例时使用。 |
| 2 | authorizationRequestConverters(): 设置 Consumer,提供对默认和(可选)添加的 AuthenticationConverter List 的访问,允许添加、删除或定制特定的 AuthenticationConverter 的能力。 |
| 3 | authenticationProvider(): 添加一个 AuthenticationProvider(主处理器),用于验证 OAuth2AuthorizationCodeRequestAuthenticationToken 或 OAuth2AuthorizationConsentAuthenticationToken。 |
| 4 | authenticationProviders(): 设置 Consumer,提供对默认和(可选)添加的 AuthenticationProvider List 的访问,允许添加、删除或定制特定的 AuthenticationProvider。 |
| 5 | authorizationResponseHandler(): AuthenticationSuccessHandler(后处理器),用于处理 "已认证" 的 OAuth2AuthorizationCodeRequestAuthenticationToken 并返回 OAuth2AuthorizationResponse。 |
| 6 | errorResponseHandler(): AuthenticationFailureHandler(后处理器),用于处理 OAuth2AuthorizationCodeRequestAuthenticationException,并返回 OAuth2Error 响应。 |
| 7 | consentPage(): 自定义同意(consent)页面的 URI,如果在授权请求流程中需要同意,则将资源所有者重定向至此。 |
OAuth2AuthorizationEndpointConfigurer 配置 OAuth2AuthorizationEndpointFilter,并将其与 OAuth2 授权服务器 SecurityFilterChain @Bean 注册。OAuth2AuthorizationEndpointFilter 是处理OAuth2 授权请求(和consent)的filter。
OAuth2AuthorizationEndpointFilter 配置的默认值如下。
-
AuthenticationConverter— 一个由OAuth2AuthorizationCodeRequestAuthenticationConverter和OAuth2AuthorizationConsentAuthenticationConverter组成的DelegatingAuthenticationConverter。 -
AuthenticationManager— 一个由OAuth2AuthorizationCodeRequestAuthenticationProvider和OAuth2AuthorizationConsentAuthenticationProvider组成的AuthenticationManager。 -
AuthenticationSuccessHandler— 一个内部实现,处理一个 "已认证" 的OAuth2AuthorizationCodeRequestAuthenticationToken并返回OAuth2AuthorizationResponse。 -
AuthenticationFailureHandler— 一个内部实现,使用与OAuth2AuthorizationCodeRequestAuthenticationException相关的OAuth2Error,并返回OAuth2Error响应。
自定义授权请求验证
OAuth2AuthorizationCodeRequestAuthenticationValidator 是默认的验证器,用于验证授权码授予中使用的特定OAuth2授权请求参数。默认的实现是验证 redirect_uri 和 scope 参数。如果验证失败,会抛出一个 OAuth2AuthorizationCodeRequestAuthenticationException。
OAuth2AuthorizationCodeRequestAuthenticationProvider 为 setAuthenticationValidator() 提供了覆盖默认授权请求验证的能力,即提供一个类型为 Consumer<OAuth2AuthorizationCodeRequestAuthenticationContext> 的自定义验证器。
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 =
// 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 Token 端点
OAuth2TokenEndpointConfigurer 提供了定制 OAuth2 Token 端点 的能力。它定义了扩展点,让你自定义 OAuth2 access token 请求 的预处理、主处理和后处理逻辑。
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 access token 请求 到 OAuth2AuthorizationGrantAuthenticationToken 的实例时使用。 |
| 2 | accessTokenRequestConverters(): 设置 Consumer,提供对默认和(可选)添加的 AuthenticationConverter List 的访问,允许添加、删除或定制特定的 AuthenticationConverter 的能力。 |
| 3 | authenticationProvider(): 添加一个用于认证 OAuth2AuthorizationGrantAuthenticationToken 的 AuthenticationProvider(主处理器)。 |
| 4 | authenticationProviders(): 设置 Consumer,提供对默认和(可选)添加的 AuthenticationProvider List 的访问,允许添加、删除或定制特定的 AuthenticationProvider。 |
| 5 | accessTokenResponseHandler(): 用于处理 OAuth2AccessTokenAuthenticationToken 并返回 OAuth2AccessTokenResponse 的 AuthenticationSuccessHandler(后处理器)。 |
| 6 | errorResponseHandler(): AuthenticationFailureHandler(后处理程序),用于处理 OAuth2AuthenticationException 并返回 OAuth2Error 响应。 |
OAuth2TokenEndpointConfigurer 配置 OAuth2TokenEndpointFilter 并将其注册到OAuth2授权服务器 SecurityFilterChain @Bean。OAuth2TokenEndpointFilter 是处理OAuth2 access token 请求的 Filter。
支持的 授权许可类型(authorization grant type)有 authorization_code、refresh_token 和 client_credentials。
OAuth2TokenEndpointFilter 配置的默认值如下。
-
AuthenticationConverter— 由OAuth2AuthorizationCodeAuthenticationConverter、OAuth2RefreshTokenAuthenticationConverter和OAuth2ClientCredentialsAuthenticationConverter组成的DelegatingAuthenticationConverter。 -
AuthenticationManager— 一个由OAuth2AuthorizationCodeAuthenticationProvider、OAuth2RefreshTokenAuthenticationProvider和OAuth2ClientCredentialsAuthenticationProvider组成的AuthenticationManager。 -
AuthenticationSuccessHandler— 一个内部实现,处理OAuth2AccessTokenAuthenticationToken并返回OAuth2AccessTokenResponse。 -
AuthenticationFailureHandler— 一个内部实现,使用与OAuth2AuthenticationException相关的OAuth2Error,并返回OAuth2Error响应。
OAuth2 Token 内省(Introspection)端点
OAuth2TokenIntrospectionEndpointConfigurer 提供了定制 OAuth2 Token Introspection 端点 的能力。它定义了一些扩展点,让你自定义 OAuth2 introspection 请求 的预处理、主处理和后处理逻辑。
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 introspection 请求 到 OAuth2TokenIntrospectionAuthenticationToken 的实例时使用。 |
| 2 | introspectionRequestConverters(): 设置 Consumer,提供对默认和(可选)添加的 AuthenticationConverter List 的访问,允许添加、删除或定制特定的 AuthenticationConverter 的能力。 |
| 3 | authenticationProvider(): 添加一个用于认证 OAuth2TokenIntrospectionAuthenticationToken 的 AuthenticationProvider(主处理器)。 |
| 4 | authenticationProviders(): 设置 Consumer,提供对默认和(可选)添加的 AuthenticationProvider List 的访问,允许添加、删除或定制特定的 AuthenticationProvider。 |
| 5 | introspectionResponseHandler(): AuthenticationSuccessHandler(后处理器),用于处理 "已认证" 的 OAuth2TokenIntrospectionAuthenticationToken 并返回 OAuth2TokenIntrospection 响应。 |
| 6 | errorResponseHandler(): AuthenticationFailureHandler(后处理程序),用于处理 OAuth2AuthenticationException 并返回 OAuth2Error 响应。 |
OAuth2TokenIntrospectionEndpointConfigurer 配置 OAuth2TokenIntrospectionEndpointFilter,并将其注册到OAuth2授权服务器 SecurityFilterChain @Bean。OAuth2TokenIntrospectionEndpointFilter 是处理OAuth2内省(introspection)请求的 Filter。
OAuth2TokenIntrospectionEndpointFilter 配置的默认值如下。
-
AuthenticationConverter— 一个OAuth2TokenIntrospectionAuthenticationConverter。 -
AuthenticationManager— 一个由OAuth2TokenIntrospectionAuthenticationProvider组成的AuthenticationManager。 -
AuthenticationSuccessHandler— 一个内部实现,处理一个 "已认证" 的OAuth2TokenIntrospectionAuthenticationToken,并返回OAuth2TokenIntrospection响应。 -
AuthenticationFailureHandler— 一个内部实现,使用与OAuth2AuthenticationException相关的OAuth2Error,并返回OAuth2Error响应。
OAuth2 Token 撤销(Revocation)端点
OAuth2TokenRevocationEndpointConfigurer 提供了定制 OAuth2 Token Revocation 端点 的能力。它定义了一些扩展点,让你自定义 OAuth2 Revocation 请求 的预处理、主处理和后处理逻辑。
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 revocation 请求 到 OAuth2TokenRevocationAuthenticationToken 的实例时使用。 |
| 2 | revocationRequestConverters(): 设置 Consumer ,提供对默认和(可选)添加的 AuthenticationConverter List 的访问,允许添加、删除或定制特定的 AuthenticationConverter 的能力。 |
| 3 | authenticationProvider(): 添加一个用于认证 OAuth2TokenRevocationAuthenticationToken 的`AuthenticationProvider`(主处理器)。 |
| 4 | authenticationProviders(): 设置 Consumer,提供对默认和(可选)添加的 AuthenticationProvider List 的访问,允许添加、删除或定制特定的 AuthenticationProvider。 |
| 5 | revocationResponseHandler(): AuthenticationSuccessHandler(后处理器),用于处理 "已认证" 的 OAuth2TokenRevocationAuthenticationToken,并返回 OAuth2 revocation 响应。 |
| 6 | errorResponseHandler(): AuthenticationFailureHandler(后处理程序),用于处理 OAuth2AuthenticationException 并返回 OAuth2Error 响应。 |
OAuth2TokenRevocationEndpointConfigurer 配置 OAuth2TokenRevocationEndpointFilter,并将其注册到OAuth2授权服务器 SecurityFilterChain @Bean。OAuth2TokenRevocationEndpointFilter 是处理OAuth2 revocation 请求的 Filter。
OAuth2TokenRevocationEndpointFilter 配置的默认值如下。
-
AuthenticationConverter— 一个OAuth2TokenRevocationAuthenticationConverter。 -
AuthenticationManager— 一个由OAuth2TokenRevocationAuthenticationProvider组成的AuthenticationManager。 -
AuthenticationSuccessHandler— 一个内部实现,处理一个 "已认证" 的OAuth2TokenRevocationAuthenticationToken,并返回OAuth2 revocation 响应。 -
AuthenticationFailureHandler— 一个内部实现,使用与OAuth2AuthenticationException相关的OAuth2Error,并返回OAuth2Error响应。
OAuth2 授权服务器元数据(Metadata)端点
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,允许自定义授权服务器的配置 claim 的能力。 |
OAuth2AuthorizationServerMetadataEndpointConfigurer 配置 OAuth2AuthorizationServerMetadataEndpointFilter,并将其与 OAuth2授权服务器 SecurityFilterChain @Bean 注册。 OAuth2AuthorizationServerMetadataEndpointFilter 是返回 OAuth2AuthorizationServerMetadata 响应 的 Filter。
JWK Set 端点
OAuth2AuthorizationServerConfigurer 为 provides support for the 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 =
new OAuth2AuthorizationServerConfigurer();
http.apply(authorizationServerConfigurer);
authorizationServerConfigurer
.oidc(oidc ->
oidc
.providerConfigurationEndpoint(providerConfigurationEndpoint ->
providerConfigurationEndpoint
.providerConfigurationCustomizer(providerConfigurationCustomizer) (1)
)
);
return http.build();
}
| 1 | providerConfigurationCustomizer(): Consumer 提供对 OidcProviderConfiguration.Builder 的访问,允许自定义 OpenID Provider 的配置 claim 的能力。 |
OidcProviderConfigurationEndpointConfigurer 配置 OidcProviderConfigurationEndpointFilter,并将其与OAuth2授权服务器 SecurityFilterChain @Bean 注册。OidcProviderConfigurationEndpointFilter 是返回 OidcProviderConfiguration 响应 的 Filter。
OpenID Connect 1.0 UserInfo 端点
OidcUserInfoEndpointConfigurer 提供了定制 OpenID Connect 1.0 UserInfo 端点 的能力。它定义了一些扩展点,让你可以定制 UserInfo 请求的预处理、主要处理和后处理逻辑。
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(): 添加一个 AuthenticationConverter(预处理器),当试图从 HttpServletRequest 中提取 UserInfo 请求到 OidcUserInfoAuthenticationToken 的一个实例时使用。 |
| 2 | userInfoRequestConverters(): 设置 Consumer,提供对默认和(可选)添加的 AuthenticationConverter List 的访问,允许添加、删除或定制特定的 AuthenticationConverter 的能力。 |
| 3 | authenticationProvider(): 添加一个用于认证 OidcUserInfoAuthenticationToken 的 AuthenticationProvider(主处理器)。 |
| 4 | authenticationProviders(): 设置 Consumer,提供对默认和(可选)添加的 AuthenticationProvider List 的访问,允许添加、删除或定制特定的 AuthenticationProvider。 |
| 5 | userInfoResponseHandler(): AuthenticationSuccessHandler(后处理器),用于处理 "已认证" 的 OidcUserInfoAuthenticationToken 并返回 UserInfo 响应。 |
| 6 | errorResponseHandler():
AuthenticationFailureHandler(后处理器),用于处理 OAuth2AuthenticationException 并返回 UserInfo Error响应。 |
| 7 | userInfoMapper(): 用于从 OidcUserInfoAuthenticationContext 向 OidcUserInfo 的实例提取 claim 的 Function。 |
OidcUserInfoEndpointConfigurer 配置 OidcUserInfoEndpointFilter,并将其与OAuth2授权服务器 SecurityFilterChain @Bean 注册。 OidcUserInfoEndpointFilter 是处理 UserInfo 请求 并返回 OidcUserInfo 响应 的 Filter。
OidcUserInfoEndpointFilter 被配置为以下默认值。
-
AuthenticationConverter— 一个内部实现,它从SecurityContext获取Authentication,并使用 principal 创建OidcUserInfoAuthenticationToken。 -
AuthenticationManager— 一个由OidcUserInfoAuthenticationProvider组成的AuthenticationManager,它与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 发送一个 access token。下面的例子显示了如何启用OAuth2资源服务器配置。
@Bean
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
new OAuth2AuthorizationServerConfigurer();
http.apply(authorizationServerConfigurer);
...
http.oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt);
return http.build();
}
@Bean
public JwtDecoder jwtDecoder(JWKSource<SecurityContext> jwkSource) {
return OAuth2AuthorizationServerConfiguration.jwtDecoder(jwkSource);
}
对于 OpenID Connect 1.0 UserInfo 端点来说,JwtDecoder @Bean 是必须的。
|
| 指南 How-to:定制 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 =
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(): 设置 Consumer,提供对默认和(可选)添加的 AuthenticationConverter List 的访问,允许添加、删除或定制特定的 AuthenticationConverter 的能力。 |
| 3 | authenticationProvider(): 添加一个用于认证 OidcClientRegistrationAuthenticationToken 的 AuthenticationProvider(主处理器)。 |
| 4 | authenticationProviders(): 设置 Consumer,提供对默认和(可选)添加的 AuthenticationProvider List 的访问,允许添加、删除或定制特定的 AuthenticationProvider。 |
| 5 | clientRegistrationResponseHandler(): AuthenticationSuccessHandler(后处理器)用于处理 "已认证" 的 OidcClientRegistrationAuthenticationToken 并返回 客户端注册响应 或 客户端读取响应。 |
| 6 | errorResponseHandler(): AuthenticationFailureHandler(后处理器),用于处理 OAuth2AuthenticationException 并返回 客户端注册错误响应 或 客户端读取错误响应。 |
| OpenID Connect 1.0客户端注册端点默认是禁用的,因为许多部署不需要动态客户端注册。 |
OidcClientRegistrationEndpointConfigurer 配置 OidcClientRegistrationEndpointFilter 并将其注册到OAuth2授权服务器 SecurityFilterChain @Bean。OidcClientRegistrationEndpointFilter 是处理 客户端注册请求 并返回 OidcClientRegistration 响应 的 Filter。
OidcClientRegistrationEndpointFilter also processes Client Read requests and returns the OidcClientRegistration response.
|
OidcClientRegistrationEndpointFilter 配置的默认值如下。
-
AuthenticationConverter— 一个OidcClientRegistrationAuthenticationConverter。 -
AuthenticationManager— 一个由OidcClientRegistrationAuthenticationProvider和OidcClientConfigurationAuthenticationProvider组成的AuthenticationManager。 -
AuthenticationSuccessHandler— 一个内部实现,处理一个 "已认证" 的OidcClientRegistrationAuthenticationToken,并返回OidcClientRegistration响应。 -
AuthenticationFailureHandler— 一个内部实现,使用与OAuth2AuthenticationException相关的OAuth2Error,并返回OAuth2Error响应。
OpenID Connect 1.0 客户端注册端点是一个受 OAuth2保护的资源,需要在客户端注册(或客户端读取)请求中发送一个 access token 作为 bearer token。
客户端注册请求中的 access token 必须是 OAuth2 scope client.create。
|
客户端读取请求中的 access token 必须是 OAuth2 scope client.read。
|
下面的例子显示了如何启用OAuth2资源服务器配置。
@Bean
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
new OAuth2AuthorizationServerConfigurer();
http.apply(authorizationServerConfigurer);
...
http.oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt);
return http.build();
}
@Bean
public JwtDecoder jwtDecoder(JWKSource<SecurityContext> jwkSource) {
return OAuth2AuthorizationServerConfiguration.jwtDecoder(jwkSource);
}
JwtDecoder @Bean 是 OpenID Connect 1.0 客户端注册端点的必要条件。
|