我正在实施在春季安全中记住我。这是我的配置
<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>
我的WMPersistentTokenBasedRememberMeServices看起来像
public class WMPersistentTokenBasedRememberMeServices extends PersistentTokenBasedRememberMeServices {
private static final String key = "WM_APP_KEY";
private static final PersistentTokenRepository tokenRepository = new InMemoryTokenRepositoryImpl();
public WMPersistentTokenBasedRememberMeServices(UserDetailsService userDetailsService) {
super(key, userDetailsService, tokenRepository);
}
public PersistentTokenRepository getTokenRepository() {
return tokenRepository;
}
public UserDetailsService getUserDetailsServiceInstance()
{
return getUserDetailsService();
}
}
但是当我使用上面的配置部署应用程序时,我得到了错误
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的创建顺序。
是否有人对此错误有所了解?
答案 0 :(得分:2)
错误消息说明了所有内容:
Exception: No bean named 'org.springframework.security.web.authentication.rememberme.InMemoryTokenRepositoryImpl@2a13c5e' is defined
令牌存储库不是bean。实际上,如果查看代码,TokenRepository是一个静态字段。而不是使用静态字段,将其声明为bean:在xml配置中,添加如下内容:
<bean id="tokenRepository"
class="org.springframework.security.web.authentication.rememberme.InMemoryTokenRepositoryImpl"/>
并用以下内容替换静态字段:
@Autowired
private PersistentTokenRepository tokenRepository;
Spring作为依赖注入框架,提供了避免使用静态引用的有用机制:只使用这些机制。