Vivek
Vivek

Reputation: 2101

Set User Object in session using Spring

I would like to set the User Object which contains the details of the user into session in my Spring Application.

I would like to use this session object when I would like to retrieve the details of the logged in user on various JSP pages.

I am using Spring 3 and Spring Security 3

I would like to set the User object in the session from my custom authentication class which is not a controller

How can I achieve this?

Upvotes: 2

Views: 2031

Answers (1)

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340693

I assume you have some implementation of UserDetailsService class. You can return any User object implementing UserDetails from loadUserByUsername(). This object is then automatically placed in your HTTP session. It can then be retrieved with:

User user = (User)SecurityContextHolder.
    getContext().getAuthentication().getPrincipal();

Spring Security handles everything you need automatically.

Upvotes: 1

Related Questions