How to get the current logged in user object from spring security?

SecurityContextHolder.getContext().getAuthentication().getPrincipal();

Returns the current user object. This can be User, UserDetails or your custom user object.
You will need to cast the return object to UserDetails or your own user object if it is a custom one.

OR you can inject Authentication or Principal directly in to your controllers.
Principle is your UserDetails/custom user object.

Note: UserDetails is an interface

Leave a Comment