没有定义[org.springframework.security.web.savedrequest.RequestCache]类型的限定bean

Dea*_*son 0 spring spring-security

我的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>
Run Code Online (Sandbox Code Playgroud)

在Java Config中,有一个自动连接的RequestCache的引用:

@Bean
public ProviderSignInController providerSignInController(RequestCache requestCache) {
    return new ProviderSignInController(connectionFactoryLocator(), usersConnectionRepository(), new SimpleSignInAdapter(requestCache, userService));
} 
Run Code Online (Sandbox Code Playgroud)

这些都不会导致问题.但是,拥有多个http块和一个自动装配的RequestCache会导致:"没有定义类型[org.springframework.security.web.savedrequest.RequestCache]的限定bean:预期的单个匹配bean但找到3"

任何人都可以帮我一个正确配置的方法吗?

Rob*_*nch 6

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

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

应该起作用的替代方法是在配置中指定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>
Run Code Online (Sandbox Code Playgroud)

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