Class CachingUserDetailsService

java.lang.Object
org.springframework.security.authentication.CachingUserDetailsService
All Implemented Interfaces:
UserDetailsService

public class CachingUserDetailsService extends Object implements UserDetailsService
Implementation of UserDetailsService that utilizes caching through a UserCache

If a null UserDetails instance is returned from UserCache.getUserFromCache(String) to the UserCache got from getUserCache(), the user load is deferred to the UserDetailsService provided during construction. Otherwise, the instance retrieved from the cache is returned.

It is initialized with a NullUserCache by default, so it's strongly recommended setting your own UserCache using setUserCache(UserCache), otherwise, the delegate will be called every time.

Utilize this class by defining a Bean that encapsulates an actual implementation of UserDetailsService and providing a UserCache implementation.

For example:
 @Bean
 public CachingUserDetailsService cachingUserDetailsService(UserCache userCache) {
     UserDetailsService delegate = ...;
     CachingUserDetailsService service = new CachingUserDetailsService(delegate);
     service.setUserCache(userCache);
     return service;
 }
 
Since:
2.0
  • Constructor Details

    • CachingUserDetailsService

      public CachingUserDetailsService(UserDetailsService delegate)
  • Method Details

    • getUserCache

      public UserCache getUserCache()
    • setUserCache

      public void setUserCache(UserCache userCache)
    • loadUserByUsername

      public UserDetails loadUserByUsername(String username)
      Description copied from interface: UserDetailsService
      Locates the user based on the username. In the actual implementation, the search may possibly be case sensitive, or case insensitive depending on how the implementation instance is configured. In this case, the UserDetails object that comes back may have a username that is of a different case than what was actually requested..
      Specified by:
      loadUserByUsername in interface UserDetailsService
      Parameters:
      username - the username identifying the user whose data is required.
      Returns:
      a fully populated user record (never null)