经代理访问的资源 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 client ID/secret。对于允许通过公共中介访问的公共 token endpoint,请从HTTP 代理列表中选择主机和端口。

当那些 API 调用需要同一条路由时,也要配置受保护资源的连接器。令牌成功和 API 成功是两个独立结果。

相关文章