Class ThreadLocalUserProvider

  • All Implemented Interfaces:
    UserProvider

    public class ThreadLocalUserProvider
    extends Object
    implements 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 Detail

      • ThreadLocalUserProvider

        public ThreadLocalUserProvider()
    • Method Detail

      • bind

        public static void bind​(FeatureUser featureUser)
        Store the supplied FeatureUser in the thread context. After calling this method all calls of getCurrentUser() from the active thread will return this feature user. Please don't forget to call release() 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: UserProvider
        Return a FeatureUser instance representing the current user. This method should return null if the implementation is unable to determine the user.
        Specified by:
        getCurrentUser in interface UserProvider
        Returns:
        current user or null