spring-boot - 如何将 OAuth 2.0 不记名令牌添加到 WebClient
问题描述
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
和阻塞。
第一步是配置WebClient
OAuth 2.0 客户端支持:
@Bean
WebClient webClient(OAuth2AuthorizedClientManager authorizedClientManager) {
ServletOAuth2AuthorizedClientExchangeFilterFunction oauth2Client =
new ServletOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);
return WebClient.builder()
.apply(oauth2Client.oauth2Configuration())
.build();
}
然后你可以设置一个OAuth2AuthorizedClient
作为请求属性:
String body = this.webClient
.get()
.attributes(clientRegistrationId("client-id"))
.retrieve()
.bodyToMono(String.class)
.block();
您可以在Spring Security 参考文档中找到更多详细信息。
您可以在Spring Security GitHub 存储库中找到完整示例。
推荐阅读
- python - 如何将巨大的 Python 字典存储到 Postgresql 表中
- google-apps-script - Google 脚本中 Google Contacts API 的权限问题
- php - 如何对逗号分隔值进行计算并在 PHP 中恢复逗号
- sas - Proc sql 更新错误 22-322:需要一个名称
- c# - 将选中的已更改侦听器添加到动态添加的复选框 Xamarin Android
- deno - 使用橡木进行 deno 测试仅检查一个文件
- react-native - 反应原生样式表对象不是对象
- flutter - 尽管应用程序正确,但颤动的手机无法访问互联网
- api - 使用 Lambda 调用托管在 AWS EC2 上的 ASP.Net API
- spss - 用于 csv 到 .sav 的 PHP 转换器