Protocol Endpoints

OAuth2 Authorization Endpoint

`OAuth2AuthorizationEndpointConfigurer`提供自定义 OAuth2 Authorization endpoint的能力。它定义了扩展点,这些扩展点让您能够自定义 OAuth2 authorization requests的预处理、主处理和后处理逻辑。

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():当尝试从 HttpServletRequest 提取 OAuth2 authorization request(或同意)到 OAuth2AuthorizationCodeRequestAuthenticationTokenOAuth2AuthorizationConsentAuthenticationToken 的实例时使用的添加的 AuthenticationConverterpre-processor)。
2 authorizationRequestConverters(): 设置`Consumer`,提供对默认`List`和(可选)添加的`AuthenticationConverter’s allowing the ability to add, remove, or customize a specific `AuthenticationConverter`的访问。
3 authenticationProvider(): 添加一个 AuthenticationProvider (main processor),用于对 OAuth2AuthorizationCodeRequestAuthenticationTokenOAuth2AuthorizationConsentAuthenticationToken 进行身份验证。
4 authenticationProviders():设置一个 Consumer,提供对默认和(可选)添加的 AuthenticationProvider’s allowing the ability to add, remove, or customize a specific `AuthenticationProviderList 的访问。
5 authorizationResponseHandler()AuthenticationSuccessHandlerpost-processor)用于处理 “authenticated” OAuth2AuthorizationCodeRequestAuthenticationToken 并返回 OAuth2AuthorizationResponse
6 errorResponseHandler()AuthenticationFailureHandlerpost-processor)用于处理 OAuth2AuthorizationCodeRequestAuthenticationException 并返回 OAuth2Error response
7 consentPage():资源所有者在授权请求流程期间需要同意时重定向到自定义同意页面的 URI

OAuth2AuthorizationEndpointConfigurer 配置 OAuth2AuthorizationEndpointFilter 并将其注册到 OAuth2 授权服务器 SecurityFilterChain @BeanOAuth2AuthorizationEndpointFilter 是负责处理 OAuth2 授权请求(以及同意)的 Filter

OAuth2AuthorizationEndpointFilter 使用以下默认值进行配置:

  • AuthenticationConverter — 一个由 DelegatingAuthenticationConverterOAuth2AuthorizationCodeRequestAuthenticationConverter 组成的 OAuth2AuthorizationConsentAuthenticationConverter

  • AuthenticationManager — 一个由 AuthenticationManagerOAuth2AuthorizationCodeRequestAuthenticationProvider 组成的 OAuth2AuthorizationConsentAuthenticationProvider

  • AuthenticationSuccessHandler — 一个处理 “authenticated” OAuth2AuthorizationCodeRequestAuthenticationToken 并返回 OAuth2AuthorizationResponse 的内部实现。

  • AuthenticationFailureHandler — 一个使用与 OAuth2AuthorizationCodeRequestAuthenticationException 关联的 OAuth2Error 并返回 OAuth2Error 响应的内部实现。

Customizing Authorization Request Validation

OAuth2AuthorizationCodeRequestAuthenticationValidator 是用于验证授权码授予中使用的特殊 OAuth2 授权请求参数的默认验证器。默认实现将验证 redirect_uriscope 参数。如果验证失败,将会抛出 OAuth2AuthorizationCodeRequestAuthenticationException

OAuth2AuthorizationCodeRequestAuthenticationProvider 提供了通过将类型为 Consumer<OAuth2AuthorizationCodeRequestAuthenticationContext> 的自定义验证器提供给 setAuthenticationValidator() 来覆盖默认授权请求验证的功能。

OAuth2AuthorizationCodeRequestAuthenticationContext 持有 OAuth2AuthorizationCodeRequestAuthenticationToken,其中包含 OAuth2 授权请求参数。

如果验证失败,则验证授权者 MUST 抛出 OAuth2AuthorizationCodeRequestAuthenticationException

生命周期开发阶段中一个常见的用例是允许 localhost 出现 redirect_uri 参数中。

以下示例演示如何使用允许 localhost 出现 redirect_uri 参数中的自定义验证器对 OAuth2AuthorizationCodeRequestAuthenticationProvider 进行配置:

@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 Device Authorization Endpoint

`OAuth2DeviceAuthorizationEndpointConfigurer`提供了自定义 OAuth2 Device Authorization endpoint的能力。它定义了扩展点,这些扩展点让您能够自定义 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 (pre-processor),在尝试从 `HttpServletRequest`中提取 OAuth2 device authorization request到一个 `OAuth2DeviceAuthorizationRequestAuthenticationToken`实例时使用。
2 deviceAuthorizationRequestConverters(): 设置一个 Consumer,提供对默认和(可选)添加的 AuthenticationConverter&#8217;s allowing the ability to add, remove, or customize a specific `AuthenticationConverterList 的访问。
3 authenticationProvider(): 添加一个 AuthenticationProvider (main processor),用于对 OAuth2DeviceAuthorizationRequestAuthenticationToken 进行身份验证。
4 authenticationProviders():设置一个 Consumer,提供对默认和(可选)添加的 AuthenticationProvider&#8217;s allowing the ability to add, remove, or customize a specific `AuthenticationProviderList 的访问。
5 deviceAuthorizationResponseHandler():用于处理 “authenticated” OAuth2DeviceAuthorizationRequestAuthenticationToken`和返回 OAuth2DeviceAuthorizationResponse的 `AuthenticationSuccessHandler (post-processor)。
6 errorResponseHandler():用于处理 OAuth2AuthenticationException`和返回 OAuth2Error response的 `AuthenticationFailureHandler (post-processor)。
7 verificationUri():资源所有者在辅助设备上直接访问自定义最终用户验证页面的 URI

OAuth2DeviceAuthorizationEndpointConfigurer 配置 OAuth2DeviceAuthorizationEndpointFilter 并将其注册到 OAuth2 授权服务器 SecurityFilterChain @BeanOAuth2DeviceAuthorizationEndpointFilter 是负责处理 OAuth2 设备授权请求的 Filter

OAuth2DeviceAuthorizationEndpointFilter 使用以下默认值进行配置:

  • AuthenticationConverter — An OAuth2DeviceAuthorizationRequestAuthenticationConverter.

  • AuthenticationManager — 一个由 OAuth2DeviceAuthorizationRequestAuthenticationProvider 组成的 AuthenticationManager

  • AuthenticationSuccessHandler — 一个处理 “authenticated” OAuth2DeviceAuthorizationRequestAuthenticationToken 并返回 OAuth2DeviceAuthorizationResponse 的内部实现。

  • AuthenticationFailureHandler — An OAuth2ErrorAuthenticationFailureHandler.

OAuth2 Device Verification Endpoint

`OAuth2DeviceVerificationEndpointConfigurer`提供了自定义 OAuth2 Device Verification endpoint(或“用户交互”)的能力。它定义了扩展点,这些扩展点让您能够自定义 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 (pre-processor),在尝试从 `HttpServletRequest`中提取 OAuth2 device verification request(或同意)到一个 `OAuth2DeviceVerificationAuthenticationToken`或 `OAuth2DeviceAuthorizationConsentAuthenticationToken`实例时使用。
2 deviceVerificationRequestConverters(): 设置一个 Consumer,提供对默认和(可选)添加的 AuthenticationConverter&#8217;s allowing the ability to add, remove, or customize a specific `AuthenticationConverterList 的访问。
3 authenticationProvider(): 添加了 AuthenticationProvider (main processor) 用来认证 OAuth2DeviceVerificationAuthenticationTokenOAuth2DeviceAuthorizationConsentAuthenticationToken
4 authenticationProviders():设置一个 Consumer,提供对默认和(可选)添加的 AuthenticationProvider&#8217;s allowing the ability to add, remove, or customize a specific `AuthenticationProviderList 的访问。
5 deviceVerificationResponseHandler(): AuthenticationSuccessHandler (post-processor) 用于处理 “authenticated” OAuth2DeviceVerificationAuthenticationToken,并指示资源所有者返回他们的设备。
6 errorResponseHandler(): AuthenticationFailureHandler (post-processor) 用于处理 OAuth2AuthenticationException 并返回错误响应。
7 consentPage(): 自定义同意页面的 URI,如果在设备验证请求流程期间需要同意,则会将资源所有者重定向到此页面。

OAuth2DeviceVerificationEndpointConfigurer 配置 OAuth2DeviceVerificationEndpointFilter,并将其注册到 OAuth2 授权服务器 SecurityFilterChain @Bean 中。OAuth2DeviceVerificationEndpointFilter 是处理 OAuth2 设备验证请求(和同意)的 Filter

OAuth2DeviceVerificationEndpointFilter 配置了以下默认值:

  • AuthenticationConverter — 一个 DelegatingAuthenticationConverter,包含 OAuth2DeviceVerificationAuthenticationConverterOAuth2DeviceAuthorizationConsentAuthenticationConverter

  • AuthenticationManager — 一个 AuthenticationManager,包含 OAuth2DeviceVerificationAuthenticationProviderOAuth2DeviceAuthorizationConsentAuthenticationProvider

  • AuthenticationSuccessHandler — 一个 SimpleUrlAuthenticationSuccessHandler,处理 “authenticated” OAuth2DeviceVerificationAuthenticationToken 并将用户重定向到成功页面 (/?success)。

  • AuthenticationFailureHandler — 一个内部实现,它使用与 OAuth2AuthenticationException 关联的 OAuth2Error,并返回 OAuth2Error 响应。

OAuth2 Token Endpoint

`OAuth2TokenEndpointConfigurer`提供了自定义 OAuth2 Token endpoint的能力。它定义了扩展点,这些扩展点让您能够自定义 OAuth2 access token requests的预处理、主处理和后处理逻辑。

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 (pre-processor),在尝试从 `HttpServletRequest`中提取 OAuth2 access token request到一个 `OAuth2AuthorizationGrantAuthenticationToken`实例时使用。
2 accessTokenRequestConverters(): 设置 Consumer,提供对默认 AuthenticationConverter&#8217;s allowing the ability to add, remove, or customize a specific `AuthenticationConverter 和(可选地)已添加的 AuthenticationConverter&#8217;s allowing the ability to add, remove, or customize a specific `AuthenticationConverter 的访问。
3 authenticationProvider(): 添加了 AuthenticationProvider (main processor) 用来认证 OAuth2AuthorizationGrantAuthenticationToken
4 authenticationProviders():设置一个 Consumer,提供对默认和(可选)添加的 AuthenticationProvider&#8217;s allowing the ability to add, remove, or customize a specific `AuthenticationProviderList 的访问。
5 accessTokenResponseHandler(): AuthenticationSuccessHandler (post-processor) 用于处理 OAuth2AccessTokenAuthenticationToken 并返回 link:https://datatracker.ietf.org/doc/html/rfc6749#section-5.1[OAuth2AccessTokenResponse
6 errorResponseHandler():用于处理 OAuth2AuthenticationException`和返回 OAuth2Error response的 `AuthenticationFailureHandler (post-processor)。

OAuth2TokenEndpointConfigurer 配置 OAuth2TokenEndpointFilter,并将其注册到 OAuth2 授权服务器 SecurityFilterChain @Bean 中。OAuth2TokenEndpointFilter 是处理 OAuth2 访问令牌请求的 Filter

所支持的 authorization grant typesauthorization_coderefresh_tokenclient_credentials`和 `urn:ietf:params:oauth:grant-type:device_code

OAuth2TokenEndpointFilter 配置了以下默认值:

  • AuthenticationConverter — 一个 DelegatingAuthenticationConverter,包含 OAuth2AuthorizationCodeAuthenticationConverterOAuth2RefreshTokenAuthenticationConverterOAuth2ClientCredentialsAuthenticationConverterOAuth2DeviceCodeAuthenticationConverterOAuth2TokenExchangeAuthenticationConverter

  • AuthenticationManager — 一个 AuthenticationManager,包含 OAuth2AuthorizationCodeAuthenticationProviderOAuth2RefreshTokenAuthenticationProviderOAuth2ClientCredentialsAuthenticationProviderOAuth2DeviceCodeAuthenticationProviderOAuth2TokenExchangeAuthenticationProvider

  • AuthenticationSuccessHandler — An OAuth2AccessTokenResponseAuthenticationSuccessHandler.

  • AuthenticationFailureHandler — An OAuth2ErrorAuthenticationFailureHandler.

Customizing Client Credentials Grant Request Validation

OAuth2ClientCredentialsAuthenticationValidator 是用于验证特定 OAuth2 客户端凭证授予请求参数的默认验证器。默认实现验证 scope 参数。如果验证失败,将会抛出 OAuth2AuthenticationException

OAuth2ClientCredentialsAuthenticationProvider 提供了通过向 setAuthenticationValidator() 提供类型为 Consumer<OAuth2ClientCredentialsAuthenticationContext> 的自定义验证器来覆盖默认请求验证的能力。

OAuth2ClientCredentialsAuthenticationContext 持有 OAuth2ClientCredentialsAuthenticationToken,其中包含 OAuth2 客户端凭据授予请求参数。

如果验证失败,则验证授权者 MUST 抛出 OAuth2AuthenticationException

以下示例展示了如何使用覆盖默认 scope 验证的自定义验证器来配置 OAuth2ClientCredentialsAuthenticationProvider

@Bean
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
	OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
		new OAuth2AuthorizationServerConfigurer();
	http.apply(authorizationServerConfigurer);

	authorizationServerConfigurer
		.tokenEndpoint(tokenEndpoint ->
			tokenEndpoint
				.authenticationProviders(configureAuthenticationValidator())
		);

	return http.build();
}

private Consumer<List<AuthenticationProvider>> configureAuthenticationValidator() {
	return (authenticationProviders) ->
		authenticationProviders.forEach((authenticationProvider) -> {
			if (authenticationProvider instanceof OAuth2ClientCredentialsAuthenticationProvider) {
				Consumer<OAuth2ClientCredentialsAuthenticationContext> authenticationValidator =
					new CustomScopeValidator();

				// Override default scope validation
				((OAuth2ClientCredentialsAuthenticationProvider) authenticationProvider)
					.setAuthenticationValidator(authenticationValidator);
			}
		});
}

static class CustomScopeValidator implements Consumer<OAuth2ClientCredentialsAuthenticationContext> {

	@Override
	public void accept(OAuth2ClientCredentialsAuthenticationContext authenticationContext) {
		OAuth2ClientCredentialsAuthenticationToken clientCredentialsAuthentication =
			authenticationContext.getAuthentication();

		Set<String> requestedScopes = clientCredentialsAuthentication.getScopes();
		RegisteredClient registeredClient = authenticationContext.getRegisteredClient();
		Set<String> allowedScopes = registeredClient.getScopes();

        // TODO Implement scope validation

	}
}

OAuth2 Token Introspection Endpoint

`OAuth2TokenIntrospectionEndpointConfigurer`提供了自定义 OAuth2 Token Introspection endpoint的能力。它定义了扩展点,这些扩展点让您能够自定义 OAuth2 introspection requests的预处理、主处理和后处理逻辑。

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 (pre-processor),在尝试从 `HttpServletRequest`中提取 OAuth2 introspection request到一个 `OAuth2TokenIntrospectionAuthenticationToken`实例时使用。
2 introspectionRequestConverters(): 设置 Consumer,提供对默认 AuthenticationConverter&#8217;s allowing the ability to add, remove, or customize a specific `AuthenticationConverter 和(可选地)已添加的 AuthenticationConverter&#8217;s allowing the ability to add, remove, or customize a specific `AuthenticationConverter 的访问。
3 authenticationProvider(): 添加了 AuthenticationProvider (main processor) 用来认证 OAuth2TokenIntrospectionAuthenticationToken
4 authenticationProviders():设置一个 Consumer,提供对默认和(可选)添加的 AuthenticationProvider&#8217;s allowing the ability to add, remove, or customize a specific `AuthenticationProviderList 的访问。
5 introspectionResponseHandler():用于处理 “authenticated” OAuth2TokenIntrospectionAuthenticationToken`和返回 OAuth2TokenIntrospection response的 `AuthenticationSuccessHandler (post-processor)。
6 errorResponseHandler():用于处理 OAuth2AuthenticationException`和返回 OAuth2Error response的 `AuthenticationFailureHandler (post-processor)。

OAuth2TokenIntrospectionEndpointConfigurer 配置 OAuth2TokenIntrospectionEndpointFilter,并将其注册到 OAuth2 授权服务器 SecurityFilterChain @Bean 中。OAuth2TokenIntrospectionEndpointFilter 是处理 OAuth2 自省请求的 Filter

OAuth2TokenIntrospectionEndpointFilter 配置了以下默认值:

  • AuthenticationConverter — An OAuth2TokenIntrospectionAuthenticationConverter.

  • AuthenticationManager — 一个由 OAuth2TokenIntrospectionAuthenticationProvider`组成的 `AuthenticationManager

  • AuthenticationSuccessHandler — 一个内部实现,用于处理一个 OAuth2TokenIntrospectionAuthenticationToken "`authenticated`"并返回 OAuth2TokenIntrospection 响应。

  • AuthenticationFailureHandler — An OAuth2ErrorAuthenticationFailureHandler.

OAuth2 Token Revocation Endpoint

`OAuth2TokenRevocationEndpointConfigurer`提供了自定义 OAuth2 Token Revocation endpoint的能力。它定义了扩展点,这些扩展点让您能够自定义 OAuth2 revocation requests的预处理、主处理和后处理逻辑。

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 (pre-processor),在尝试从 `HttpServletRequest`中提取 OAuth2 revocation request到一个 `OAuth2TokenRevocationAuthenticationToken`实例时使用。
2 revocationRequestConverters(): 设置 Consumer 来提供对默认 AuthenticationConverter&#8217;s allowing the ability to add, remove, or customize a specific `AuthenticationConverterList 的访问(可选)。
3 authenticationProvider(): 添加一个 AuthenticationProvider (main processor),用于给 OAuth2TokenRevocationAuthenticationToken 授权。
4 authenticationProviders():设置一个 Consumer,提供对默认和(可选)添加的 AuthenticationProvider&#8217;s allowing the ability to add, remove, or customize a specific `AuthenticationProviderList 的访问。
5 revocationResponseHandler():用于处理 “authenticated” OAuth2TokenRevocationAuthenticationToken`和返回 OAuth2 revocation response的 `AuthenticationSuccessHandler (post-processor)。
6 errorResponseHandler():用于处理 OAuth2AuthenticationException`和返回 OAuth2Error response的 `AuthenticationFailureHandler (post-processor)。

OAuth2TokenRevocationEndpointConfigurer 配置 OAuth2TokenRevocationEndpointFilter,并将其注册到 OAuth2 授权服务器 SecurityFilterChain @Bean 中。OAuth2TokenRevocationEndpointFilter 是处理 OAuth2 撤销请求的 Filter

OAuth2TokenRevocationEndpointFilter 配置了以下默认值:

  • AuthenticationConverter — An OAuth2TokenRevocationAuthenticationConverter.

  • AuthenticationManager — 一个由 OAuth2TokenRevocationAuthenticationProvider`组成的 `AuthenticationManager

  • AuthenticationSuccessHandler — 一个内部实现,用于处理一个 OAuth2TokenRevocationAuthenticationToken "`authenticated`"并返回 OAuth2 吊销响应。

  • AuthenticationFailureHandler — An OAuth2ErrorAuthenticationFailureHandler.

OAuth2 Authorization Server Metadata Endpoint

`OAuth2AuthorizationServerMetadataEndpointConfigurer`提供了自定义 OAuth2 Authorization Server Metadata endpoint的能力。它定义了一个扩展点,该扩展点让您能够自定义 OAuth2 Authorization Server Metadata response

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(): Consumer 提供对 OAuth2AuthorizationServerMetadata.Builder 的访问,允许自定义授权服务器配置的声明。

OAuth2AuthorizationServerMetadataEndpointConfigurer`配置 `OAuth2AuthorizationServerMetadataEndpointFilter`并将它注册到 OAuth2 授权服务器 `SecurityFilterChain @Bean`中。`OAuth2AuthorizationServerMetadataEndpointFilter`是返回 OAuth2AuthorizationServerMetadata response的 `Filter

JWK Set Endpoint

`OAuth2AuthorizationServerConfigurer`提供了对 JWK Set endpoint的支持。

OAuth2AuthorizationServerConfigurer`配置 `NimbusJwkSetEndpointFilter`并将它注册到 OAuth2 授权服务器 `SecurityFilterChain @Bean`中。`NimbusJwkSetEndpointFilter`是返回 JWK Set的 `Filter

如果一个 JWKSource<SecurityContext> @Bean 已注册,则 JWK 集端点 only 已配置。

OpenID Connect 1.0 Provider Configuration Endpoint

`OidcProviderConfigurationEndpointConfigurer`提供了自定义 OpenID Connect 1.0 Provider Configuration endpoint的能力。它定义了一个扩展点,该扩展点让您能够自定义 OpenID Provider Configuration response

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 提供程序配置的声明。

OidcProviderConfigurationEndpointConfigurer`配置 `OidcProviderConfigurationEndpointFilter`并将它注册到 OAuth2 授权服务器 `SecurityFilterChain @Bean`中。`OidcProviderConfigurationEndpointFilter`是返回 OidcProviderConfiguration response的 `Filter

OpenID Connect 1.0 Logout Endpoint

`OidcLogoutEndpointConfigurer`提供了自定义 OpenID Connect 1.0 Logout endpoint的能力。它定义了扩展点,这些扩展点让您能够自定义 RP 发起的注销请求的预处理、主处理和后处理逻辑。

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 (pre-processor),在尝试从 `HttpServletRequest`中提取一个 Logout request到一个 `OidcLogoutAuthenticationToken`实例时使用。
2 logoutRequestConverters(): 设置 Consumer 来提供对默认 AuthenticationConverter&#8217;s allowing the ability to add, remove, or customize a specific `AuthenticationConverterList 的访问(可选)。
3 authenticationProvider(): 添加一个 AuthenticationProvider (main processor),用于给 OidcLogoutAuthenticationToken 授权。
4 authenticationProviders():设置一个 Consumer,提供对默认和(可选)添加的 AuthenticationProvider&#8217;s allowing the ability to add, remove, or customize a specific `AuthenticationProviderList 的访问。
5 logoutResponseHandler(): AuthenticationSuccessHandler (post-processor) 用于处理一个 OidcLogoutAuthenticationToken “authenticated” 并执行注销。
6 errorResponseHandler(): AuthenticationFailureHandler (post-processor) 用于处理 OAuth2AuthenticationException 并返回错误响应。

OidcLogoutEndpointConfigurer`配置 `OidcLogoutEndpointFilter`并将它注册到 OAuth2 授权服务器 `SecurityFilterChain @Bean`中。`OidcLogoutEndpointFilter`是处理 RP-Initiated Logout requests并执行最终用户注销的 `Filter

OidcLogoutEndpointFilter 配置了以下默认值:

  • AuthenticationConverter — An OidcLogoutAuthenticationConverter.

  • AuthenticationManager — 一个由 OidcLogoutAuthenticationProvider`组成的 `AuthenticationManager

  • AuthenticationSuccessHandler — 一个内部实现,用于处理一个 OidcLogoutAuthenticationToken "`authenticated`"并执行注销。

  • AuthenticationFailureHandler — 一个内部实现,它使用与 OAuth2AuthenticationException 关联的 OAuth2Error,并返回 OAuth2Error 响应。

OidcLogoutAuthenticationProvider 使用 SessionRegistry 查找与请求登出的最终用户关联的 SessionInformation 实例。

OidcClientInitiatedLogoutSuccessHandler 是 Spring Security 的 OAuth2 客户端支持中用于配置 OpenID Connect 1.0 RP-Initiated Logout 的相应配置。

OpenID Connect 1.0 UserInfo Endpoint

`OidcUserInfoEndpointConfigurer`提供了自定义 OpenID Connect 1.0 UserInfo endpoint的能力。它定义了扩展点,这些扩展点让您能够自定义 UserInfo requests的预处理、主处理和后处理逻辑。

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 (pre-processor),在尝试从 `HttpServletRequest`中提取一个 UserInfo request到一个 `OidcUserInfoAuthenticationToken`实例时使用。
2 userInfoRequestConverters(): 设置 Consumer 来提供对默认 AuthenticationConverter&#8217;s allowing the ability to add, remove, or customize a specific `AuthenticationConverterList 的访问(可选)。
3 authenticationProvider(): 添加 AuthenticationProvider (main processor),用于验证 OidcUserInfoAuthenticationToken
4 authenticationProviders():设置一个 Consumer,提供对默认和(可选)添加的 AuthenticationProvider&#8217;s allowing the ability to add, remove, or customize a specific `AuthenticationProviderList 的访问。
5 userInfoResponseHandler():用于处理 “authenticated” OidcUserInfoAuthenticationToken`和返回 UserInfo response的 `AuthenticationSuccessHandler (post-processor)。
6 errorResponseHandler():用于处理 OAuth2AuthenticationException`和返回 UserInfo Error response的 `AuthenticationFailureHandler (post-processor)。
7 userInfoMapper(): Function 用于将 @{18} 的声明提取到 OidcUserInfo 的实例中。

OidcUserInfoEndpointConfigurer`配置了`OidcUserInfoEndpointFilter`并将其注册到OAuth2授权服务器`SecurityFilterChain`@Bean`.OidcUserInfoEndpointFilter`是用于处理 UserInfo requests并返回 OidcUserInfo response的`Filter.

OidcUserInfoEndpointFilter 配置了以下默认值:

  • AuthenticationConverter — 内部实现,它从 SecurityContext 中获取 Authentication,并使用主体创建 OidcUserInfoAuthenticationToken

  • AuthenticationManager——一个由 OidcUserInfoAuthenticationProvider`组成的 `AuthenticationManager,它与 `userInfoMapper`的内部实现相关联,该实现根据授权期间的 scopes requestedID Token中提取 standard claims

  • AuthenticationSuccessHandler — 内部实现,它处理 “authenticated” OidcUserInfoAuthenticationToken 并返回 OidcUserInfo 响应。

  • AuthenticationFailureHandler — 一个内部实现,它使用与 OAuth2AuthenticationException 关联的 OAuth2Error,并返回 OAuth2Error 响应。

您可以通过提供 OAuth2TokenCustomizer<JwtEncodingContext> @Bean 来自定义 ID 令牌。

OpenID Connect 1.0 UserInfo端点是一个受OAuth2保护的资源,它*REQUIRES*获取访问令牌,作为持有者令牌发送到 UserInfo request中。以下示例展示了如何启用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);
}

一个 JwtDecoder @Bean 对于 OpenID Connect 1.0 UserInfo 端点 REQUIRED

指南 How-to: Customize the OpenID Connect 1.0 UserInfo response 含有有关如何自定义 UserInfo 端点的示例。

OpenID Connect 1.0 Client Registration Endpoint

`OidcClientRegistrationEndpointConfigurer`提供了自定义 OpenID Connect 1.0 Client Registration endpoint的能力。它定义了让你能够自定义针对 Client Registration requestsClient Read requests进行预处理、主处理和后处理逻辑的扩展点。

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 (pre-processor),在尝试从 `HttpServletRequest`中提取一个 Client Registration requestClient Read request到一个 `OidcClientRegistrationAuthenticationToken`实例时使用。
2 clientRegistrationRequestConverters(): 设置 Consumer,提供对默认 List 和(可选)已添加 AuthenticationConverter&#8217;s allowing the ability to add, remove, or customize a specific `AuthenticationConverter 的访问权限。
3 authenticationProvider(): 添加 AuthenticationProvider (main processor),用于验证 OidcClientRegistrationAuthenticationToken
4 authenticationProviders():设置一个 Consumer,提供对默认和(可选)添加的 AuthenticationProvider&#8217;s allowing the ability to add, remove, or customize a specific `AuthenticationProviderList 的访问。
5 clientRegistrationResponseHandler():用于处理一个 “authenticated” OidcClientRegistrationAuthenticationToken`和返回 Client Registration responseClient Read response的 `AuthenticationSuccessHandler (post-processor)。
6 errorResponseHandler():用于处理一个 OAuth2AuthenticationException`和返回 Client Registration Error responseClient Read Error response的 `AuthenticationFailureHandler (post-processor)。

OpenID Connect 1.0 客户端注册端点在默认情况下禁用,因为很多部署不需要动态客户端注册。

OidcClientRegistrationEndpointConfigurer`配置了`OidcClientRegistrationEndpointFilter`并将其注册到OAuth2授权服务器`SecurityFilterChain`@Bean`.OidcClientRegistrationEndpointFilter`是用于处理 Client Registration requests并返回 OidcClientRegistration response的`Filter.

OidcClientRegistrationEndpointFilter 还处理 Client Read requests 并返回 OidcClientRegistration response

OidcClientRegistrationEndpointFilter 配置了以下默认值:

  • AuthenticationConverter — An OidcClientRegistrationAuthenticationConverter.

  • AuthenticationManager — AuthenticationManagerOidcClientRegistrationAuthenticationProviderOidcClientConfigurationAuthenticationProvider 组成。

  • AuthenticationSuccessHandler — 内部实现,它处理 “authenticated” OidcClientRegistrationAuthenticationToken 并返回 OidcClientRegistration 响应。

  • AuthenticationFailureHandler — 一个内部实现,它使用与 OAuth2AuthenticationException 关联的 OAuth2Error,并返回 OAuth2Error 响应。

OpenID Connect 1.0 Client Registration端点是一个 OAuth2 protected resource,它*REQUIRES*获取访问令牌,作为持有者令牌发送到Client Registration (或Client Read)请求中。

客户端注册请求中的访问令牌 REQUIRES OAuth2 范围 client.create

客户端读取请求中的访问令牌 REQUIRES 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);
}

一个 JwtDecoder @Bean 对于 OpenID Connect 1.0 客户端注册端点 REQUIRED