|
23 | 23 | import org.springframework.util.Assert;
|
24 | 24 |
|
25 | 25 | /**
|
| 26 | + * Implementation of {@link UserDetailsService} that utilizes caching through a |
| 27 | + * {@link UserCache} |
| 28 | + * <p> |
| 29 | + * If a null {@link UserDetails} instance is got from calling |
| 30 | + * {@link UserCache#getUserFromCache(String)} to the {@link UserCache} got from |
| 31 | + * {@link #getUserCache()}, the user load is deferred to the {@link UserDetailsService} |
| 32 | + * provided during construction. Otherwise, the instance got from cache is returned. |
| 33 | + * <p> |
| 34 | + * It is initialized with a {@link NullUserCache} by default, so it's strongly recommended |
| 35 | + * setting your own {@link UserCache} using {@link #setUserCache(UserCache)}, otherwise, |
| 36 | + * the delegate will be called every time. |
| 37 | + * <p> |
| 38 | + * Utilize this class by defining {@link org.springframework.context.annotation.Bean} that |
| 39 | + * encapsulates an actual implementation of {@link UserDetailsService} and set an |
| 40 | + * {@link UserCache}. |
| 41 | + * </p> |
| 42 | + * For example: <pre>{@code |
| 43 | + * @Bean |
| 44 | + * public CachingUserDetailsService cachingUserDetailsService(UserDetailsService delegate, |
| 45 | + * UserCache userCache) { |
| 46 | + * CachingUserDetailsService service = new CachingUserDetailsService(delegate); |
| 47 | + * service.setUserCache(userCache); |
| 48 | + * return service; |
| 49 | + * } |
| 50 | + * }</pre> |
| 51 | + * |
| 52 | + * <p> |
| 53 | + * However, a preferable approach would be to use |
| 54 | + * {@link org.springframework.cache.annotation.Cacheable} in your |
| 55 | + * {@link UserDetailsService#loadUserByUsername(String)} implementation to cache |
| 56 | + * {@link UserDetails} by <code>username</code>, reducing boilerplate and setup, specially |
| 57 | + * if you are already using cache in your application. |
| 58 | + * </p> |
| 59 | + * |
| 60 | + * For example: |
| 61 | + * |
| 62 | + * <pre>{@code |
| 63 | + * @Service |
| 64 | + * public class MyCustomUserDetailsImplementation implements UserDetailsService { |
| 65 | +
|
| 66 | + * @Override |
| 67 | + * @Cacheable |
| 68 | + * public UserDetails loadUserByUsername(String username) { |
| 69 | + * //some logic here to get the actual user details |
| 70 | + * return userDetails; |
| 71 | + * } |
| 72 | + * } |
| 73 | + * }</pre> |
| 74 | + * |
26 | 75 | * @author Luke Taylor
|
27 | 76 | * @since 2.0
|
28 | 77 | */
|
|
0 commit comments