このバージョンはまだ開発中であり、まだ安定しているとは見なされていません。最新の安定バージョンについては、Spring Security 6.5.6 を使用してください! |
HTTP サービスクライアントの統合
Spring Security の OAuth サポートは、RestClient および WebClient HTTP サービスクライアントと統合できます。
構成
RestClient または WebClient 固有の構成の後、HTTP サービスクライアントの統合を使用するには、OAuth またはその宣言 HTTP インターフェースを必要とするメソッドに @ClientRegistrationId を追加するだけで済みます。
@ClientRegistrationId の存在によって OAuth トークンが解決されるかどうか、またどのように解決されるかが決まるため、Spring Security の OAuth サポートをどのような構成でも安全に追加できます。
RestClient の設定
Spring Security の OAuth サポートは、RestClient を基盤とする HTTP サービスクライアントと統合できます。最初のステップは、OAuthAuthorizedClientManager Bean を作成することです。
次に、HttpServiceProxyFactory と RestClient が @ClientRegistrationId を認識するように構成する必要があります。この構成を簡素化するには、OAuth2RestClientHttpServiceGroupConfigurer (Javadoc) を使用します。
Java
Kotlin
@Bean
OAuth2RestClientHttpServiceGroupConfigurer securityConfigurer(
OAuth2AuthorizedClientManager manager) {
return OAuth2RestClientHttpServiceGroupConfigurer.from(manager);
}@Bean
fun securityConfigurer(manager: OAuth2AuthorizedClientManager): OAuth2RestClientHttpServiceGroupConfigurer {
return OAuth2RestClientHttpServiceGroupConfigurer.from(manager)
}構成:
ClientRegistrationIdProcessorをHttpServiceProxyFactoryに追加しますRestClientにOAuth2ClientHttpRequestInterceptorを追加
WebClient の設定
Spring Security の OAuth サポートは、WebClient を基盤とする HTTP サービスクライアントと統合できます。最初のステップは、ReactiveOAuthAuthorizedClientManager Bean を作成することです。
次に、HttpServiceProxyFactory と WebRestClient が @ClientRegistrationId を認識するように構成する必要があります。この構成を簡素化するには、OAuth2WebClientHttpServiceGroupConfigurer (Javadoc) を使用します。
Java
Kotlin
@Bean
OAuth2WebClientHttpServiceGroupConfigurer securityConfigurer(
ReactiveOAuth2AuthorizedClientManager manager) {
return OAuth2WebClientHttpServiceGroupConfigurer.from(manager);
}@Bean
fun securityConfigurer(
manager: ReactiveOAuth2AuthorizedClientManager?
): OAuth2WebClientHttpServiceGroupConfigurer {
return OAuth2WebClientHttpServiceGroupConfigurer.from(manager)
}構成:
ClientRegistrationIdProcessorをHttpServiceProxyFactoryに追加しますWebClientにServerOAuth2AuthorizedClientExchangeFilterFunctionを追加
@ClientRegistrationId
HTTP サービスに ClientRegistrationId (Javadoc) を追加して、使用する ClientRegistration (Javadoc) を指定できます。
Java
Kotlin
@GetExchange("/user")
@ClientRegistrationId("github")
User getAuthenticatedUser(); @GetExchange("/user")
@ClientRegistrationId("github")
fun getAuthenticatedUser() : User@ClientRegistrationId は ClientRegistrationIdProcessor によって処理されます
型レベルの宣言
すべてのメソッドで宣言を繰り返さないように、@ClientRegistrationId を型レベルで追加することもできます。
Java
Kotlin
@HttpExchange
@ClientRegistrationId("github")
public interface UserService {
@GetExchange("/user")
User getAuthenticatedUser();
@GetExchange("/users/{username}/hovercard")
Hovercard getHovercard(@PathVariable String username);
}@HttpExchange
@ClientRegistrationId("github")
interface UserService {
@GetExchange("/user")
fun getAuthenticatedUser(): User
@GetExchange("/users/{username}/hovercard")
fun getHovercard(@PathVariable username: String): Hovercard
}ClientRegistrationIdProcessor
各
@ClientRegistrationIdに対してClientAttributes.clientRegistrationId(String)(Javadoc) を自動的に呼び出します。これにより、
ClientRegistration.getId()(Javadoc) が属性に追加されます。
id は次のように処理されます。
OAuth2ClientHttpRequestInterceptorを RestClient 統合に置き換えるWebClientの場合はServletOAuth2AuthorizedClientExchangeFilterFunction(サーブレット) またはServerOAuth2AuthorizedClientExchangeFilterFunction(リアクティブ環境)。