2016-01-09 65 views
0

我實現記住我在春天security.And這裏是我的配置春季安全:無法解析參考豆,同時設置構造函數的參數

<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[email protected]' 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.sec[email protected]2a13c5e' is defined 

當我刪除使用

<security:remember-me ...> 

標籤彈簧表達式語言,並提供有效的用戶參考實例,它工作正常。

但是,當我使用春天的表達語言,它與上述錯誤失敗。我懷疑與bean創建的順序。

任何人有關於這個錯誤的想法?

回答

2

錯誤消息說明了一切:

Exception: No bean named 'org.springframework.sec[email protected]2a13c5e' is defined 

令牌倉庫不是一個bean。事實上,如果你看看你的代碼,TokenRepository是一個靜態字段。而不是使用一個靜態字段,聲明爲一個bean:在你的XML配置,添加類似:

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

並且用類似替換靜態字段:

@Autowired 
private PersistentTokenRepository tokenRepository; 

春天,作爲一個依賴注入框架提供了有用的機制來避免使用靜態引用:只使用這些機制。

+0

即使按照您的建議進行更改,仍然會收到錯誤消息。設置構造函數參數時無法解析對bean#{rememberMeServices.getTokenRepository()}的引用;嵌套異常是org.springframework.beans.factory.NoSuchBeanDefinitionException:沒有名爲'org.springframework.sec[email protected]4d19bf23'的bean。豆類正在創建和自動裝配。我唯一關心的是表達式語言在春季安全標籤中沒有解決。 –

相關問題