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

如何将OAuth 2令牌映射到资源服务器中的UserDetails对象?

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

如何将OAuth 2令牌映射到资源服务器中的UserDetails对象?

为了解决这个问题,让我先介绍一下建筑背景。通过进行

UserDetails
自动映射的对象
@AuthenticationPrincipal
来自
principal
活动
Authentication
对象的字段。资源服务器控制器仅通过将其声明为方法参数的一部分即可访问
OAuth2Authencation
对象,该对象是
Authentication
Spring
OAuth2安全框架的专用实例。

public void controllerMethod(OAuth2Authentication authentication) {    //controller definition}

知道了这一点,问题现在转移到如何确保对象中的

getPrincipal()
方法
Authentication
是我的自定义
UserDetails
类的实例。将
RemoteTokenServices
在资源服务器应用程序使用我的使用实例
AccessTokenConverter
来解释该授权服务器发送令牌的详细信息。默认情况下,它使用
DefaultAccessTokenConverter
,它只是将身份验证主体设置为用户名,即用户名
String
。此转换器利用
UserAuthenticationConverter
将来自授权服务器的数据转换为的实例
Authentication
。这是我需要自定义的内容:

DefaultAccessTokenConverter tokenConverter = new DefaultAccessTokenConverter();tokenConverter.setUserTokenConverter(new DefaultUserAuthenticationConverter() {    @Override    public Authentication extractAuthentication(Map<String, ?> map) {        Authentication authentication = super.extractAuthentication(map);        // User is my custom UserDetails class        User user = new User();        user.setSpecialKey(map.get("specialKey").toString());        return new UsernamePasswordAuthenticationToken(user,     authentication.getCredentials(), authentication.getAuthorities());    }});tokenServices.setAccessTokenConverter(tokenConverter);

通过所有这些设置,该

@AuthenticationPrincipal
机制现在可以按预期工作。



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

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

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