springSecurity修改SecurityUtils.getInstance().getLoginUser()内的登录信息
时间: 2024-05-15 19:12:45 浏览: 14
要修改`SecurityUtils.getInstance().getLoginUser()`内的登录信息,可以使用`SecurityContextHolder`中提供的`Authentication`对象来进行操作。
首先,获取当前用户的`Authentication`对象:
```java
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
```
然后,可以将`Authentication`对象转换成`UsernamePasswordAuthenticationToken`类型,以便进行修改,例如:
```java
if (authentication instanceof UsernamePasswordAuthenticationToken) {
UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) authentication;
// 获取当前用户的登录信息
YourUserDetails userDetails = (YourUserDetails) token.getPrincipal();
// 修改用户信息
userDetails.setXXX(...);
// 将修改后的用户信息重新设置到Authentication对象中
UsernamePasswordAuthenticationToken newToken = new UsernamePasswordAuthenticationToken(userDetails, token.getCredentials(), token.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(newToken);
}
```
这样就可以修改`SecurityUtils.getInstance().getLoginUser()`内的登录信息了。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)