注入和使用Spring Security UserCache

sme*_*eeb 3 caching spring-security spring-boot

Spring Boot在这里.我正在实现我自己的UserDetailsServiceimpl并需要引用/使用Spring提供的UserCache:

package myorg.myapp;

import org.springframework.security.core.userdetails.UserCache;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

@Service
public class MyUserDetailsService implements UserDetailsService {
    @Resource
    private UserCache userCache;

    ...
}
Run Code Online (Sandbox Code Playgroud)

当我去运行我的应用程序时,我收到以下异常消息:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of
type [org.springframework.security.core.userdetails.UserCache] found for dependency: expected at
least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER,
type=class java.lang.Object, mappedName=)}
Run Code Online (Sandbox Code Playgroud)

有什么想法为什么Spring找不到UserCache豆子?

den*_*nov 9

您需要在代码中的某处将UserCache实现为@Bean.

NullUserCache是一个"假"一个实际上并不缓存内容.看看代码.

SpringCacheBasedUserCache可能是你想要的东西.它需要在构造函数中传递Cache.此缓存可以是ConcurrentMapCache,EhCacheCache或JCacheCache.

我发现ConcurrentMapCache非常适合本地开发.

我没有在我正在使用的任何东西中使用UserCache,但这是我的对象缓存配置.在这个@Configuration文件中,您将看到2个配置文件,一个用于本地开发,一个用于aws部署.希望这能让你前进 - https://gist.github.com/denov/021e9c736d15b1fc56c3e8d185b8ef15