0

我的 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”

任何人都可以帮助我正确配置它吗?

4

1 回答 1

6

问题是每个块都创建了自己的 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 元素重用相同的实例。例如:

<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。

于 2013-01-25T20:58:31.490 回答