0

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.

R. Rahul
  • 1,166
  • 3
  • 16
  • 40

1 Answers1

1

You can write custom UserDetails and UserDetailsService

Spring Security: custom userdetails

Custom UserDetailsService example for spring 3 security

Community
  • 1
  • 1
Vitaliy
  • 163
  • 1
  • 5