Recently in a project I faced a situation where there was a need that one java object needs to be accessed by different layers like Controller, Biz, DAO layers etc.
I had a UserInfo object that contains the User information who has logged into the system and his authorities. You can think it as ThreadLocal object containing user information and his authorities. And I want to access it in most of the other spring component classes.
To solve this problem I made one CustomSuccessHandler by extending org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler.
This handler populates the User object in UserInfo class after successful authentication. Below is the structure of UserInfo class.
public class UserInfo
private User ctxUser;
* @return the ctxUser
public User getCtxUser()
* @param ctxUser
* the ctxUser to set
public void setCtxUser(User ctxUser)
this.ctxUser = ctxUser;
You can place additional variables if required. Here User is my own pojo class. It has some fields and getters/setters.
Now to support
In any java file within Spring Context I include below entries in applicationContext.xml or you can do that in any of your context representing xml file.
<bean id="userInfo" class="UserInfo"
<ref bean="userInfo" />
Assuming all your views are going via some controller that is intercepted by spring’s InternalResourceViewResolver
<property name="exposeContextBeansAsAttributes" value="true"/>
In this case you can now access UserInfo bean at class level and use it.
To allow a bean object to be available in your request object below is the configuration for the same class
<bean id='userInfo' class='UserInfo' />
<bean scope="request" factory-bean="userInfo"
Please do let me know if you need more information.