Package org.togglz.core.user.thread
Class ThreadLocalUserProvider
- java.lang.Object
-
- org.togglz.core.user.thread.ThreadLocalUserProvider
-
- All Implemented Interfaces:
UserProvider
public class ThreadLocalUserProvider extends Object implements UserProvider
This implementation ofUserProvideris very useful if authentication has been implemented using a servlet filter. It allows to store the current user in aThreadLocalfor the active thread. See the following code for an example for how to user this class.FeatureUser user = .... ThreadLocalFeatureUserProvider.bind(user); try { chain.doFilter(request, response); } finally { ThreadLocalFeatureUserProvider.release(); }Please not that it is very important to remove the user from the provider after- Author:
- Christian Kaltepoth
-
-
Constructor Summary
Constructors Constructor Description ThreadLocalUserProvider()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static voidbind(FeatureUser featureUser)Store the supplied FeatureUser in the thread context.FeatureUsergetCurrentUser()Return aFeatureUserinstance representing the current user.static voidrelease()Removes the user associated with the current thread from the thread's context.
-
-
-
Method Detail
-
bind
public static void bind(FeatureUser featureUser)
Store the supplied FeatureUser in the thread context. After calling this method all calls ofgetCurrentUser()from the active thread will return this feature user. Please don't forget to callrelease()before the thread is put back to a thread pool to prevent memory leaks.- Parameters:
featureUser- The feature user to store
-
release
public static void release()
Removes the user associated with the current thread from the thread's context. It's required to always call this method before a thread is put back to a thread pool.
-
getCurrentUser
public FeatureUser getCurrentUser()
Description copied from interface:UserProviderReturn aFeatureUserinstance representing the current user. This method should returnnullif the implementation is unable to determine the user.- Specified by:
getCurrentUserin interfaceUserProvider- Returns:
- current user or
null
-
-