当前位置:网站首页>SecurityContextHolder.getContext().getAuthentication().getPrincipal()获取到的是username而不是UserDetails
SecurityContextHolder.getContext().getAuthentication().getPrincipal()获取到的是username而不是UserDetails
2022-06-11 22:34:00 【单筱风】
1.问题引入
我在使用SpringSecurity+JWT做权限认证的时候, @PreAuthorize("@el.check(‘system:user:query’)")
使用上面这个注解判断用户是否有拥有方法级的操作权限,但是在使用下方这个方法获取当前登陆用户时只获取到了用户名,而不是UserDetails对象。
SecurityContextHolder.getContext().getAuthentication().getPrincipal()
2.解决思路
查看登陆成功后是否保存了 Authentication auth
虽然已删除打印,但是测试时是能打印auth的,其中包含UserDetails
@Override
protected void successfulAuthentication(HttpServletRequest req, HttpServletResponse res, FilterChain chain,
Authentication auth) throws IOException, ServletException {
SecurityUser user = (SecurityUser) auth.getPrincipal();
String token = tokenManager.createToken(user.getCurrentUserInfo().getAccount());
SecurityContextHolder.getContext().setAuthentication(auth);
redisUtil.set(user.getCurrentUserInfo().getAccount(),user, GlobalConstant.REDIS_SAVE_TIME);
log.info(" 3.登录成功保存用户达到redis并返回token=="+token);
ResponseUtil.out(res, ResultJson.ok().data(GlobalConstant.ACCESS_TOKEN, token));
}
而下方代码中.getAuthentication().getPrincipal()获取到的是username,导致认证出现问题
/** * 获取当前登录的用户 * @return UserDetails */
public static UserDetails getCurrentUser() {
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null) {
throw new BizException(CommonEnum.STATUS_EXPIRED);
}
if (authentication.getPrincipal() instanceof UserDetails) {
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
UserDetailsService userDetailsService = SpringContextHolder.getBean(UserDetailsService.class);
return userDetailsService.loadUserByUsername(userDetails.getUsername());
}
throw new BizException(CommonEnum.NOT_FIND_LOGIN_INFORMATION);
}
后来才发现登陆授权是没有出问题的,但是当请求拿着token来鉴权的时候却出错了。因为我自己写的token判定过滤器,所以我反复检查代码终于发现问题: user.getUsername()
logger.info("4.更具token访问授权==" + user.getAuthorities());
return new UsernamePasswordAuthenticationToken(user.getUsername(), null, user.getAuthorities());
改成
UsernamePasswordAuthenticationToken(user, null, user.getAuthorities());
就正确了
边栏推荐
- Learn to crawl for a month and earn 6000 a month? Don't be fooled. The teacher told you the truth about the reptile
- 利用SecureCRTPortable脚本功能完成网络设备的数据读取
- Daily question -1317 Converts an integer to the sum of two zero free integers
- Exercise 10-1 judge the three digits that meet the conditions (15 points)
- 启牛推荐开通的证券账户安全吗?靠谱吗
- [JS] 1347- high level usage of localstorage
- 6.项目上线
- [Yu Yue education] basic engineering English of Zhejiang industrial and Commercial University (wuyiping) reference materials
- Computer forced shutdown Oracle login failed
- IEEE754标准中的4种舍入模式
猜你喜欢

Tkinter study notes (III)

Tkinter study notes (II)

Lecture de l'article dense Visual SLAM for RGB - D Cameras

論文閱讀《Dense Visual SLAM for RGB-D Cameras》

动态规划之0-1背包问题(详解+分析+原码)

inner join执行计划变了

电脑强制关机 oracle登录不上

Tkinter学习笔记(三)

Computer forced shutdown Oracle login failed

Basic operation and question type summary of binary tree
随机推荐
还在直接用 localStorage 么?该提升下逼格了
习题6-6 使用函数输出一个整数的逆序数 (20 分)
Correcting high score phrases & sentence patterns
分类统计字符个数 (15 分)
leetcode 257. Binary tree paths all paths to a binary tree (simple)
如果重来一次高考,我要好好学数学!
Start notes under the Astro Pro binocular camera ROS
SequenceList顺序表的实现
一款开源的Markdown转富文本编辑器的实现原理剖析
Neglected technique: bit operation
Submit task to thread pool
How to adjust the font blur of win10
【NodeJs】Electron安装
16 | 浮点数和定点数(下):深入理解浮点数到底有什么用?
STM32开发笔记112:ADS1258驱动设计——读寄存器
0-1 knapsack problem of dynamic programming (detailed explanation + analysis + original code)
STM32 Development Notes 112:ads1258 driver design - read register
IEEE浮点数尾数向偶舍入-四舍六入五成双
astra pro双目相机ros下启动笔记
Players must read starfish NFT advanced introduction