2012-08-16 71 views
0

我們已經從3.0.7 spring security遷移到3.1.2,並且我們的一個使用in-memory-config的測試在錯誤憑證上失敗。BadCredentialsException從Spring 3.0.x遷移到3.1.x時

我們不做任何特殊的事情,只需用純文本用戶名和密碼驗證其中一個用戶即可。一旦通過認證,我們就會填充我們的權限。

代碼:

public Authentication authenticate(UserDetails userDetails) 
     throws AuthenticationException { 
    try { 
     org.springframework.security.core.Authentication authenticate = authenticationManager.authenticate(createAuthenticationRequest(userDetails)); 
     if (!authenticate.isAuthenticated()) { 
      throw new AuthenticationException("Authentication failed for user ["+userDetails.getUsername()+"]"); 
     } 

     Collection<? extends GrantedAuthority> grantedAuthorities = authenticate.getAuthorities(); 
        ... 
      } catch(Exception exception) { 
     throw new AuthenticationException(exception); 
    } 

代碼:

<bean id="daoAuthenticationProvider" 
    class="org.springframework.security.authentication.dao.DaoAuthenticationProvider"> 
    <property name="userDetailsService" ref="daoUserDetailsService" /> 
</bean> 

<bean id="daoUserDetailsService" class="org.springframework.security.core.userdetails.memory.InMemoryDaoImpl"> 
    <property name="userMap"> 
     <value> 
      Edward = koala, READ_ONLY 
     </value> 
    </property> 
</bean> 

我們得到一個調用以下異常驗證:

Caused by: org.springframework.security.authentication.BadCre dentialsException: Bad credentials 
at org.springframework.security.authentication.dao.Da oAuthenticationProvider.additionalAuthenticationCh ecks(DaoAuthenticationProvider.java:67) 
at org.springframework.security.authentication.dao.Ab stractUserDetailsAuthenticationProvider.authentica te(AbstractUserDetailsAuthenticationProvider.java: 149) 
at org.springframework.security.authentication.Provid erManager.authenticate(ProviderManager.java:156) 
at org.openspaces.security.spring.SpringSecurityManag er.authenticate(SpringSecurityManager.java:117) 
... 11 more 

任何想法如何解決它或如果有補丁等待解決這個問題?

回答

2

看着你的配置,這可能是一個空白解析問題,但它應該很容易調試,通過在DaoAuthenticationProvider.additionalAuthenticationChecks中放置一個斷點來了解身份驗證失敗的原因。

在任何情況下,用於配置內存中用戶的屬性編輯器方法都被棄用,以支持命名空間配置。你可以使用類似

<security:user-service id="daoUserDetailsService"> 
    <security:user name="Edward" password="koala" authorities="READ_ONLY" /> 
</security:user-service> 

得到相同的結果。當然,您必須將add the security namespace添加到您的應用程序上下文文件中。

+0

謝謝,但它似乎不是問題。 當按照建議進行調試時,我看到以下內容: AbstractUserDetailsAuthenticationProvider 126:UserDetails user = this.userCache.getUserFromCache(username); 返回一個UserDetails,其密碼設置爲空... – 2012-08-16 20:40:23

+0

您是否使用名稱空間嘗試它?如果沒有,那麼這聽起來像你可能看到[這裏描述的問題](https://jira.springsource.org/browse/SEC-1952)。 – 2012-08-17 21:34:45

+0

我嘗試過使用命名空間和類似的結果... – 2012-08-18 19:57:04

0

以下的答案是基於蓋伊Korland的評論(08月16日在'12 20:40),在那裏他做進一步調試:用於擦除的憑據從「假」改爲「真」與

默認值Spring 3.1以後,這就是爲什麼當你的密碼從緩存中被取出時你的密碼是空的。它也解釋了爲什麼你的測試用例在Spring 3.1之前通過。您從緩存中檢索的類是UserDetails,一旦Spring對未加密的密碼進行了驗證,它就不再使用它,因此它將其作爲安全措施進行擦除。對於簡單的測試場景,您可以將erase-credentials值重寫爲'false',但如果在確認身份驗證後確實依賴於未加密的值,則考慮爲長期尋找更安全的解決方案。