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 성공은 별개의 결과입니다.