通訊協定端點

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 授權請求(或同意),並轉換為 OAuth2AuthorizationCodeRequestAuthenticationTokenOAuth2AuthorizationConsentAuthenticationToken 的實例。
2 authorizationRequestConverters():設定 Consumer,提供對預設和(可選)新增的 AuthenticationConverter List 的存取權,允許新增、移除或自訂特定的 AuthenticationConverter
3 authenticationProvider():新增一個 AuthenticationProvider主要處理器),用於驗證 OAuth2AuthorizationCodeRequestAuthenticationTokenOAuth2AuthorizationConsentAuthenticationToken
4 authenticationProviders():設定 Consumer,提供對預設和(可選)新增的 AuthenticationProvider List 的存取權,允許新增、移除或自訂特定的 AuthenticationProvider
5 authorizationResponseHandler()AuthenticationSuccessHandler後處理器),用於處理「已驗證」的 OAuth2AuthorizationCodeRequestAuthenticationToken,並傳回 OAuth2AuthorizationResponse
6 errorResponseHandler()AuthenticationFailureHandler後處理器),用於處理 OAuth2AuthorizationCodeRequestAuthenticationException,並傳回 OAuth2Error 回應
7 consentPage():自訂同意頁面的 URI,用於在授權請求流程中需要同意時,將資源擁有者重新導向至該頁面。

OAuth2AuthorizationEndpointConfigurer 組態 OAuth2AuthorizationEndpointFilter,並將其註冊到 OAuth2 授權伺服器 SecurityFilterChain @BeanOAuth2AuthorizationEndpointFilter 是處理 OAuth2 授權請求(和同意)的 Filter

OAuth2AuthorizationEndpointFilter 組態了以下預設值

  • AuthenticationConverter — 一個 DelegatingAuthenticationConverter,由 OAuth2AuthorizationCodeRequestAuthenticationConverterOAuth2AuthorizationConsentAuthenticationConverter 組成。

  • AuthenticationManager — 一個 AuthenticationManager,由 OAuth2AuthorizationCodeRequestAuthenticationProviderOAuth2AuthorizationConsentAuthenticationProvider 組成。

  • AuthenticationSuccessHandler — 一個內部實作,處理「已驗證」的 OAuth2AuthorizationCodeRequestAuthenticationToken,並傳回 OAuth2AuthorizationResponse

  • AuthenticationFailureHandler — 一個內部實作,使用與 OAuth2AuthorizationCodeRequestAuthenticationException 關聯的 OAuth2Error,並傳回 OAuth2Error 回應。

自訂授權請求驗證

OAuth2AuthorizationCodeRequestAuthenticationValidator 是用於驗證授權碼 Grant 中使用的特定 OAuth2 授權請求參數的預設驗證器。預設實作驗證 redirect_uriscope 參數。如果驗證失敗,則會拋出 OAuth2AuthorizationCodeRequestAuthenticationException

OAuth2AuthorizationCodeRequestAuthenticationProvider 提供覆寫預設授權請求驗證的能力,方法是提供類型為 Consumer<OAuth2AuthorizationCodeRequestAuthenticationContext> 的自訂驗證器給 setAuthenticationValidator()

OAuth2AuthorizationCodeRequestAuthenticationContext 保留 OAuth2AuthorizationCodeRequestAuthenticationToken,其中包含 OAuth2 授權請求參數。
如果驗證失敗,驗證器必須拋出 OAuth2AuthorizationCodeRequestAuthenticationException

在開發生命週期階段中,常見的使用案例是允許 redirect_uri 參數中使用 localhost

以下範例示範如何使用允許 redirect_uri 參數中使用 localhost 的自訂驗證器來組態 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 裝置授權端點

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():設定 Consumer,提供對預設和(可選)新增的 AuthenticationConverter List 的存取權,允許新增、移除或自訂特定的 AuthenticationConverter
3 authenticationProvider():新增一個 AuthenticationProvider主要處理器),用於驗證 OAuth2DeviceAuthorizationRequestAuthenticationToken
4 authenticationProviders():設定 Consumer,提供對預設和(可選)新增的 AuthenticationProvider List 的存取權,允許新增、移除或自訂特定的 AuthenticationProvider
5 deviceAuthorizationResponseHandler()AuthenticationSuccessHandler後處理器),用於處理「已驗證」的 OAuth2DeviceAuthorizationRequestAuthenticationToken,並傳回 OAuth2DeviceAuthorizationResponse
6 errorResponseHandler()AuthenticationFailureHandler後處理器),用於處理 OAuth2AuthenticationException,並傳回 OAuth2Error 回應
7 verificationUri():自訂使用者驗證頁面的 URI,用於將資源擁有者導向至次要裝置。

OAuth2DeviceAuthorizationEndpointConfigurer 組態 OAuth2DeviceAuthorizationEndpointFilter,並將其註冊到 OAuth2 授權伺服器 SecurityFilterChain @BeanOAuth2DeviceAuthorizationEndpointFilter 是處理 OAuth2 裝置授權請求的 Filter

OAuth2DeviceAuthorizationEndpointFilter 組態了以下預設值

  • AuthenticationConverter — 一個 OAuth2DeviceAuthorizationRequestAuthenticationConverter

  • AuthenticationManager — 一個 AuthenticationManager,由 OAuth2DeviceAuthorizationRequestAuthenticationProvider 組成。

  • 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 裝置驗證請求(或同意),並轉換為 OAuth2DeviceVerificationAuthenticationTokenOAuth2DeviceAuthorizationConsentAuthenticationToken 的實例。
2 deviceVerificationRequestConverters():設定 Consumer,提供對預設和(可選)新增的 AuthenticationConverter List 的存取權,允許新增、移除或自訂特定的 AuthenticationConverter
3 authenticationProvider():新增一個 AuthenticationProvider主要處理器),用於驗證 OAuth2DeviceVerificationAuthenticationTokenOAuth2DeviceAuthorizationConsentAuthenticationToken
4 authenticationProviders():設定 Consumer,提供對預設和(可選)新增的 AuthenticationProvider List 的存取權,允許新增、移除或自訂特定的 AuthenticationProvider
5 deviceVerificationResponseHandler()AuthenticationSuccessHandler後處理器),用於處理「已驗證」的 OAuth2DeviceVerificationAuthenticationToken,並指示資源擁有者返回其裝置。
6 errorResponseHandler()AuthenticationFailureHandler後處理器),用於處理 OAuth2AuthenticationException,並傳回錯誤回應。
7 consentPage():自訂同意頁面的 URI,用於在裝置驗證請求流程中需要同意時,將資源擁有者重新導向至該頁面。

OAuth2DeviceVerificationEndpointConfigurer 組態 OAuth2DeviceVerificationEndpointFilter,並將其註冊到 OAuth2 授權伺服器 SecurityFilterChain @BeanOAuth2DeviceVerificationEndpointFilter 是處理 OAuth2 裝置驗證請求(和同意)的 Filter

OAuth2DeviceVerificationEndpointFilter 組態了以下預設值

  • AuthenticationConverter — 一個 DelegatingAuthenticationConverter,由 OAuth2DeviceVerificationAuthenticationConverterOAuth2DeviceAuthorizationConsentAuthenticationConverter 組成。

  • AuthenticationManager — 一個 AuthenticationManager,由 OAuth2DeviceVerificationAuthenticationProviderOAuth2DeviceAuthorizationConsentAuthenticationProvider 組成。

  • AuthenticationSuccessHandler — 一個 SimpleUrlAuthenticationSuccessHandler,處理「已驗證」的 OAuth2DeviceVerificationAuthenticationToken,並將使用者重新導向至成功頁面 (/?success)。

  • 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():設定 Consumer,提供對預設和(可選)新增的 AuthenticationConverter List 的存取權,允許新增、移除或自訂特定的 AuthenticationConverter
3 authenticationProvider():新增一個 AuthenticationProvider主要處理器),用於驗證 OAuth2AuthorizationGrantAuthenticationToken
4 authenticationProviders():設定 Consumer,提供對預設和(可選)新增的 AuthenticationProvider List 的存取權,允許新增、移除或自訂特定的 AuthenticationProvider
5 accessTokenResponseHandler()AuthenticationSuccessHandler後處理器),用於處理 OAuth2AccessTokenAuthenticationToken,並傳回 OAuth2AccessTokenResponse
6 errorResponseHandler()AuthenticationFailureHandler後處理器),用於處理 OAuth2AuthenticationException,並傳回 OAuth2Error 回應

OAuth2TokenEndpointConfigurer 組態 OAuth2TokenEndpointFilter,並將其註冊到 OAuth2 授權伺服器 SecurityFilterChain @BeanOAuth2TokenEndpointFilter 是處理 OAuth2 存取權杖請求的 Filter

支援的 授權 Grant Typeauthorization_coderefresh_tokenclient_credentialsurn:ietf:params:oauth:grant-type:device_codeurn:ietf:params:oauth:grant-type:token-exchange

OAuth2TokenEndpointFilter 組態了以下預設值

  • AuthenticationConverter — 一個 DelegatingAuthenticationConverter,由 OAuth2AuthorizationCodeAuthenticationConverterOAuth2RefreshTokenAuthenticationConverterOAuth2ClientCredentialsAuthenticationConverterOAuth2DeviceCodeAuthenticationConverterOAuth2TokenExchangeAuthenticationConverter 組成。

  • AuthenticationManager — 一個 AuthenticationManager,由 OAuth2AuthorizationCodeAuthenticationProviderOAuth2RefreshTokenAuthenticationProviderOAuth2ClientCredentialsAuthenticationProviderOAuth2DeviceCodeAuthenticationProviderOAuth2TokenExchangeAuthenticationProvider 組成。

  • AuthenticationSuccessHandler — 一個 OAuth2AccessTokenResponseAuthenticationSuccessHandler

  • AuthenticationFailureHandler — 一個 OAuth2ErrorAuthenticationFailureHandler

自訂用戶端憑證 Grant 請求驗證

OAuth2ClientCredentialsAuthenticationValidator 是用於驗證特定 OAuth2 用戶端憑證 Grant 請求參數的預設驗證器。預設實作驗證 scope 參數。如果驗證失敗,則會拋出 OAuth2AuthenticationException

OAuth2ClientCredentialsAuthenticationProvider 提供覆寫預設請求驗證的能力,方法是提供類型為 Consumer<OAuth2ClientCredentialsAuthenticationContext> 的自訂驗證器給 setAuthenticationValidator()

OAuth2ClientCredentialsAuthenticationContext 保留 OAuth2ClientCredentialsAuthenticationToken,其中包含 OAuth2 用戶端憑證 Grant 請求參數。
如果驗證失敗,驗證器必須拋出 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 權杖內省端點

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():設定 Consumer,提供對預設和(可選)新增的 AuthenticationConverter List 的存取權,允許新增、移除或自訂特定的 AuthenticationConverter
3 authenticationProvider():新增一個 AuthenticationProvider主要處理器),用於驗證 OAuth2TokenIntrospectionAuthenticationToken
4 authenticationProviders():設定 Consumer,提供對預設和(可選)新增的 AuthenticationProvider List 的存取權,允許新增、移除或自訂特定的 AuthenticationProvider
5 introspectionResponseHandler()AuthenticationSuccessHandler後處理器),用於處理「已驗證」的 OAuth2TokenIntrospectionAuthenticationToken,並傳回 OAuth2TokenIntrospection 回應
6 errorResponseHandler()AuthenticationFailureHandler後處理器),用於處理 OAuth2AuthenticationException,並傳回 OAuth2Error 回應

OAuth2TokenIntrospectionEndpointConfigurer 組態 OAuth2TokenIntrospectionEndpointFilter,並將其註冊到 OAuth2 授權伺服器 SecurityFilterChain @BeanOAuth2TokenIntrospectionEndpointFilter 是處理 OAuth2 內省請求的 Filter

OAuth2TokenIntrospectionEndpointFilter 組態了以下預設值

  • AuthenticationConverter — 一個 OAuth2TokenIntrospectionAuthenticationConverter

  • AuthenticationManager — 一個 AuthenticationManager,由 OAuth2TokenIntrospectionAuthenticationProvider 組成。

  • 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():設定 Consumer,提供對預設和(可選)新增的 AuthenticationConverter List 的存取權,允許新增、移除或自訂特定的 AuthenticationConverter
3 authenticationProvider():新增一個 AuthenticationProvider主要處理器),用於驗證 OAuth2TokenRevocationAuthenticationToken
4 authenticationProviders():設定 Consumer,提供對預設和(可選)新增的 AuthenticationProvider List 的存取權,允許新增、移除或自訂特定的 AuthenticationProvider
5 revocationResponseHandler()AuthenticationSuccessHandler後處理器),用於處理「已驗證」的 OAuth2TokenRevocationAuthenticationToken,並傳回 OAuth2 撤銷回應
6 errorResponseHandler()AuthenticationFailureHandler後處理器),用於處理 OAuth2AuthenticationException,並傳回 OAuth2Error 回應

OAuth2TokenRevocationEndpointConfigurer 組態 OAuth2TokenRevocationEndpointFilter,並將其註冊到 OAuth2 授權伺服器 SecurityFilterChain @BeanOAuth2TokenRevocationEndpointFilter 是處理 OAuth2 撤銷請求的 Filter

OAuth2TokenRevocationEndpointFilter 組態了以下預設值

  • AuthenticationConverter — 一個 OAuth2TokenRevocationAuthenticationConverter

  • AuthenticationManager — 一個 AuthenticationManager,由 OAuth2TokenRevocationAuthenticationProvider 組成。

  • AuthenticationSuccessHandler — 一個內部實作,處理「已驗證」的 OAuth2TokenRevocationAuthenticationToken,並傳回 OAuth2 撤銷回應。

  • AuthenticationFailureHandler — 一個 OAuth2ErrorAuthenticationFailureHandler

OAuth2 授權伺服器 Metadata 端點

OAuth2AuthorizationServerMetadataEndpointConfigurer 提供自訂 OAuth2 授權伺服器 Metadata 端點 的能力。它定義了一個擴充點,讓您可以自訂 OAuth2 授權伺服器 Metadata 回應

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 @BeanOAuth2AuthorizationServerMetadataEndpointFilter 是傳回 OAuth2AuthorizationServerMetadata 回應Filter

JWK Set 端點

OAuth2AuthorizationServerConfigurer 提供對 JWK Set 端點 的支援。

OAuth2AuthorizationServerConfigurer 組態 NimbusJwkSetEndpointFilter,並將其註冊到 OAuth2 授權伺服器 SecurityFilterChain @BeanNimbusJwkSetEndpointFilter 是傳回 JWK SetFilter

JWK Set 端點僅在註冊了 JWKSource<SecurityContext> @Bean 時才會組態。

OpenID Connect 1.0 Provider Configuration 端點

OidcProviderConfigurationEndpointConfigurer 提供自訂 OpenID Connect 1.0 Provider Configuration 端點 的能力。它定義了一個擴充點,讓您可以自訂 OpenID Provider Configuration 回應

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 組態的宣告。

OidcProviderConfigurationEndpointConfigurer 組態 OidcProviderConfigurationEndpointFilter,並將其註冊到 OAuth2 授權伺服器 SecurityFilterChain @BeanOidcProviderConfigurationEndpointFilter 是傳回 OidcProviderConfiguration 回應Filter

OpenID Connect 1.0 登出端點

OidcLogoutEndpointConfigurer 提供自訂 OpenID Connect 1.0 登出端點 的能力。它定義了擴充點,讓您可以自訂 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預處理器),用於嘗試從 HttpServletRequest 中提取 登出請求,並轉換為 OidcLogoutAuthenticationToken 的實例。
2 logoutRequestConverters():設定 Consumer,提供對預設和(可選)新增的 AuthenticationConverter List 的存取權,允許新增、移除或自訂特定的 AuthenticationConverter
3 authenticationProvider():新增一個 AuthenticationProvider主要處理器),用於驗證 OidcLogoutAuthenticationToken
4 authenticationProviders():設定 Consumer,提供對預設和(可選)新增的 AuthenticationProvider List 的存取權,允許新增、移除或自訂特定的 AuthenticationProvider
5 logoutResponseHandler()AuthenticationSuccessHandler後處理器),用於處理「已驗證」的 OidcLogoutAuthenticationToken,並執行登出。
6 errorResponseHandler()AuthenticationFailureHandler後處理器),用於處理 OAuth2AuthenticationException,並傳回錯誤回應。

OidcLogoutEndpointConfigurer 組態 OidcLogoutEndpointFilter,並將其註冊到 OAuth2 授權伺服器 SecurityFilterChain @BeanOidcLogoutEndpointFilter 是處理 RP 啟動的登出請求 並執行終端使用者登出的 Filter

OidcLogoutEndpointFilter 組態了以下預設值

  • AuthenticationConverter — 一個 OidcLogoutAuthenticationConverter

  • AuthenticationManager — 一個 AuthenticationManager,由 OidcLogoutAuthenticationProvider 組成。

  • AuthenticationSuccessHandler — 一個內部實作,處理「已驗證」的 OidcLogoutAuthenticationToken,並執行登出。

  • AuthenticationFailureHandler — 一個內部實作,使用與 OAuth2AuthenticationException 關聯的 OAuth2Error,並傳回 OAuth2Error 回應。

OidcLogoutAuthenticationProvider 使用 SessionRegistry 查找與請求登出的終端使用者相關聯的 SessionInformation 實例。
OidcClientInitiatedLogoutSuccessHandler 是 Spring Security 的 OAuth2 用戶端支援中,用於組態 OpenID Connect 1.0 RP 啟動的登出 的對應組態。

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():新增一個 AuthenticationProvider主要處理器),用於驗證 OidcUserInfoAuthenticationToken
4 authenticationProviders():設定 Consumer,提供對預設和(可選)新增的 AuthenticationProvider List 的存取權,允許新增、移除或自訂特定的 AuthenticationProvider
5 userInfoResponseHandler()AuthenticationSuccessHandler後處理器),用於處理「已驗證」的 OidcUserInfoAuthenticationToken,並傳回 UserInfo 回應
6 errorResponseHandler()AuthenticationFailureHandler後處理器),用於處理 OAuth2AuthenticationException,並傳回 UserInfo 錯誤回應
7 userInfoMapper()Function 用於從 OidcUserInfoAuthenticationContext 中提取宣告,並轉換為 OidcUserInfo 的實例。

OidcUserInfoEndpointConfigurer 組態 OidcUserInfoEndpointFilter,並將其註冊到 OAuth2 授權伺服器 SecurityFilterChain @BeanOidcUserInfoEndpointFilter 是處理 UserInfo 請求 並傳回 OidcUserInfo 回應Filter

OidcUserInfoEndpointFilter 預設配置如下

  • AuthenticationConverter — 一個內部實作,從 SecurityContext 取得 Authentication,並使用主體建立一個 OidcUserInfoAuthenticationToken

  • AuthenticationManager — 一個由 OidcUserInfoAuthenticationProvider 組成的 AuthenticationManager,它與 userInfoMapper 的內部實作相關聯,該實作會根據授權期間 請求的 scopesID Token 中提取 標準宣告

  • AuthenticationSuccessHandler — 一個內部實作,用於處理「已驗證」的 OidcUserInfoAuthenticationToken 並返回 OidcUserInfo 回應。

  • AuthenticationFailureHandler — 一個內部實作,使用與 OAuth2AuthenticationException 關聯的 OAuth2Error,並傳回 OAuth2Error 回應。

您可以透過提供一個 OAuth2TokenCustomizer<JwtEncodingContext> @Bean 來客製化 ID Token。

OpenID Connect 1.0 UserInfo 端點是一個 OAuth2 保護資源,需要UserInfo 請求 中以持有者令牌形式發送存取令牌。以下範例展示如何啟用 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 UserInfo 端點**需要**一個 JwtDecoder @Bean
操作指南:客製化 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():新增一個 AuthenticationProvider主要處理器),用於驗證 OidcClientRegistrationAuthenticationToken
4 authenticationProviders():設定 Consumer,提供對預設和(可選)新增的 AuthenticationProvider List 的存取權,允許新增、移除或自訂特定的 AuthenticationProvider
5 clientRegistrationResponseHandler()AuthenticationSuccessHandler後處理器),用於處理「已驗證」的 OidcClientRegistrationAuthenticationToken 並返回 用戶端註冊回應用戶端讀取回應
6 errorResponseHandler()AuthenticationFailureHandler後處理器),用於處理 OAuth2AuthenticationException 並返回 用戶端註冊錯誤回應用戶端讀取錯誤回應
OpenID Connect 1.0 用戶端註冊端點預設為停用,因為許多部署不需要動態用戶端註冊。

OidcClientRegistrationEndpointConfigurer 配置 OidcClientRegistrationEndpointFilter 並將其註冊到 OAuth2 授權伺服器 SecurityFilterChain @BeanOidcClientRegistrationEndpointFilter 是處理 用戶端註冊請求 並返回 OidcClientRegistration 回應Filter

OidcClientRegistrationEndpointFilter 也處理 用戶端讀取請求 並返回 OidcClientRegistration 回應

OidcClientRegistrationEndpointFilter 預設配置如下

  • AuthenticationConverter — 一個 OidcClientRegistrationAuthenticationConverter

  • AuthenticationManager — 一個由 OidcClientRegistrationAuthenticationProviderOidcClientConfigurationAuthenticationProvider 組成的 AuthenticationManager

  • AuthenticationSuccessHandler — 一個內部實作,用於處理「已驗證」的 OidcClientRegistrationAuthenticationToken 並返回 OidcClientRegistration 回應。

  • AuthenticationFailureHandler — 一個內部實作,使用與 OAuth2AuthenticationException 關聯的 OAuth2Error,並傳回 OAuth2Error 回應。

OpenID Connect 1.0 用戶端註冊端點是一個 OAuth2 保護資源需要 在用戶端註冊(或用戶端讀取)請求中以持有者令牌形式發送存取令牌。

用戶端註冊請求中的存取令牌**需要** OAuth2 scope client.create
用戶端讀取請求中的存取令牌**需要** OAuth2 scope 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