1
我想散列&盐用户密码与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);
}
这里是Spring配置:
<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()
;
问题:我收到以下错误:与密码模块使用的PasswordEncoder时
盐值必须为null。
看着在stackoverflow后,盐似乎必须为空,因为BCryptPasswordEncoder使用它自己的SaltSource。
- 有没有办法使用我
SaltSource
的方法吗?或 - 哪个可靠的算法可以让我的
SaltSource
?
谢谢。
你想使用自己的盐源的特定原因? – holmis83 2014-11-05 15:01:18
@ holmis83我不知道'BCryptPasswordEncoder'是如何的,但我想为每个用户使用不同的salt,我希望能够在多台服务器上以相同的方式进行salt/check。 – 2014-11-05 15:24:11