三种获取登陆信息方式
@RequestMapping("/user/info")
@ResponseBody
public Object userInfo(Authentication authentication) {
return authentication.getPrincipal();
}
@RequestMapping("/user/info2")
@ResponseBody
public Object userInfo2(@AuthenticationPrincipal UserDetails userDetails) {
return userDetails;
}
@RequestMapping("/user/info3")
@ResponseBody
public Object userInfo3() {
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if (principal != null && principal instanceof UserDetails) {
UserDetails userDetails = (UserDetails) principal;
return userDetails;
}
return principal;
}
结果:
![image-20210508143455451](https://typora-oss.oss-cn-beijing.aliyuncs.com/image-20210508143455451.png)
![image-20210508143506926](https://typora-oss.oss-cn-beijing.aliyuncs.com/image-20210508143506926.png)
![image-20210508143824512](https://typora-oss.oss-cn-beijing.aliyuncs.com/image-20210508143824512.png)