Spring Reactive OAuth2: プロキシ経由の client_credentials トークン要求
Spring Security 6.5 の client_credentials では、トークン用 WebClient をプロバイダー経由で実際に使われる authorized-client マネージャーに接続します。
プロキシ経由のリソース WebClient は、トークン取得が別のコネクターを使っていると API 呼び出しの前で失敗することがあります。Spring Security 6.5 のリアクティブな client_credentials フローでは、三つの部分をすべて接続します:
HttpClient route = HttpClient.create()
.proxy(p -> p.type(ProxyProvider.Proxy.HTTP)
.host("proxy.example").port(8080));
WebClient tokenHttp = WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(route))
.build();
var tokens = new WebClientReactiveClientCredentialsTokenResponseClient();
tokens.setWebClient(tokenHttp);
var provider = new ClientCredentialsReactiveOAuth2AuthorizedClientProvider();
provider.setAccessTokenResponseClient(tokens);
var manager = new AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager(
registrations, authorizedClients);
manager.setAuthorizedClientProvider(provider);tokens.setWebClient はトークン用 HTTP クライアントを設定し、provider.setAccessTokenResponseClient はそれを grant プロバイダーに設定し、manager.setAuthorizedClientProvider はそのプロバイダーを組み込みます。このマネージャーを実際の OAuth フィルターまたは authorized-client ワークフローで使います。
HttpClient と ProxyProvider は Reactor Netty の型です。WebClient とそのコネクターは Spring WebFlux の型です。既存の ReactiveClientRegistrationRepository を registrations として、ReactiveOAuth2AuthorizedClientService を authorizedClients として渡します。
このプロバイダーは client_credentials を扱います。他の grant が必要なら適切なプロバイダー連鎖を保ってください。例にはプロキシのアカウントがありません。それは OAuth クライアント ID/シークレットではなく転送層に属します。公開の中間者を通すことが許された公開 token endpoint では、HTTP プロキシ一覧からホストとポートを選びます。
これらの API 呼び出しが経路を必要とする場合は、保護リソース側のコネクターも設定します。トークンの成功と API の成功は別の結果です。