【问题标题】:Handling salt with spring security PasswordEncoder使用 Spring Security PasswordEncoder 处理盐
【发布时间】:2016-11-28 02:02:45
【问题描述】:
我很惊讶地看到 Spring Security PasswordEncoder(我使用的实现是 BCryptPasswordEncoder,如果这会有所不同)在编码密码时会生成盐。
我不明白的是,在验证登录请求时我应该如何获得这种盐?我打算使用我自己的盐,但是(大概)由于自动生成盐,对于相同的密码+盐组合,我得到不同的哈希值。
我有点迷茫,不知道应该如何正确使用编码器。
【问题讨论】:
标签:
java
spring
spring-security
【解决方案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);