Java Spring安全性:设置构造函数参数时无法解析对bean的引用
我正在SpringSecurity中实现记住我。这是我的配置Java Spring安全性:设置构造函数参数时无法解析对bean的引用,java,spring,spring-security,spring-bean,Java,Spring,Spring Security,Spring Bean,我正在SpringSecurity中实现记住我。这是我的配置 <bean id="rememberMeServices" class="com.spring.security.service.WMPersistentTokenBasedRememberMeServices"> <constructor-arg name="userDetailsService" ref="loginService"/> <prope
<bean id="rememberMeServices"
class="com.spring.security.service.WMPersistentTokenBasedRememberMeServices">
<constructor-arg name="userDetailsService" ref="loginService"/>
<property name="tokenValiditySeconds" value="300"></property>
</bean>
<security:http auto-config="false" use-expressions="true" entry-point-ref="appAuthenticationEntryPoint">
<!-- Interceptor urls -->
<security:remember-me user-service-ref="#{rememberMeServices.getUserDetailsServiceInstance()}" token-repository-ref="#{rememberMeServices.getTokenRepository()}" />
</security:http>
但当我使用上面的配置部署应用程序时,我会发现下面的错误
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#61fc8560': Cannot resolve reference to bean 'org.springframework.security.web.authentication.rememberme.PersistentTokenBasedRememberMeServices#0' while setting bean property 'logoutHandlers' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.authentication.rememberme.PersistentTokenBasedRememberMeServices#0': Cannot resolve reference to bean '#{rememberMeServices.getTokenRepository()}' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinition
Exception: No bean named 'org.springframework.security.web.authentication.rememberme.InMemoryTokenRepositoryImpl@2a13c5e' is defined
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.authentication.rememberme.PersistentTokenBasedRememberMeServices#0': Cannot resolve reference to bean '#{rememberMeServices.getTokenRepository()}' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.springframework.security.web.authentication.rememberme.InMemoryTokenRepositoryImpl@2a13c5e' is defined
在中使用spring表达式语言删除时
<security:remember-me ...>
标记并提供有效的用户引用实例,它工作正常
但当我使用spring表达式语言时,由于上述错误而失败。我怀疑bean创建的顺序
有人知道这个错误吗?错误消息说明了一切:
Exception: No bean named 'org.springframework.security.web.authentication.rememberme.InMemoryTokenRepositoryImpl@2a13c5e' is defined
令牌存储库不是bean。事实上,如果您查看代码,令牌存储库是一个静态字段。与其使用静态字段,不如将其声明为bean:在xml配置中,添加如下内容:
<bean id="tokenRepository"
class="org.springframework.security.web.authentication.rememberme.InMemoryTokenRepositoryImpl"/>
@Autowired
private PersistentTokenRepository tokenRepository;
Spring作为一个依赖项注入框架,提供了一些有用的机制来避免使用静态引用:只需使用这些机制。即使按照您的建议进行了更改,仍然会出现错误。在设置构造函数参数时,无法解析对bean“#{rememberservices.getTokenRepository()}”的引用;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有名为“org.springframework.security.web.authentication.rememberme”的bean。InMemoryTokenRepositoryImpl@4d19bf23'. bean的创建和自动连接都很好。我唯一关心的是表达式语言在spring安全标记中没有解析。
@Autowired
private PersistentTokenRepository tokenRepository;