这里是 Spring Boot。我正在实现自己的 UserDetailsService
impl,需要引用/使用 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;
...
}
当我运行我的应用程序时,我收到以下异常消息:
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=)}
有什么想法为什么Spring找不到UserCache
bean吗?
最佳答案
您需要实现 UserCache作为代码中某处的@Bean。
NullUserCache是一个“假”的,实际上不缓存任何内容。看code 。
SpringCacheBasedUserCache可能就是你想要的。它需要 Cache在 constructor 中传递。该缓存可以类似于 ConcurrentMapCache 、EhCacheCache 或 JCacheCache。
我发现 ConcurrentMapCache 非常适合本地开发。
我在当前正在处理的任何事情中都没有使用 UserCache,但这是我的对象缓存配置。在此 @Configuration 文件中,您将看到 2 个配置文件,一个用于本地开发,一个用于 aws 部署。希望这能让您继续前进 - https://gist.github.com/denov/021e9c736d15b1fc56c3e8d185b8ef15
关于caching - 注入(inject)和使用 Spring Security UserCache,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37660032/