【发布时间】:2014-12-31 21:17:19
【问题描述】:
我想使用 bcrypt 和 Spring 安全性对用户密码进行哈希处理和加盐。
这是我的用户模型(我删除了无用的代码):
public class User {
private Integer id;
private String email;
private String hashedPassword;
private String salt; //I want to use this salt.
private Boolean enabled;
//Getters & Setters
}
这是我使用自己的盐创建新用户的方法:
@Transactional
public User create(String email, String password) {
User user = new User();
user.setSalt(BCrypt.gensalt(12)); //Generate salt
user.setEnabled(true);
user.setEmail(email);
user.setHashedPassword(BCrypt.hashpw(password, user.getSalt()));
return dao.persist(user);
}
这里是弹簧配置:
<beans:bean id="userService" class="com.mycompany.service.UserService"/>
<beans:bean id="myCustomUserDetailService" class="com.mycompany.service.MyCustomUserDetailService"/>
<beans:bean id="bcryptEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>
<beans:bean id="saltSource" class="org.springframework.security.authentication.dao.ReflectionSaltSource">
<beans:property name="userPropertyToUse" value="salt"/>
</beans:bean>
<authentication-manager>
<authentication-provider user-service-ref="myCustomUserDetailService">
<password-encoder ref="bcryptEncoder">
<salt-source ref="saltSource"/>
</password-encoder>
</authentication-provider>
</authentication-manager>
在这个配置中,我指出PasswordEncoder必须使用User.getSalt();
问题:我收到以下错误 500:
与加密模块 PasswordEncoder 一起使用时,Salt 值必须为空。
查看stackoverflow后,似乎salt必须为null,因为BCryptPasswordEncoder使用自己的SaltSource。
- 有没有办法使用 my
SaltSource?或 - 哪种可靠算法允许我的
SaltSource?
谢谢。
【问题讨论】:
-
您想使用自己的盐源有什么特别的原因吗?
-
@holmis83 我不知道
BCryptPasswordEncoder如何使用,但我想为每个用户使用不同的盐,并且我希望能够在多台服务器上以相同的方式加盐/检查。 -
你能解决这个问题吗?
标签: java spring-security bcrypt