【问题标题】:No qualifying bean of type [org.springframework.security.web.savedrequest.RequestCache] is defined没有定义 [org.springframework.security.web.savedrequest.RequestCache] 类型的限定 bean
【发布时间】:2013-01-09 20:59:39
【问题描述】:

我的 spring 安全配置中有多个 http 块。从 Spring Security 3.1 版开始允许这样做。当我的 SpringSocial 配置尝试自动注入 RequestCache 对象时,就会出现问题。我收到以下错误:没有定义 [org.springframework.security.web.savedrequest.RequestCache] 类型的限定 bean:预期的单个匹配 bean 但找到了 3。如果我删除 Java 代码中对自动连接的 RequestCache 对象的引用,在我的 xml 配置中有三个 http 块是可以的。修复此错误的最佳方法是什么?有什么方法可以自动连接正确的 RequestCache?

xml http 块:

<http pattern="/oauth/token"...>
</http>

<http pattern="/something/**...>
</http>

<http use-expressions="true"...>
</http>

在 Java Config 中,有一个对自动装配的 RequestCache 的引用:

@Bean
public ProviderSignInController providerSignInController(RequestCache requestCache) {
    return new ProviderSignInController(connectionFactoryLocator(), usersConnectionRepository(), new SimpleSignInAdapter(requestCache, userService));
} 

这些都不会自己造成问题。但是,具有多个 http 块和自动装配的 RequestCache 会导致:“没有定义 [org.springframework.security.web.savedrequest.RequestCache] 类型的合格 bean:预期的单个匹配 bean,但找到了 3”

谁能帮我正确配置它?

【问题讨论】:

    标签: spring spring-security


    【解决方案1】:

    问题是每个块都创建了自己的 RequestCache 实例。由于 RequestCache 的默认实现使用相同的数据存储(即 HttpSession),因此使用新实例仍然可以在块之间工作。但是,当您尝试使用自动装配时,它不知道要使用哪个实例。相反,您创建自己的 HttpSessionRequestCache 实例并使用它(就像 http 块一样)。例如:

    @Bean
    public ProviderSignInController providerSignInController() {
        RequestCache requestCache = new HttpSessionRequestCache();  
        return new ProviderSignInController(connectionFactoryLocator(), usersConnectionRepository(), new SimpleSignInAdapter(requestCache, userService));
    }
    

    另一种可行的方法是在配置中指定 RequestCache 并使用 request-cache element 重用相同的实例。例如:

    <bean:bean class="org.springframework.security.web.savedrequest.HttpSessionRequestCache"
            id="requestCache" />
    
    <http pattern="/oauth/token"...>
        <request-cache ref="requestCache"/>
    </http>
    
    <http pattern="/something/**...>
        <request-cache ref="requestCache"/>
    </http>
    
    <http use-expressions="true"...>
        <request-cache ref="requestCache"/>
    </http>
    

    由于只有一个 RequestCache 实例,您可以使用现有的 Java Config(即自动装配 RequestCache)。请注意,如果您愿意,您可以在 Java Config 中而不是在 XML 中轻松创建 RequestCache。

    【讨论】:

    • 谢谢,可以。我做了类似的事情,通过在 xml 中声明一个空请求缓存和一个 http 请求缓存,添加了 标签,并在代码中显式注入了 http 请求缓存。
    猜你喜欢
    • 2018-07-25
    • 2016-06-28
    • 2016-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多