murraco/spring-boot-jwt

How to access to current logged user in controllers?

Mistral opened this issue · 1 comments

How can I get access to current logged user in rest controllers? Now I am injecting Principal principal after that I have to load user using principal.getName() and then I can pass to other services User object.

Is there any better solution to inject current User (object) to any methods?

Ok. I found the solution.

  1. User entity should implements UserDetails
  2. In MyUserDetails change loadUserByUsername() method to return our User model, not org.springframework.security.core.userdetails.User. Remember to implement all necessary methods, and make them to return true
  3. Inject User user to your controller method.