From a32cd66179b59961a54c3d2489ccc08019125f84 Mon Sep 17 00:00:00 2001 From: Steve Riesenberg <5248162+sjohnr@users.noreply.github.com> Date: Tue, 26 Dec 2023 10:53:42 -0600 Subject: [PATCH] Polish gh-14263 --- .../CachingUserDetailsService.java | 44 ++--- .../authentication/passwords/cached.adoc | 38 ----- .../authentication/passwords/caching.adoc | 156 ++++++++++++++++++ .../passwords/user-details-service.adoc | 2 +- 4 files changed, 168 insertions(+), 72 deletions(-) delete mode 100644 docs/modules/ROOT/pages/servlet/authentication/passwords/cached.adoc create mode 100644 docs/modules/ROOT/pages/servlet/authentication/passwords/caching.adoc diff --git a/core/src/main/java/org/springframework/security/authentication/CachingUserDetailsService.java b/core/src/main/java/org/springframework/security/authentication/CachingUserDetailsService.java index da6e7de77d..ba8f6d3695 100644 --- a/core/src/main/java/org/springframework/security/authentication/CachingUserDetailsService.java +++ b/core/src/main/java/org/springframework/security/authentication/CachingUserDetailsService.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,51 +26,29 @@ import org.springframework.util.Assert; * Implementation of {@link UserDetailsService} that utilizes caching through a * {@link UserCache} *
- * If a null {@link UserDetails} instance is got from calling + * If a null {@link UserDetails} instance is returned from * {@link UserCache#getUserFromCache(String)} to the {@link UserCache} got from * {@link #getUserCache()}, the user load is deferred to the {@link UserDetailsService} - * provided during construction. Otherwise, the instance got from cache is returned. + * provided during construction. Otherwise, the instance retrieved from the cache is + * returned. *
* It is initialized with a {@link NullUserCache} by default, so it's strongly recommended * setting your own {@link UserCache} using {@link #setUserCache(UserCache)}, otherwise, * the delegate will be called every time. *
- * Utilize this class by defining {@link org.springframework.context.annotation.Bean} that - * encapsulates an actual implementation of {@link UserDetailsService} and set an - * {@link UserCache}. + * Utilize this class by defining a {@link org.springframework.context.annotation.Bean} + * that encapsulates an actual implementation of {@link UserDetailsService} and providing + * a {@link UserCache} implementation. *
- * For example:{@code
+ * For example:
* @Bean
- * public CachingUserDetailsService cachingUserDetailsService(UserDetailsService delegate,
- * UserCache userCache) {
+ * public CachingUserDetailsService cachingUserDetailsService(UserCache userCache) {
+ * UserDetailsService delegate = ...;
* CachingUserDetailsService service = new CachingUserDetailsService(delegate);
* service.setUserCache(userCache);
* return service;
* }
- * }
- *
- *
- * However, a preferable approach would be to use
- * {@link org.springframework.cache.annotation.Cacheable} in your
- * {@link UserDetailsService#loadUserByUsername(String)} implementation to cache
- * {@link UserDetails} by username, reducing boilerplate and setup, specially
- * if you are already using cache in your application.
- *
- *
- * For example:
- *
- * {@code
- * @Service
- * public class MyCustomUserDetailsImplementation implements UserDetailsService {
-
- * @Override
- * @Cacheable
- * public UserDetails loadUserByUsername(String username) {
- * //some logic here to get the actual user details
- * return userDetails;
- * }
- * }
- * }
+ *
*
* @author Luke Taylor
* @since 2.0
diff --git a/docs/modules/ROOT/pages/servlet/authentication/passwords/cached.adoc b/docs/modules/ROOT/pages/servlet/authentication/passwords/cached.adoc
deleted file mode 100644
index 8d58252b6e..0000000000
--- a/docs/modules/ROOT/pages/servlet/authentication/passwords/cached.adoc
+++ /dev/null
@@ -1,38 +0,0 @@
-[[servlet-authentication-cached]]
-= CachingUserDetailsService
-
-Spring Security's `CachingUserDetailsService` implements xref:servlet/authentication/passwords/user-details-service.adoc#servlet-authentication-userdetailsservice[UserDetailsService] offering support for caching authentication.
-
-`CachingUserDetailsService` provides caching support for `UserDetails` by delegating the authentication process to the provided `UserDetailsService`. The result is then stored in a `UserCache` to reduce computation in subsequent calls.
-
-Utilize this class by defining a `@Bean` of it that encapsulates a concrete implementation of `UserDetailsService` and set a `UserCache` to cache authenticated `UserDetails`.
-
-For example:
-
-[source,java]
-----
-@Bean
-public CachingUserDetailsService cachingUserDetailsService(UserDetailsService delegate, UserCache userCache) {
- CachingUserDetailsService service = new CachingUserDetailsService(delegate);
- service.setUserCache(userCache);
- return service;
-}
-----
-
-However, a preferable approach would be to use `@Cacheable` in your `UserDetailsService.loadUserByUsername(String)` implementation to cache `UserDetails` by `username`, reducing boilerplate and setup, especially if you are already using cache in your application.
-
-For example:
-
-[source,java]
-----
-@Service
-public class MyCustomUserDetailsImplementation implements UserDetailsService {
-
- @Override
- @Cacheable
- public UserDetails loadUserByUsername(String username) {
- // some logic here to get the actual user details
- return userDetails;
- }
-}
-----
diff --git a/docs/modules/ROOT/pages/servlet/authentication/passwords/caching.adoc b/docs/modules/ROOT/pages/servlet/authentication/passwords/caching.adoc
new file mode 100644
index 0000000000..9c0d37771e
--- /dev/null
+++ b/docs/modules/ROOT/pages/servlet/authentication/passwords/caching.adoc
@@ -0,0 +1,156 @@
+[[servlet-authentication-caching-user-details]]
+= Caching `UserDetails`
+
+Spring Security provides support for caching `UserDetails` with <