gpt4 book ai didi

java - Spring 安全: Cannot resolve reference to bean while setting constructor argument

转载 作者:行者123 更新时间:2023-12-02 04:03:01 26 4
gpt4 key购买 nike

我正在 spring security 中实现“记住我”。这是我的配置

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

我的 WMPercientTokenBasedRememberMeServices 看起来像

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 创建的顺序表示怀疑。

有人知道这个错误吗?

最佳答案

错误消息说明了一切:

Exception: No bean named 'org.springframework.security.web.authentication.rememberme.InMemoryTokenRepositoryImpl@2a13c5e' is defined

token 存储库不是 bean。事实上,如果您查看代码,就会发现 TokenRepository 是一个静态字段。不要使用静态字段,而是将其声明为 bean:在 xml 配置中添加如下内容:

<bean id="tokenRepository"
class="org.springframework.security.web.authentication.rememberme.InMemoryTokenRepositoryImpl"/>

并将静态字段替换为以下内容:

@Autowired
private PersistentTokenRepository tokenRepository;

Spring 作为一个依赖注入(inject)框架,提供了有用的机制来避免使用静态引用:只需使用这些机制即可。

关于java - Spring 安全: Cannot resolve reference to bean while setting constructor argument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34693438/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com