協議端點

OAuth2 授權端點

OAuth2AuthorizationEndpointConfigurer 提供自定義 OAuth2 授權端點 的能力。它定義了擴充套件點,允許您自定義 OAuth2 授權請求 的預處理、主要處理和後處理邏輯。

OAuth2AuthorizationEndpointConfigurer 提供以下配置選項

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

	http
		.securityMatcher(authorizationServerConfigurer.getEndpointsMatcher())
		.with(authorizationServerConfigurer, (authorizationServer) ->
			authorizationServer
				.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 (預處理器),用於嘗試將 OAuth2 授權請求(或同意)從 HttpServletRequest 中提取到 OAuth2AuthorizationCodeRequestAuthenticationTokenOAuth2AuthorizationConsentAuthenticationToken 的例項。
2 authorizationRequestConverters(): 設定 Consumer,提供對預設和(可選)新增的 AuthenticationConverter 列表的訪問許可權,允許新增、刪除或自定義特定的 AuthenticationConverter
3 authenticationProvider(): 新增一個 AuthenticationProvider (主處理器),用於驗證 OAuth2AuthorizationCodeRequestAuthenticationTokenOAuth2AuthorizationConsentAuthenticationToken
4 authenticationProviders(): 設定 Consumer,提供對預設和(可選)新增的 AuthenticationProvider 列表的訪問許可權,允許新增、刪除或自定義特定的 AuthenticationProvider
5 authorizationResponseHandler(): 用於處理已驗證的 OAuth2AuthorizationCodeRequestAuthenticationToken 並返回 OAuth2AuthorizationResponseAuthenticationSuccessHandler (後處理器)。
6 errorResponseHandler(): 用於處理 OAuth2AuthorizationCodeRequestAuthenticationException 並返回 OAuth2Error 響應AuthenticationFailureHandler (後處理器)。
7 consentPage(): 自定義同意頁面的 URI,如果在授權請求流程中需要同意,資源所有者將被重定向到此頁面。

OAuth2AuthorizationEndpointConfigurer 配置 OAuth2AuthorizationEndpointFilter 並將其註冊到 OAuth2 授權伺服器的 SecurityFilterChain @Bean 中。OAuth2AuthorizationEndpointFilter 是處理 OAuth2 授權請求(和同意)的 Filter

OAuth2AuthorizationEndpointFilter 配置了以下預設值

  • AuthenticationConverter — 由 OAuth2AuthorizationCodeRequestAuthenticationConverterOAuth2AuthorizationConsentAuthenticationConverter 組成的 DelegatingAuthenticationConverter

  • AuthenticationManager — 由 OAuth2AuthorizationCodeRequestAuthenticationProviderOAuth2AuthorizationConsentAuthenticationProvider 組成的 AuthenticationManager

  • AuthenticationSuccessHandler — 一個內部實現,用於處理已驗證的 OAuth2AuthorizationCodeRequestAuthenticationToken 並返回 OAuth2AuthorizationResponse

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

自定義授權請求驗證

OAuth2AuthorizationCodeRequestAuthenticationValidator 是用於驗證授權碼 Grant 中使用的特定 OAuth2 授權請求引數的預設驗證器。預設實現驗證 redirect_uriscope 引數。如果驗證失敗,則丟擲 OAuth2AuthorizationCodeRequestAuthenticationException

OAuth2AuthorizationCodeRequestAuthenticationProvider 允許透過向 setAuthenticationValidator() 提供一個型別為 Consumer<OAuth2AuthorizationCodeRequestAuthenticationContext> 的自定義身份驗證驗證器來覆蓋預設的授權請求驗證。

OAuth2AuthorizationCodeRequestAuthenticationContext 包含 OAuth2AuthorizationCodeRequestAuthenticationToken,後者包含 OAuth2 授權請求引數。
如果驗證失敗,身份驗證驗證器必須丟擲 OAuth2AuthorizationCodeRequestAuthenticationException

在開發生命週期階段的一個常見用例是允許 redirect_uri 引數中使用 localhost

以下示例展示瞭如何使用允許 redirect_uri 引數中使用 localhost 的自定義身份驗證驗證器配置 OAuth2AuthorizationCodeRequestAuthenticationProvider

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

	http
		.securityMatcher(authorizationServerConfigurer.getEndpointsMatcher())
		.with(authorizationServerConfigurer, (authorizationServer) ->
			authorizationServer
				.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 =
			OAuth2AuthorizationServerConfigurer.authorizationServer();

	http
		.securityMatcher(authorizationServerConfigurer.getEndpointsMatcher())
		.with(authorizationServerConfigurer, (authorizationServer) ->
			authorizationServer
				.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 (預處理器),用於嘗試將 OAuth2 裝置授權請求HttpServletRequest 中提取到 OAuth2DeviceAuthorizationRequestAuthenticationToken 的例項。
2 deviceAuthorizationRequestConverters(): 設定 Consumer,提供對預設和(可選)新增的 AuthenticationConverter 列表的訪問許可權,允許新增、刪除或自定義特定的 AuthenticationConverter
3 authenticationProvider(): 新增一個 AuthenticationProvider (主處理器),用於驗證 OAuth2DeviceAuthorizationRequestAuthenticationToken
4 authenticationProviders(): 設定 Consumer,提供對預設和(可選)新增的 AuthenticationProvider 列表的訪問許可權,允許新增、刪除或自定義特定的 AuthenticationProvider
5 deviceAuthorizationResponseHandler(): 用於處理已驗證的 OAuth2DeviceAuthorizationRequestAuthenticationToken 並返回 OAuth2DeviceAuthorizationResponseAuthenticationSuccessHandler (後處理器)。
6 errorResponseHandler(): 用於處理 OAuth2AuthenticationException 並返回 OAuth2Error 響應AuthenticationFailureHandler (後處理器)。
7 verificationUri(): 自定義終端使用者驗證頁面的 URI,用於引導資源所有者在輔助裝置上進行操作。

OAuth2DeviceAuthorizationEndpointConfigurer 配置 OAuth2DeviceAuthorizationEndpointFilter 並將其註冊到 OAuth2 授權伺服器的 SecurityFilterChain @Bean 中。OAuth2DeviceAuthorizationEndpointFilter 是處理 OAuth2 裝置授權請求的 Filter

OAuth2DeviceAuthorizationEndpointFilter 配置了以下預設值

  • AuthenticationConverter — 一個 OAuth2DeviceAuthorizationRequestAuthenticationConverter

  • AuthenticationManager — 由 OAuth2DeviceAuthorizationRequestAuthenticationProvider 組成的 AuthenticationManager

  • AuthenticationSuccessHandler — 一個內部實現,用於處理已驗證的 OAuth2DeviceAuthorizationRequestAuthenticationToken 並返回 OAuth2DeviceAuthorizationResponse

  • AuthenticationFailureHandler — 一個 OAuth2ErrorAuthenticationFailureHandler

OAuth2 裝置驗證端點

OAuth2DeviceVerificationEndpointConfigurer 提供自定義 OAuth2 裝置驗證端點(或“使用者互動”)的能力。它定義了擴充套件點,允許您自定義 OAuth2 裝置驗證請求的預處理、主要處理和後處理邏輯。

OAuth2DeviceVerificationEndpointConfigurer 提供以下配置選項

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

	http
		.securityMatcher(authorizationServerConfigurer.getEndpointsMatcher())
		.with(authorizationServerConfigurer, (authorizationServer) ->
			authorizationServer
				.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 (預處理器),用於嘗試將 OAuth2 裝置驗證請求(或同意)從 HttpServletRequest 中提取到 OAuth2DeviceVerificationAuthenticationTokenOAuth2DeviceAuthorizationConsentAuthenticationToken 的例項。
2 deviceVerificationRequestConverters(): 設定 Consumer,提供對預設和(可選)新增的 AuthenticationConverter 列表的訪問許可權,允許新增、刪除或自定義特定的 AuthenticationConverter
3 authenticationProvider(): 新增一個 AuthenticationProvider (主處理器),用於驗證 OAuth2DeviceVerificationAuthenticationTokenOAuth2DeviceAuthorizationConsentAuthenticationToken
4 authenticationProviders(): 設定 Consumer,提供對預設和(可選)新增的 AuthenticationProvider 列表的訪問許可權,允許新增、刪除或自定義特定的 AuthenticationProvider
5 deviceVerificationResponseHandler(): 用於處理已驗證的 OAuth2DeviceVerificationAuthenticationToken 並引導資源所有者返回其裝置的 AuthenticationSuccessHandler (後處理器)。
6 errorResponseHandler(): 用於處理 OAuth2AuthenticationException 並返回錯誤響應的 AuthenticationFailureHandler (後處理器)。
7 consentPage(): 自定義同意頁面的 URI,如果在裝置驗證請求流程中需要同意,資源所有者將被重定向到此頁面。

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

OAuth2DeviceVerificationEndpointFilter 配置了以下預設值

  • AuthenticationConverter — 由 OAuth2DeviceVerificationAuthenticationConverterOAuth2DeviceAuthorizationConsentAuthenticationConverter 組成的 DelegatingAuthenticationConverter

  • AuthenticationManager — 由 OAuth2DeviceVerificationAuthenticationProviderOAuth2DeviceAuthorizationConsentAuthenticationProvider 組成的 AuthenticationManager

  • AuthenticationSuccessHandler — 一個 SimpleUrlAuthenticationSuccessHandler,用於處理已驗證的 OAuth2DeviceVerificationAuthenticationToken 並將使用者重定向到成功頁面 (/?success)。

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

OAuth2 令牌端點

OAuth2TokenEndpointConfigurer 提供自定義 OAuth2 令牌端點 的能力。它定義了擴充套件點,允許您自定義 OAuth2 訪問令牌請求 的預處理、主要處理和後處理邏輯。

OAuth2TokenEndpointConfigurer 提供以下配置選項

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

	http
		.securityMatcher(authorizationServerConfigurer.getEndpointsMatcher())
		.with(authorizationServerConfigurer, (authorizationServer) ->
			authorizationServer
				.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 (預處理器),用於嘗試將 OAuth2 訪問令牌請求HttpServletRequest 中提取到 OAuth2AuthorizationGrantAuthenticationToken 的例項。
2 accessTokenRequestConverters(): 設定 Consumer,提供對預設和(可選)新增的 AuthenticationConverter 列表的訪問許可權,允許新增、刪除或自定義特定的 AuthenticationConverter
3 authenticationProvider(): 新增一個 AuthenticationProvider (主處理器),用於驗證 OAuth2AuthorizationGrantAuthenticationToken
4 authenticationProviders(): 設定 Consumer,提供對預設和(可選)新增的 AuthenticationProvider 列表的訪問許可權,允許新增、刪除或自定義特定的 AuthenticationProvider
5 accessTokenResponseHandler(): 用於處理 OAuth2AccessTokenAuthenticationToken 並返回 OAuth2AccessTokenResponseAuthenticationSuccessHandler (後處理器)。
6 errorResponseHandler(): 用於處理 OAuth2AuthenticationException 並返回 OAuth2Error 響應AuthenticationFailureHandler (後處理器)。

OAuth2TokenEndpointConfigurer 配置 OAuth2TokenEndpointFilter 並將其註冊到 OAuth2 授權伺服器的 SecurityFilterChain @Bean 中。OAuth2TokenEndpointFilter 是處理 OAuth2 訪問令牌請求的 Filter

支援的 授權 Grant 型別 包括 authorization_coderefresh_tokenclient_credentialsurn:ietf:params:oauth:grant-type:device_codeurn:ietf:params:oauth:grant-type:token_exchange

OAuth2TokenEndpointFilter 配置了以下預設值

  • AuthenticationConverter — 由 OAuth2AuthorizationCodeAuthenticationConverterOAuth2RefreshTokenAuthenticationConverterOAuth2ClientCredentialsAuthenticationConverterOAuth2DeviceCodeAuthenticationConverterOAuth2TokenExchangeAuthenticationConverter 組成的 DelegatingAuthenticationConverter

  • AuthenticationManager — 由 OAuth2AuthorizationCodeAuthenticationProviderOAuth2RefreshTokenAuthenticationProviderOAuth2ClientCredentialsAuthenticationProviderOAuth2DeviceCodeAuthenticationProviderOAuth2TokenExchangeAuthenticationProvider 組成的 AuthenticationManager

  • AuthenticationSuccessHandler — 一個 OAuth2AccessTokenResponseAuthenticationSuccessHandler

  • AuthenticationFailureHandler — 一個 OAuth2ErrorAuthenticationFailureHandler

自定義客戶端憑據 Grant 請求驗證

OAuth2ClientCredentialsAuthenticationValidator 是用於驗證特定 OAuth2 客戶端憑據 Grant 請求引數的預設驗證器。預設實現驗證 scope 引數。如果驗證失敗,則丟擲 OAuth2AuthenticationException

OAuth2ClientCredentialsAuthenticationProvider 允許透過向 setAuthenticationValidator() 提供一個型別為 Consumer<OAuth2ClientCredentialsAuthenticationContext> 的自定義身份驗證驗證器來覆蓋預設的請求驗證。

OAuth2ClientCredentialsAuthenticationContext 包含 OAuth2ClientCredentialsAuthenticationToken,後者包含 OAuth2 客戶端憑據 Grant 請求引數。
如果驗證失敗,身份驗證驗證器必須丟擲 OAuth2AuthenticationException

以下示例展示瞭如何使用覆蓋預設 scope 驗證的自定義身份驗證驗證器配置 OAuth2ClientCredentialsAuthenticationProvider

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

	http
		.securityMatcher(authorizationServerConfigurer.getEndpointsMatcher())
		.with(authorizationServerConfigurer, (authorizationServer) ->
			authorizationServer
				.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 內省端點

OAuth2TokenIntrospectionEndpointConfigurer 提供自定義 OAuth2 Token 內省端點 的能力。它定義了擴充套件點,允許您自定義 OAuth2 內省請求 的預處理、主要處理和後處理邏輯。

OAuth2TokenIntrospectionEndpointConfigurer 提供以下配置選項

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

	http
		.securityMatcher(authorizationServerConfigurer.getEndpointsMatcher())
		.with(authorizationServerConfigurer, (authorizationServer) ->
			authorizationServer
				.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 (預處理器),用於嘗試將 OAuth2 內省請求HttpServletRequest 中提取到 OAuth2TokenIntrospectionAuthenticationToken 的例項。
2 introspectionRequestConverters(): 設定 Consumer,提供對預設和(可選)新增的 AuthenticationConverter 列表的訪問許可權,允許新增、刪除或自定義特定的 AuthenticationConverter
3 authenticationProvider(): 新增一個 AuthenticationProvider (主處理器),用於驗證 OAuth2TokenIntrospectionAuthenticationToken
4 authenticationProviders(): 設定 Consumer,提供對預設和(可選)新增的 AuthenticationProvider 列表的訪問許可權,允許新增、刪除或自定義特定的 AuthenticationProvider
5 introspectionResponseHandler(): 用於處理已驗證的 OAuth2TokenIntrospectionAuthenticationToken 並返回 OAuth2TokenIntrospection 響應AuthenticationSuccessHandler (後處理器)。
6 errorResponseHandler(): 用於處理 OAuth2AuthenticationException 並返回 OAuth2Error 響應AuthenticationFailureHandler (後處理器)。

OAuth2TokenIntrospectionEndpointConfigurer 配置 OAuth2TokenIntrospectionEndpointFilter 並將其註冊到 OAuth2 授權伺服器的 SecurityFilterChain @Bean 中。OAuth2TokenIntrospectionEndpointFilter 是處理 OAuth2 內省請求的 Filter

OAuth2TokenIntrospectionEndpointFilter 配置了以下預設值

  • AuthenticationConverter — 一個 OAuth2TokenIntrospectionAuthenticationConverter

  • AuthenticationManager — 由 OAuth2TokenIntrospectionAuthenticationProvider 組成的 AuthenticationManager

  • AuthenticationSuccessHandler — 一個內部實現,用於處理已驗證的 OAuth2TokenIntrospectionAuthenticationToken 並返回 OAuth2TokenIntrospection 響應。

  • AuthenticationFailureHandler — 一個 OAuth2ErrorAuthenticationFailureHandler

OAuth2 Token 吊銷端點

OAuth2TokenRevocationEndpointConfigurer 提供自定義 OAuth2 Token 吊銷端點 的能力。它定義了擴充套件點,允許您自定義 OAuth2 吊銷請求 的預處理、主要處理和後處理邏輯。

OAuth2TokenRevocationEndpointConfigurer 提供以下配置選項

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

	http
		.securityMatcher(authorizationServerConfigurer.getEndpointsMatcher())
		.with(authorizationServerConfigurer, (authorizationServer) ->
			authorizationServer
				.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 (預處理器),用於嘗試將 OAuth2 吊銷請求HttpServletRequest 中提取到 OAuth2TokenRevocationAuthenticationToken 的例項。
2 revocationRequestConverters(): 設定 Consumer,提供對預設和(可選)新增的 AuthenticationConverter 列表的訪問許可權,允許新增、刪除或自定義特定的 AuthenticationConverter
3 authenticationProvider(): 新增一個 AuthenticationProvider (主處理器),用於驗證 OAuth2TokenRevocationAuthenticationToken
4 authenticationProviders(): 設定 Consumer,提供對預設和(可選)新增的 AuthenticationProvider 列表的訪問許可權,允許新增、刪除或自定義特定的 AuthenticationProvider
5 revocationResponseHandler(): 用於處理已驗證的 OAuth2TokenRevocationAuthenticationToken 並返回 OAuth2 吊銷響應AuthenticationSuccessHandler (後處理器)。
6 errorResponseHandler(): 用於處理 OAuth2AuthenticationException 並返回 OAuth2Error 響應AuthenticationFailureHandler (後處理器)。

OAuth2TokenRevocationEndpointConfigurer 配置 OAuth2TokenRevocationEndpointFilter 並將其註冊到 OAuth2 授權伺服器的 SecurityFilterChain @Bean 中。OAuth2TokenRevocationEndpointFilter 是處理 OAuth2 吊銷請求的 Filter

OAuth2TokenRevocationEndpointFilter 配置了以下預設值

  • AuthenticationConverter — 一個 OAuth2TokenRevocationAuthenticationConverter

  • AuthenticationManager — 由 OAuth2TokenRevocationAuthenticationProvider 組成的 AuthenticationManager

  • AuthenticationSuccessHandler — 一個內部實現,用於處理已驗證的 OAuth2TokenRevocationAuthenticationToken 並返回 OAuth2 吊銷響應。

  • AuthenticationFailureHandler — 一個 OAuth2ErrorAuthenticationFailureHandler

OAuth2 授權伺服器元資料端點

OAuth2AuthorizationServerMetadataEndpointConfigurer 提供自定義 OAuth2 授權伺服器元資料端點 的能力。它定義了一個擴充套件點,允許您自定義 OAuth2 授權伺服器元資料響應

OAuth2AuthorizationServerMetadataEndpointConfigurer 提供以下配置選項

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

	http
		.securityMatcher(authorizationServerConfigurer.getEndpointsMatcher())
		.with(authorizationServerConfigurer, (authorizationServer) ->
			authorizationServer
				.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 端點

OAuth2AuthorizationServerConfigurerJWK Set 端點 提供支援。

OAuth2AuthorizationServerConfigurer 配置 NimbusJwkSetEndpointFilter 並將其註冊到 OAuth2 授權伺服器的 SecurityFilterChain @Bean 中。NimbusJwkSetEndpointFilter 是返回 JWK SetFilter

僅當註冊了 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 =
			OAuth2AuthorizationServerConfigurer.authorizationServer();

	http
		.securityMatcher(authorizationServerConfigurer.getEndpointsMatcher())
		.with(authorizationServerConfigurer, (authorizationServer) ->
			authorizationServer
                .oidc(oidc ->
                    oidc
                        .providerConfigurationEndpoint(providerConfigurationEndpoint ->
                            providerConfigurationEndpoint
                                .providerConfigurationCustomizer(providerConfigurationCustomizer)   (1)
                        )
                )
		);

	return http.build();
}
1 providerConfigurationCustomizer(): 提供對 OidcProviderConfiguration.Builder 的訪問許可權的 Consumer,允許自定義 OpenID Provider 配置的 claim。

OidcProviderConfigurationEndpointConfigurer 配置 OidcProviderConfigurationEndpointFilter 並將其註冊到 OAuth2 授權伺服器的 SecurityFilterChain @Bean 中。OidcProviderConfigurationEndpointFilter 是返回 OidcProviderConfiguration 響應Filter

OpenID Connect 1.0 退出端點

OidcLogoutEndpointConfigurer 提供自定義 OpenID Connect 1.0 退出端點 的能力。它定義了擴充套件點,允許您自定義 RP 發起的退出請求的預處理、主要處理和後處理邏輯。

OidcLogoutEndpointConfigurer 提供以下配置選項

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

	http
		.securityMatcher(authorizationServerConfigurer.getEndpointsMatcher())
		.with(authorizationServerConfigurer, (authorizationServer) ->
			authorizationServer
                .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 列表的訪問許可權,允許新增、刪除或自定義特定的 AuthenticationConverter
3 authenticationProvider(): 新增一個 AuthenticationProvider (主處理器),用於驗證 OidcLogoutAuthenticationToken
4 authenticationProviders(): 設定 Consumer,提供對預設和(可選)新增的 AuthenticationProvider 列表的訪問許可權,允許新增、刪除或自定義特定的 AuthenticationProvider
5 logoutResponseHandler(): 用於處理已驗證的 OidcLogoutAuthenticationToken 並執行退出的 AuthenticationSuccessHandler (後處理器)。
6 errorResponseHandler(): 用於處理 OAuth2AuthenticationException 並返回錯誤響應的 AuthenticationFailureHandler (後處理器)。

OidcLogoutEndpointConfigurer 配置 OidcLogoutEndpointFilter 並將其註冊到 OAuth2 授權伺服器的 SecurityFilterChain @Bean 中。OidcLogoutEndpointFilter 是處理 RP 發起的退出請求 並執行終端使用者退出的 Filter

OidcLogoutEndpointFilter 配置了以下預設值

  • AuthenticationConverter — 一個 OidcLogoutAuthenticationConverter

  • AuthenticationManager — 由 OidcLogoutAuthenticationProvider 組成的 AuthenticationManager

  • AuthenticationSuccessHandler — 一個 OidcLogoutAuthenticationSuccessHandler

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

OidcLogoutAuthenticationProvider 使用 SessionRegistry 查詢與請求退出的終端使用者關聯的 SessionInformation 例項。
OidcClientInitiatedLogoutSuccessHandler 是 Spring Security 的 OAuth2 Client 支援中用於配置 OpenID Connect 1.0 RP 發起的退出 的相應配置。

自定義退出請求驗證

OidcLogoutAuthenticationValidator 是用於驗證特定 OpenID Connect RP 發起的退出請求引數的預設驗證器。預設實現驗證 post_logout_redirect_uri 引數。如果驗證失敗,則丟擲 OAuth2AuthenticationException

OidcLogoutAuthenticationProvider 允許透過向 setAuthenticationValidator() 提供一個型別為 Consumer<OidcLogoutAuthenticationContext> 的自定義身份驗證驗證器來覆蓋預設的退出請求驗證。

OidcLogoutAuthenticationContext 包含 OidcLogoutAuthenticationToken,後者包含退出請求引數。
如果驗證失敗,身份驗證驗證器必須丟擲 OAuth2AuthenticationException

以下示例展示瞭如何使用自定義身份驗證驗證器配置 OidcLogoutAuthenticationProvider

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

	http
		.securityMatcher(authorizationServerConfigurer.getEndpointsMatcher())
		.with(authorizationServerConfigurer, (authorizationServer) ->
			authorizationServer
                .oidc(oidc ->
                    oidc
                        .logoutEndpoint(logoutEndpoint ->
                            logoutEndpoint
                                .authenticationProviders(configureAuthenticationValidator())
                        )
                )
		);

	return http.build();
}

private Consumer<List<AuthenticationProvider>> configureAuthenticationValidator() {
	return (authenticationProviders) ->
			authenticationProviders.forEach((authenticationProvider) -> {
				if (authenticationProvider instanceof OidcLogoutAuthenticationProvider oidcLogoutAuthenticationProvider) {
					Consumer<OidcLogoutAuthenticationContext> authenticationValidator = new CustomPostLogoutRedirectUriValidator();
					oidcLogoutAuthenticationProvider.setAuthenticationValidator(authenticationValidator);
				}
			});
}

static class CustomPostLogoutRedirectUriValidator implements Consumer<OidcLogoutAuthenticationContext> {

	@Override
	public void accept(OidcLogoutAuthenticationContext authenticationContext) {
		OidcLogoutAuthenticationToken oidcLogoutAuthentication =
				authenticationContext.getAuthentication();
		RegisteredClient registeredClient = authenticationContext.getRegisteredClient();

		// TODO

	}
}

OpenID Connect 1.0 UserInfo 端點

OidcUserInfoEndpointConfigurer 提供自定義 OpenID Connect 1.0 UserInfo 端點 的能力。它定義了擴充套件點,允許您自定義 UserInfo 請求 的預處理、主要處理和後處理邏輯。

OidcUserInfoEndpointConfigurer 提供以下配置選項

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

	http
		.securityMatcher(authorizationServerConfigurer.getEndpointsMatcher())
		.with(authorizationServerConfigurer, (authorizationServer) ->
			authorizationServer
                .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 (預處理器),用於嘗試將 UserInfo 請求HttpServletRequest 中提取到 OidcUserInfoAuthenticationToken 的例項。
2 userInfoRequestConverters(): 設定 Consumer,提供對預設和(可選)新增的 AuthenticationConverter 列表的訪問許可權,允許新增、刪除或自定義特定的 AuthenticationConverter
3 authenticationProvider(): 新增一個 AuthenticationProvider (主處理器),用於驗證 OidcUserInfoAuthenticationToken
4 authenticationProviders(): 設定 Consumer,提供對預設和(可選)新增的 AuthenticationProvider 列表的訪問許可權,允許新增、刪除或自定義特定的 AuthenticationProvider
5 userInfoResponseHandler(): 用於處理已驗證的 OidcUserInfoAuthenticationToken 並返回 UserInfo 響應AuthenticationSuccessHandler (後處理器)。
6 errorResponseHandler(): 用於處理 OAuth2AuthenticationException 並返回 UserInfo 錯誤響應AuthenticationFailureHandler (後處理器)。
7 userInfoMapper(): 用於從 OidcUserInfoAuthenticationContext 提取 claim 到 OidcUserInfo 例項的 Function

OidcUserInfoEndpointConfigurer 配置 OidcUserInfoEndpointFilter 並將其註冊到 OAuth2 授權伺服器的 SecurityFilterChain @Bean 中。OidcUserInfoEndpointFilter 是處理 UserInfo 請求 並返回 OidcUserInfo 響應Filter

OidcUserInfoEndpointFilter 配置了以下預設值

  • AuthenticationConverter — 一個內部實現,從 SecurityContext 獲取 Authentication 並使用 principal 建立一個 OidcUserInfoAuthenticationToken

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

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

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

您可以透過提供一個 OAuth2TokenCustomizer<JwtEncodingContext> @Bean 來自定義 ID Token。

OpenID Connect 1.0 UserInfo 端點是一個 OAuth2 保護的資源,要求UserInfo 請求 中傳送訪問令牌作為 bearer token。

OAuth2 資源伺服器支援是自動配置的,但是 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 =
			OAuth2AuthorizationServerConfigurer.authorizationServer();

	http
		.securityMatcher(authorizationServerConfigurer.getEndpointsMatcher())
		.with(authorizationServerConfigurer, (authorizationServer) ->
			authorizationServer
                .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 列表的訪問許可權,允許新增、刪除或自定義特定的 AuthenticationConverter
3 authenticationProvider(): 新增一個 AuthenticationProvider (主處理器),用於驗證 OidcClientRegistrationAuthenticationToken
4 authenticationProviders(): 設定 Consumer,提供對預設和(可選)新增的 AuthenticationProvider 列表的訪問許可權,允許新增、刪除或自定義特定的 AuthenticationProvider
5 clientRegistrationResponseHandler(): 用於處理“已認證”的 OidcClientRegistrationAuthenticationToken 並返回客戶端註冊響應客戶端讀取響應AuthenticationSuccessHandler後處理器)。
6 errorResponseHandler(): 用於處理 OAuth2AuthenticationException 並返回客戶端註冊錯誤響應客戶端讀取錯誤響應AuthenticationFailureHandler後處理器)。
OpenID Connect 1.0 客戶端註冊端點預設是停用的,因為許多部署不需要動態客戶端註冊。

OidcClientRegistrationEndpointConfigurer 配置 OidcClientRegistrationEndpointFilter 並將其註冊到 OAuth2 授權伺服器的 SecurityFilterChain @Bean 中。OidcClientRegistrationEndpointFilter 是處理客戶端註冊請求並返回OidcClientRegistration 響應Filter

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

OidcClientRegistrationEndpointFilter 使用以下預設值進行配置

  • AuthenticationConverter — 一個 OidcClientRegistrationAuthenticationConverter

  • AuthenticationManager — 一個由 OidcClientRegistrationAuthenticationProviderOidcClientConfigurationAuthenticationProvider 組成的 AuthenticationManager

  • AuthenticationSuccessHandler — 一個處理“已認證”的 OidcClientRegistrationAuthenticationToken 並返回 OidcClientRegistration 響應的內部實現。

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

OpenID Connect 1.0 客戶端註冊端點是一個OAuth2 受保護資源,它**要求**在客戶端註冊(或客戶端讀取)請求中以持有者令牌的形式傳送訪問令牌。

OAuth2 資源伺服器支援是自動配置的,但是,OpenID Connect 1.0 客戶端註冊端點**需要**一個 JwtDecoder @Bean
客戶端註冊請求中的訪問令牌**要求** OAuth2 範圍是 client.create
客戶端讀取請求中的訪問令牌**要求** OAuth2 範圍是 client.read