I am using spring security for authentication and successfully able to get User
object (org.springframework.security.core.userdetails.User
) anywhere I
@RequestMapping(value="/getLogedUserid")
public Long getUserByUsername(HttpServletRequest httpServletRequest) {
HttpSession httpSession = httpServletRequest.getSession();
SecurityContext securityContext = (SecurityContext)
httpSession.getAttribute("SPRING_SECURITY_CONTEXT");
String username = securityContext.getAuthentication().getName();
return userMetier.getUserByUsername(username);
}
here we have the definition of the function getting the id by the username
@Override
public Long getUserByUsername(String Username) {
User user = userRepository.getUserByUsername(Username);
return user.getIdUser();
}