Spring boot 如何将OAuth 2.0承载令牌添加到WebClient

Spring boot 如何将OAuth 2.0承载令牌添加到WebClient,spring-boot,spring-security,spring-webclient,Spring Boot,Spring Security,Spring Webclient,这是我当前使用WebClient时得到的错误响应 { "type": "https://www.jhipster.tech/problem/problem-with-message", "title": "Internal Server Error", "status": 500, "detail": "401 Unauthorized from GE

这是我当前使用
WebClient
时得到的错误响应

{
  "type": "https://www.jhipster.tech/problem/problem-with-message",
  "title": "Internal Server Error",
  "status": 500,
  "detail": "401 Unauthorized from GET http://localhost:8080/services/user/api/users/1",
  "path": "/api/posts",
  "message": "error.http.500"
}
这就是我如何使用
WebClient

webclient.get().uri(ur).retrieve().bodyToMono(User.class).block();

我假设您有一个基于servlet的应用程序,因为您订阅的是
Mono
和阻塞

第一步是使用OAuth 2.0客户端支持配置
WebClient

@Bean
网络客户端网络客户端(OAuth2AuthorizedClientManager授权客户端管理器){
ServletOAuth2AuthorizedClient更改筛选器函数oauth2Client=
新的ServletOAuth2AuthorizedClientChangeFilterFunction(authorizedClientManager);
返回WebClient.builder()
.apply(oauth2Client.oauth2Configuration())
.build();
}
然后可以将
OAuth2AuthorizedClient
设置为请求属性:

String body=this.webClient
.get()
.attributes(clientRegistrationId(“客户端id”))
.retrieve()
.bodyToMono(String.class)
.block();
您可以在中找到更多详细信息

您可以在中找到完整的示例