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

Is there a < getCurrentUser" way in Spring to access a user who is currently part of the request, even if that username is not passed as part of the web request?

+3
source share
1 answer

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

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

Source: https://habr.com/ru/post/1760583/


All Articles