2016-07-24 49 views
-1

我很驚訝地看到,Spring Security PasswordEncoder(我使用的實現是BCryptPasswordEncoder,如果這樣做有所不同)在編碼密碼時生成鹽。用彈簧安全處理salt PasswordEncoder

我不明白的是,在驗證登錄請求時我該如何獲得這種鹽?我的意思是使用我自己的鹽,但由於自動鹽生成(大概),我得到不同的哈希值爲相同的密碼+鹽組合。

我有點困惑,不知道如何正確使用編碼器。

回答

1

您應該使用內置驗證邏輯,而不是編寫自己的密碼驗證功能。因此,您不需要獲取Spring Security生成的鹽。請參閱PasswordEncoder中的文檔:

/** 
* Verify the encoded password obtained from storage matches the submitted raw 
* password after it too is encoded. Returns true if the passwords match, false if 
* they do not. The stored password itself is never decoded. 
* 
* @param rawPassword the raw password to encode and match 
* @param encodedPassword the encoded password from storage to compare with 
* @return true if the raw password, after encoding, matches the encoded password from 
* storage 
*/ 
boolean matches(CharSequence rawPassword, String encodedPassword);