开发者

In Spring how do you get the current user whose credentials are not part of the current http request?

开发者 https://www.devze.com 2023-01-12 19:58 出处:网络
Is there a \"getCurrentUser\" method in Spring to access the user that is currently part of a request - even i开发者_运维百科f that user\'s name is not being passed around as part of a web request?Sin

Is there a "getCurrentUser" method in Spring to access the user that is currently part of a request - even i开发者_运维百科f that user's name is not being passed around as part of a web request?


Since you have tagged your question with spring-security I assume your question is in same context. With spring-security you can retrieve current user as :

Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
UserDetails userDetails = null;
if (principal instanceof UserDetails) {
  userDetails = (UserDetails) principal;
}
String userName = userDetails.getUsername();
0

精彩评论

暂无评论...
验证码 换一张
取 消