Package org.togglz.core.user.thread
Class ThreadLocalUserProvider
java.lang.Object
org.togglz.core.user.thread.ThreadLocalUserProvider
- All Implemented Interfaces:
UserProvider
This implementation of
UserProvider
is very useful if authentication has been implemented using a servlet
filter. It allows to store the current user in a ThreadLocal
for 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 -
Method Summary
Modifier and TypeMethodDescriptionstatic void
bind
(FeatureUser featureUser) Store the supplied FeatureUser in the thread context.Return aFeatureUser
instance representing the current user.static void
release()
Removes the user associated with the current thread from the thread's context.
-
Constructor Details
-
ThreadLocalUserProvider
public ThreadLocalUserProvider()
-
-
Method Details
-
bind
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
Description copied from interface:UserProvider
Return aFeatureUser
instance representing the current user. This method should returnnull
if the implementation is unable to determine the user.- Specified by:
getCurrentUser
in interfaceUserProvider
- Returns:
- current user or
null
-