Reputation: 1186
In my project, I am using spring security. To get the current username I am accessing SpringContextHolder
UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
username = principal.getUsername()
Using org.springframework.security.core.userdetails.UserDetails
object I can only retrieve limited information like username, password, roles etc. What if I want to retrieve user id? Do I need to make database call every time to get more user details corresponding to the username? Can't I set attributes to the session object? In most of the post, solution is proposed by using ModelAndView
object. But In my case, I don't need SessionAttributes for the view layer but also requires at business logic layer.
Mainly I want to persist UserID and ClientID in some way to access whenever and wherever required.
Upvotes: 0
Views: 1577
Reputation: 163
You can write custom UserDetails and UserDetailsService
Spring Security: custom userdetails
Custom UserDetailsService example for spring 3 security
Upvotes: 1