Spring Security 5中 PasswordEncoder的使用


在最新的 Spring Security 5發布版本中, 出於安全性的考慮調整了PasswordEncoder的實現與使用策略.

1.以前常用的實現 StandardPasswordEncoder, MessageDigestPasswordEncoder, StandardPasswordEncoder 不再推薦使用, 全加上了@Deprecated ,並有具體的說明

1

推薦使用BCryptPasswordEncoder, Pbkdf2PasswordEncoder, SCryptPasswordEncoder等

 

2.增加 PasswordEncoderFactories 類提供一個靜態方法 createDelegatingPasswordEncoder()方法, 使用委托方式創建PasswordEncoder的實現

2

在此方法的實現中,實際上使用了 BCryptPasswordEncoder 作為默認的實現(我想, 這也是Spring Security團隊推薦使用的吧).

 

3.關於加密salt(鹽值)的變化. 在之前的使用中, 一般在使用時自己指定salt值,一個配置示例如下:

3

而在新的使用中, salt的實現 將由各具體實現類去處理(如使用隨機生成的值), 以 BCryptPasswordEncoder的實現說明

4

如圖,在進行加密時, 使用隨機生成的salt值或由指定的 strength, random去生成.

比如原密碼為 admin, 分別進行兩次 bcrypt加密的結果如下

$2a$10$OEaUDkvTlWY/BpoAL7f.H.X7Cz9x3OR3pfWv5wasidP4B7izvyRy6
$2a$10$BBFV5eyg4YoBLThOzi1bDO0WYDBCzcLq3ISokgzxCB/CM0YTXHrha

里面也有 salt 的作用.

 

 

以上為使用 Spring Security 5中 PasswordEncoder的變化總結, 希望對你有用.

當然, 更安全的加密可以使用基於Spring Security提供的類去進行擴展自己的PasswordEncoder實現


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM