栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Spring Security 5.1-使用WebClient获取客户端凭证流的令牌

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Security 5.1-使用WebClient获取客户端凭证流的令牌

这里的问题是您没有以正确的方式实例化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配置的更多见解。请问您有什么问题。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/466223.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号