这里的问题是您没有以正确的方式实例化WebClient。
由于您位于 客户端
,因此您无权访问
OAuth2AuthorizedClientRepository。该bean应该链接到您使用配置
.oauth2Login()上的方法声明登录到的资源服务器
HttpSecurity。这些细节在这里解释:Spring
Security 5 Oauth2登录。
同样,您在 客户端,
因此需要一个交换筛选器功能,该功能将触发向授权服务器的请求以获取JWT令牌。您可以使用
ServerOAuth2AuthorizedClientExchangeFilterFunction代替。
最好使用a
WebClientCustomizer在WebClient过滤器中添加交换过滤器功能。为什么呢 仅仅因为在Spring应用程序中注入a
WebClient.Builder将使您能够访问链接到Web交换的本机指标。
因此,您将使用
UnAuthenticatedServerOAuth2AuthorizedClientRepository如下所示的bean
的新实例来构建WebClient :
// Use injection to get an in-memory reposiroty or client registrations@BeanWebClient webClient(ClientRegistrationRepository clientRegistrations) { // Provides support for an unauthenticated user such as an application ServerOAuth2AuthorizedClientExchangeFilterFunction oauth = new ServerOAuth2AuthorizedClientExchangeFilterFunction( clientRegistrations, new UnAuthenticatedServerOAuth2AuthorizedClientRepository()); // Build up a new WebClientCustomizer implementation to inject the oauth filter // function into the WebClient.Builder instance return new WebClientSecurityCustomizer(oauth);}正如您在 客户端上一样 ,您没有将用户与您的流程相关联,这就是为什么您不能使用任何授权的客户端存储库bean实例化的原因。查看Spring
Security文档:Spring
Security文档:UnAuthenticatedServerOAuth2AuthorizedClientRepository类。
我试图在以下GitHub项目中总结一个演示案例:GitHub-Spring Security OAuth2 Machine-To-
Machine场景。
我希望这能为您提供有关WebClient配置的更多见解。请问您有什么问题。



