展开
拓展 关闭
订阅号推广码
GitHub
视频
公告栏 关闭

获取用户信息

  • 代码案例
@RestController
@RequestMapping("/user")
public class WebController {
@RequestMapping({"/test1"})
public String index() {
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String username = "";
if(principal != null && principal instanceof UserDetails) {
UserDetails userDetails = (UserDetails)principal;
username = userDetails.getUsername();
}
return username;
}
@RequestMapping("/test2")
@ResponseBody
public Object userInfo(Authentication authentication) {
return authentication.getPrincipal();
}
@RequestMapping("/test3")
@ResponseBody
public Object userInfo2(@AuthenticationPrincipal UserDetails userDetails) {
return userDetails;
}
}
  • 测试结果

posted @   DogLeftover  阅读(17)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
· 面试官:你是如何进行SQL调优的?
点击右上角即可分享
微信分享提示