caching - 注入(inject)和使用 Spring Security UserCache

标签 caching spring-security spring-boot

这里是 Spring Boot。我正在实现自己的 UserDetailsS​​ervice 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可能就是你想要的。它需要 Cacheconstructor 中传递。该缓存可以类似于 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/

相关文章:

php apc_fetch 所有 id

asp.net - 将在公共(public)计算机或信息亭上使用的 ASP.Net Web 应用程序的安全注意事项

Spring-Boot 不使用 Thymeleaf 加载资源文件夹

java - Spring Boot 序列化 JSON

java - 服务器重启后如何将 HazelcastClient 重新连接到 HazelcastServer

javascript - CSS 未在 Chrome 上加载 - 缓存问题

java - Oauth2 有效 token JWT Spring

java - Spring 安全 : Multiple HTTP Config not working

java - Spring Security SAML2 为它们动态选择 IDP 或动态 URL

spring-boot - 文件和标准输出的 logback 配置