Spring 授权服务器核心协议端点
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring 授权服务器核心协议端点相关的知识,希望对你有一定的参考价值。
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)
.authorizationRequestConverters(authorizationRequestConvertersConsumer)
.authenticationProvider(authenticationProvider)
.authenticationProviders(authenticationProvidersConsumer)
.authorizationResponseHandler(authorizationResponseHandler)
.errorResponseHandler(errorResponseHandler)
.consentPage("/oauth2/v1/authorize")
);
return http.build();
|
|
|
|
|
|
|
OAuth2AuthorizationEndpointConfigurer
配置并使用 OAuth2 授权 server.is 处理 OAuth2 授权请求(和同意)进行注册。OAuth2AuthorizationEndpointFilter
SecurityFilterChain
@Bean
OAuth2AuthorizationEndpointFilter
Filter
OAuth2AuthorizationEndpointFilter
配置了以下默认值:
-
AuthenticationConverter
——由和组成。DelegatingAuthenticationConverter
OAuth2AuthorizationCodeRequestAuthenticationConverter
OAuth2AuthorizationConsentAuthenticationConverter
-
AuthenticationManager
——安安。AuthenticationManager
OAuth2AuthorizationCodeRequestAuthenticationProvider
OAuth2AuthorizationConsentAuthenticationProvider
-
AuthenticationSuccessHandler
— 处理“经过身份验证”并返回的内部实现。OAuth2AuthorizationCodeRequestAuthenticationToken
OAuth2AuthorizationResponse
-
AuthenticationFailureHandler
— 使用与 the和 关联的内部实现 返回响应。OAuth2Error
OAuth2AuthorizationCodeRequestAuthenticationException
OAuth2Error
自定义授权请求验证
OAuth2AuthorizationCodeRequestAuthenticationValidator
是用于验证授权代码授予中使用的特定 OAuth2 授权请求参数的默认验证器。 默认实现验证 and参数。 如果验证失败,则引发 anis。redirect_uri
scope
OAuth2AuthorizationCodeRequestAuthenticationException
OAuth2AuthorizationCodeRequestAuthenticationProvider
提供通过提供 typeto 的自定义身份验证验证程序来覆盖默认授权请求验证的功能。Consumer<OAuth2AuthorizationCodeRequestAuthenticationContext>
setAuthenticationValidator()
|
如果验证失败,身份验证验证程序必须抛出。 |
在开发生命周期阶段的一个常见用例是允许参数。localhost
redirect_uri
以下示例演示如何使用允许参数的自定义身份验证验证程序进行配置:OAuth2AuthorizationCodeRequestAuthenticationProvider
localhost
redirect_uri
@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 令牌端点
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)
.accessTokenRequestConverters(accessTokenRequestConvertersConsumer)
.authenticationProvider(authenticationProvider)
.authenticationProviders(authenticationProvidersConsumer)
.accessTokenResponseHandler(accessTokenResponseHandler)
.errorResponseHandler(errorResponseHandler)
);
return http.build();
|
|
|
|
|
|
OAuth2TokenEndpointConfigurer
配置并使用 OAuth2 授权 server.is 处理 OAuth2 访问令牌请求进行注册。OAuth2TokenEndpointFilter
SecurityFilterChain
@Bean
OAuth2TokenEndpointFilter
Filter
支持的授权授权类型包括、和。authorization_code
refresh_token
client_credentials
OAuth2TokenEndpointFilter
配置了以下默认值:
-
AuthenticationConverter
— 由、和组成。DelegatingAuthenticationConverter
OAuth2AuthorizationCodeAuthenticationConverter
OAuth2RefreshTokenAuthenticationConverter
OAuth2ClientCredentialsAuthenticationConverter
-
AuthenticationManager
— 由、、和组成。AuthenticationManager
OAuth2AuthorizationCodeAuthenticationProvider
OAuth2RefreshTokenAuthenticationProvider
OAuth2ClientCredentialsAuthenticationProvider
-
AuthenticationSuccessHandler
— 处理 anand 返回的内部实现。OAuth2AccessTokenAuthenticationToken
OAuth2AccessTokenResponse
-
AuthenticationFailureHandler
— 使用与 the和 关联的内部实现 返回响应。OAuth2Error
OAuth2AuthenticationException
OAuth2Error
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)
.introspectionRequestConverters(introspectionRequestConvertersConsumer)
.authenticationProvider(authenticationProvider)
.authenticationProviders(authenticationProvidersConsumer)
.introspectionResponseHandler(introspectionResponseHandler)
.errorResponseHandler(errorResponseHandler)
);
return http.build();
|
|
|
|
|
|
OAuth2TokenIntrospectionEndpointConfigurer
配置并使用 OAuth2 授权 server.is 处理 OAuth2 自检请求。OAuth2TokenIntrospectionEndpointFilter
SecurityFilterChain
@Bean
OAuth2TokenIntrospectionEndpointFilter
Filter
OAuth2TokenIntrospectionEndpointFilter
配置了以下默认值:
-
AuthenticationConverter
— 安.OAuth2TokenIntrospectionAuthenticationConverter
-
AuthenticationManager
——由。AuthenticationManager
OAuth2TokenIntrospectionAuthenticationProvider
-
AuthenticationSuccessHandler
— 处理“经过身份验证”并返回响应的内部实现。OAuth2TokenIntrospectionAuthenticationToken
OAuth2TokenIntrospection
-
AuthenticationFailureHandler
— 使用与 the和 关联的内部实现 返回响应。OAuth2Error
OAuth2AuthenticationException
OAuth2Error
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)
.revocationRequestConverters(revocationRequestConvertersConsumer)
.authenticationProvider(authenticationProvider)
.authenticationProviders(authenticationProvidersConsumer)
.revocationResponseHandler(revocationResponseHandler)
.errorResponseHandler(errorResponseHandler)
);
return http.build();
| |
| |
| |
| |
| |
|
OAuth2TokenRevocationEndpointConfigurer
配置并使用 OAuth2 授权 server.is 处理 OAuth2 吊销请求进行注册。OAuth2TokenRevocationEndpointFilter
SecurityFilterChain
@Bean
OAuth2TokenRevocationEndpointFilter
Filter
OAuth2TokenRevocationEndpointFilter
配置了以下默认值:
-
AuthenticationConverter
— 安.OAuth2TokenRevocationAuthenticationConverter
-
AuthenticationManager
——由。AuthenticationManager
OAuth2TokenRevocationAuthenticationProvider
-
AuthenticationSuccessHandler
— 处理“经过身份验证”并返回 OAuth2 吊销响应的内部实现。OAuth2TokenRevocationAuthenticationToken
-
AuthenticationFailureHandler
— 使用与 the和 关联的内部实现 返回响应。OAuth2Error
OAuth2AuthenticationException
OAuth2Error
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));
return http.build();
|
OAuth2AuthorizationServerMetadataEndpointConfigurer
配置并使用返回OAuth2AuthorizationServerMetadata 响应的 OAuth2 授权 server.is 注册它。OAuth2AuthorizationServerMetadataEndpointFilter
SecurityFilterChain
@Bean
OAuth2AuthorizationServerMetadataEndpointFilter
Filter
JWK 设置终结点
OAuth2AuthorizationServerConfigurer
提供对JWK 集终结点的支持。
OAuth2AuthorizationServerConfigurer配置并使用返回JWK 集的 OAuth2 授权 server.is 注册它。NimbusJwkSetEndpointFilter
SecurityFilterChain
@Bean
NimbusJwkSetEndpointFilter
Filter
仅当 ais 已注册时,才会配置 JWK Set 终结点。 |
OpenID Connect 1.0 提供程序配置终结点
OidcProviderConfigurationEndpointConfigurer
提供自定义OpenID Connect 1.0 提供程序配置终结点的功能。 它定义了一个扩展点
以上是关于Spring 授权服务器核心协议端点的主要内容,如果未能解决你的问题,请参考以下文章
SpringCloud使用Spring Cloud OAuth2保护微服务系统