在最新的 Spring Security 5发布版本中, 出于安全性的考虑调整了PasswordEncoder的实现与使用策略.

1.以前常用的实现 StandardPasswordEncoder, MessageDigestPasswordEncoder, StandardPasswordEncoder 不再推荐使用, 全加上了@Deprecated ,并有具体的说明

Spring Security 5中 PasswordEncoder的使用

推荐使用BCryptPasswordEncoder, Pbkdf2PasswordEncoder, SCryptPasswordEncoder等

 

2.增加 PasswordEncoderFactories 类提供一个静态方法 createDelegatingPasswordEncoder()方法, 使用委托方式创建PasswordEncoder的实现

Spring Security 5中 PasswordEncoder的使用

在此方法的实现中,实际上使用了 BCryptPasswordEncoder 作为默认的实现(我想, 这也是Spring Security团队推荐使用的吧).

 

3.关于加密salt(盐值)的变化. 在之前的使用中, 一般在使用时自己指定salt值,一个配置示例如下:

Spring Security 5中 PasswordEncoder的使用

而在新的使用中, salt的实现 将由各具体实现类去处理(如使用随机生成的值), 以 BCryptPasswordEncoder的实现说明

Spring Security 5中 PasswordEncoder的使用

如图,在进行加密时, 使用随机生成的salt值或由指定的 strength, random去生成.

比如原密码为 admin, 分别进行两次 bcrypt加密的结果如下

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

里面也有 salt 的作用.

 

 

以上为使用 Spring Security 5中 PasswordEncoder的变化总结, 希望对你有用.

当然, 更安全的加密可以使用基于Spring Security提供的类去进行扩展自己的PasswordEncoder实现

原文地址:http://andaily.com/blog/?p=927

相关文章:

  • 2022-12-23
  • 2021-04-28
  • 2021-10-21
  • 2023-01-03
  • 2021-04-26
  • 2022-01-03
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-08-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案