当前位置:网站首页>SecurityContextHolder. getContext(). getAuthentication(). Getprincipal() gets username instead of userdetails
SecurityContextHolder. getContext(). getAuthentication(). Getprincipal() gets username instead of userdetails
2022-06-11 22:36:00 【Shan Xiaofeng】
1. Problem introduction
I am using SpringSecurity+JWT When doing authority authentication , @PreAuthorize("@el.check(‘system:user:query’)")
Use the above annotation to determine whether the user has method level operation permission , However, when using the method below to obtain the current login user, only the user name is obtained , instead of UserDetails object .
SecurityContextHolder.getContext().getAuthentication().getPrincipal()
2. Solutions
Check whether it is saved after successful login Authentication auth
Although the print has been deleted , But the test can print auth Of , It includes 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. Login successfully saved the user to redis And back to token=="+token);
ResponseUtil.out(res, ResultJson.ok().data(GlobalConstant.ACCESS_TOKEN, token));
}
And in the code below .getAuthentication().getPrincipal() What you get is username, Cause problems with certification
/** * Get the current login user * @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);
}
Later, I found that there was no problem with the login authorization , But when asked to hold token When I came to authenticate, I made a mistake . Because I wrote it myself token Decision filter , So I checked the code again and again and finally found the problem : user.getUsername()
logger.info("4. More token Access authorization ==" + user.getAuthorities());
return new UsernamePasswordAuthenticationToken(user.getUsername(), null, user.getAuthorities());
Change to
UsernamePasswordAuthenticationToken(user, null, user.getAuthorities());
That's right
边栏推荐
- 重温c语言一
- Dynamics 365 选项集操作
- Are you still using localstorage directly? It's time to raise the bar
- 3.3 naming rules of test modules
- Dynamics 365 option set operation
- 926. 将字符串翻转到单调递增
- leetcode 257. Binary tree paths all paths to a binary tree (simple)
- Exercise 9-6 statistics of student scores by grade (20 points)
- Learn to crawl for a month and earn 6000 a month? Don't be fooled. The teacher told you the truth about the reptile
- Exercise 11-2 find week (15 points)
猜你喜欢

Bit operation in leetcode

图的基本操作(C语言)

2022新兴市场品牌出海线上峰会即将举办 ADVANCE.AI CEO寿栋将受邀出席

Tkinter学习笔记(三)

CloudCompare源码分析:读取ply文件

Explain asynchronous tasks in detail: the task of function calculation triggers de duplication

FastAPI 5 - 常用请求及 postman、curl 使用(parameters,x-www-form-urlencoded, raw)

Simple example of logistic regression for machine learning

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

Basic operation of graph (C language)
随机推荐
The device is in use when win10 ejects USB
Unity3D getLaunchIntentForPackage 获取包返回null问题
【NodeJs】Electron安装
Computer forced shutdown Oracle login failed
Exercise 9-6 statistics of student scores by grade (20 points)
二叉树的基本操作与题型总结
习题8-5 使用函数实现字符串部分复制 (20 分)
习题8-8 判断回文字符串 (20 分)
Dynamics 365 option set operation
[matlab] second order saving response
5. Xuecheng project Alipay payment
电脑强制关机 oracle登录不上
Learn to crawl for a month and earn 6000 a month? Don't be fooled. The teacher told you the truth about the reptile
Basic operation and question type summary of linked list
BUUCTF(5)
Prefabricated dishes in the trillion market have also begun to roll inside. How can brands stand out in the fierce competition?
16 | 浮点数和定点数(下):深入理解浮点数到底有什么用?
LeetCode栈题目总结
STM32 development note 113:ads1258 drive design - reading temperature value
STM32开发笔记112:ADS1258驱动设计——读寄存器