2013-03-31 38 views
0

我再次遇到與Spring Security相同的問題:「密碼與存儲值不匹配」。春季安全 - 用戶創建和登錄時不同的SALT

我用我自定義的GraphPopulator類在我的圖表中導入4個帳戶(我使用SpringData/Neo4J),並嘗試使用一個帳戶(「fbiville」/「s3cret」)登錄。

認證配置如下:

<beans:bean id="encoder" class="org.springframework.security.crypto.password.StandardPasswordEncoder"> 
    <beans:constructor-arg value="******" /> 
</beans:bean> 

<beans:bean id="userService" class="com.lateralthoughts.devinlove.service.LoginService" /> 

<authentication-manager> 
    <authentication-provider user-service-ref="userService"> 
     <password-encoder ref="encoder" /> 
    </authentication-provider> 
</authentication-manager> 

和主管堅持賬戶類是部分地基於自定義SpringData倉庫實現:

class PersonRepositoryImpl implements PersonRepositoryCustom { 

    private final PasswordEncoder passwordEncoder; 
    private final Neo4jOperations template; 

    @Autowired 
    public PersonRepositoryImpl(PasswordEncoder passwordEncoder, 
          Neo4jOperations template) { 

     this.passwordEncoder = passwordEncoder; 
     this.template = template; 
    } 

    @Override 
    @Transactional 
    public void persist(Person person) { 
     person.setPassword(passwordEncoder.encode(person.getPassword())); 
     template.save(person); 
    } 
} 

最後,在登錄過程中配置如下:

<http auto-config='true' use-expressions='true' realm="devinlove: love is just another algorithm"> 
    <form-login login-page="/login" 
        default-target-url="/" 
        authentication-failure-url="/login" /> 
    <intercept-url pattern="/login" access="isAnonymous()" /> 
    <intercept-url pattern="/**" access="hasRole('USER')" /> 
</http> 

我調試了標準PasswordEncoder在創建帳戶和用戶登錄時,我注意到salt不匹配,這顯然導致認證錯誤。

如果您想重現問題,可以複製repository

在此先感謝!

羅爾夫

+2

一個指向任意大量的代碼是_not_勝過千言萬語的解釋。這甚至不值得_one_。它補充了一個解釋,因爲編寫解釋的行爲可以幫助你更好地理解問題。 –

+0

你是對的,我的道歉。讓我編輯我的初始信息以提供更多細節。 – Rolf

回答

-1

您需要使用org.springframework.security.authentication.encoding.PasswordEncoder執行,而不是org.springframework.security.crypto.password.PasswordEncoder

所以,你可以簡單地使用作爲如下:

<authentication-manager> 
    <authentication-provider user-service-ref="userService"> 
     <password-encoder hash="sha" base64="true"> 
      <!--Single salt - the very same as you are using in `encoder` instance--> 
      <salt-source system-wide="********"/> 
     </password-encoder> 
    </authentication-provider> 
</authentication-manager> 

注意,這將正確地與@Autowired性工作,雖然不應該有@Qualifier和豆類都只有一次表示。

它公開了這種接口的實現:org.springframework.security.authentication.encoding.PasswordEncoderorg.springframework.security.authentication.dao.SaltSource

所以在您的特定情況下,它看起來像如下:

class PersonRepositoryImpl implements PersonRepositoryCustom { 

    private final PasswordEncoder passwordEncoder; 
    private final Neo4jOperations template; 

    @Autowired 
    public PersonRepositoryImpl(PasswordEncoder passwordEncoder, 
           SaltSource saltSource 
           Neo4jOperations template) { 

     this.passwordEncoder = passwordEncoder; 
     this.saltSource = saltSource; 
     this.template = template; 
    } 

    @Override 
    @Transactional 
    public void persist(Person person) { 
     person.setPassword(passwordEncoder.encode(person.getPassword(), saltSource)); 
     template.save(person); 
    } 
} 
+0

Thx爲您的答案。我用你提到的配置替換了我的配置,並修改了PersonRepositoryImpl以使用正確的Spring Security包,結果仍然是:/ – Rolf

+0

它是否爲相同的密碼返回相同的值?你在數據庫中檢查過嗎? – n1ckolas

+0

我的不好,我誤以某種方式。但是,現在,當我按照所粘貼的配置進行操作時,我無法注入任何PasswordEncoder實例,也無法注入SaltSource實例。你知道什麼豆實際上暴露? – Rolf