缓存 UserDetails
Spring Security 通过 CachingUserDetailsService 支持缓存 UserDetails。或者,您可以使用 Spring Framework 的 @Cacheable 注解。无论哪种情况,您都需要 禁用凭据清除,以便验证从缓存中检索到的密码。
CachingUserDetailsService
Spring Security 的 CachingUserDetailsService 实现了 UserDetailsService,以支持缓存 UserDetails。CachingUserDetailsService 通过委派给提供的 UserDetailsService 来为 UserDetails 提供缓存支持。结果随后存储在 UserCache 中,以减少后续调用中的计算。
以下示例简单地定义了一个 @Bean,它封装了 UserDetailsService 的具体实现和一个用于缓存 UserDetails 的 UserCache
CachingUserDetailsService @Bean-
Java
-
Kotlin
@Bean
public CachingUserDetailsService cachingUserDetailsService(UserCache userCache) {
UserDetailsService delegate = ...;
CachingUserDetailsService service = new CachingUserDetailsService(delegate);
service.setUserCache(userCache);
return service;
}
@Bean
fun cachingUserDetailsService(userCache: UserCache): CachingUserDetailsService {
val delegate: UserDetailsService = ...
val service = CachingUserDetailsService(delegate)
service.userCache = userCache
return service
}
@Cacheable
另一种方法是在您的 UserDetailsService 实现中使用 Spring Framework 的 @Cacheable 注解来按 username 缓存 UserDetails。这种方法的好处是配置更简单,特别是如果您已经在应用程序的其他地方使用了缓存。
以下示例假设已配置缓存,并使用 @Cacheable 注解 loadUserByUsername
@Cacheable 注解的 UserDetailsService-
Java
-
Kotlin
@Service
public class MyCustomUserDetailsImplementation implements UserDetailsService {
@Override
@Cacheable
public UserDetails loadUserByUsername(String username) {
// some logic here to get the actual user details
return userDetails;
}
}
@Service
class MyCustomUserDetailsImplementation : UserDetailsService {
@Cacheable
override fun loadUserByUsername(username: String): UserDetails {
// some logic here to get the actual user details
return userDetails
}
}
禁用凭据清除
无论您使用 CachingUserDetailsService 还是 @Cacheable,您都需要禁用 凭据清除,以便 UserDetails 在从缓存中检索时包含用于验证的 password。以下示例通过配置 Spring Security 提供的 AuthenticationManagerBuilder 来禁用全局 AuthenticationManager 的凭据清除
AuthenticationManager 的凭据清除-
Java
-
Kotlin
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
// ...
return http.build();
}
@Bean
public UserDetailsService userDetailsService() {
// Return a UserDetailsService that caches users
// ...
}
@Autowired
public void configure(AuthenticationManagerBuilder builder) {
builder.eraseCredentials(false);
}
}
import org.springframework.security.config.annotation.web.invoke
@Configuration
@EnableWebSecurity
class SecurityConfig {
@Bean
fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
// ...
return http.build()
}
@Bean
fun userDetailsService(): UserDetailsService {
// Return a UserDetailsService that caches users
// ...
}
@Autowired
fun configure(builder: AuthenticationManagerBuilder) {
builder.eraseCredentials(false)
}
}