public static String getCurrentUserId() {
SecurityContext securityContext = SecurityContextHolder.getContext();
Authentication authentication = securityContext.getAuthentication();
String login = null;
if (authentication != null) {
if (authentication.getPrincipal() instanceof UserDetails)
login = ((UserDetails) authentication.getPrincipal()).getUsername();
else if (authentication.getPrincipal() instanceof String)
login = (String) authentication.getPrincipal();
}
return login;
}
I used above way to get user name. But I want to get logged user ID. Can I get that from SecurityUtils.java.
getUserWithAuthorities()
method fromUserService
? Please edit your question with more details like which authentication type you're using and which context you want to get this id. – Gaël Marziou Jan 21 at 13:05