Pet*_*ete 52 authentication passwords salt spring-security
是否有一个教程或有没有人指出如何使用Spring-Security执行以下操作?
任务:
我需要从我的数据库中获取用于验证用户名的salt并使用它来加密提供的密码(从登录页面)以将其与存储的加密密码(也称为用户验证)进行比较.
附加信息:
我使用自定义数据库结构.一个UserDetails
对象是通过自定义创建UserDetailsService
这反过来使用自定义DAOProvider来从数据库中获取信息.
我的security.xml
文件到目前为止:
<authentication-manager>
<authentication-provider user-service-ref="userDetailsService">
</authentication-provider>
</authentication-manager>
Run Code Online (Sandbox Code Playgroud)
现在我想我需要
<password-encoder hash="sha" />
Run Code Online (Sandbox Code Playgroud)
还有什么?如何告诉spring security使用databaseprovided salt来编码密码?
编辑:
我发现这个SO帖子是信息性的,但还不够:如果我在我的xml中定义一个盐源来供密码编码器使用,如下所示:
<password-encoder ref="passwordEncoder">
<salt-source ref="saltSource"/>
</password-encoder>
Run Code Online (Sandbox Code Playgroud)
我将不得不编写一个自定义SaltSource来使用我的自定义盐.但是在UserDetails
对象内部找不到.所以...
备选方案1:
我可以使用可能具有salt属性的UserDetails的自定义实现吗?
<beans:bean id="saltSource" class="path.to.MySaltSource"
p:userPropertyToUse="salt"/>
Run Code Online (Sandbox Code Playgroud)
和
@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {
public UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException, DataAccessException {
// ...
return buildUserFromAccount(account);
}
@Transactional(readOnly = true)
UserDetailsImpl buildUserFromAccount(Account account){
// ... build User object that contains salt property
}
Run Code Online (Sandbox Code Playgroud)
自定义用户类:
public class UserDetailsImpl extends User{
// ...
private String salt;
public String getSalt() { return salt; }
public void setSalt(String salt) { this.salt = salt; }
}
Run Code Online (Sandbox Code Playgroud)
security.xml文件:
<authentication-manager>
<authentication-provider user-service-ref="userDetailsService">
<password-encoder hash="sha">
<salt-source ref="saltSource"/>
</password-encoder>
</authentication-provider>
</authentication-manager>
<beans:bean id="saltSource" class="org.springframework.security.authentication.dao.ReflectionSaltSource" p:userPropertyToUse="salt"/>
Run Code Online (Sandbox Code Playgroud)
备选方案2:
否则,我必须将我的accountDAO注入SaltSource
到userName
从数据库中提取给定的盐.
但是:Spring Security如何调用SaltSource
?总是带着saltSource.getSalt(userDetails)
?
然后我必须确保我在我身上的SaltSource
用途来检索盐.userDetails.accountName
accountDAO
EDIT2:
刚刚得知我的方法是..遗产.. :(所以我想我只会使用StandardPasswordEncoder
(我仍然需要弄清楚如何使用).
顺便说一下:我用自定义的UserDetails类实现了第一个选项,扩展了User类,只添加了一个salt属性,然后将其作为userPropertyToUse传递给SaltSource,就像在Edit 1中提到的SO帖子中提出的那样...
编辑3:
刚刚使用了StandardPasswordEncoder,所以我将在这里留下一些指示:
使用StandardPasswordEncoder进行身份验证:
<beans:bean id="encoder"
class="org.springframework.security.crypto.password.StandardPasswordEncoder">
</beans:bean>
<authentication-manager>
<authentication-provider user-service-ref="userDetailsService">
<password-encoder ref="encoder" />
</authentication-provider>
</authentication-manager>
Run Code Online (Sandbox Code Playgroud)
这需要3.1.0.RC版本中的spring-security-crypto模块?我所知道的.找不到任何具有3.0的存储库.版本(即使在某个地方它列出的版本包括3.0.6等等).此外,文件还谈到了弹簧安全3.1,所以我想,我会接受它.
在创建用户时(对我来说只有管理员可以这样做),我只是使用
StandardPasswordEncoder encoder = new StandardPasswordEncoder();
String result = encoder.encode(password);
Run Code Online (Sandbox Code Playgroud)
我已经完成了
Spring安全性会随机创建一个salt并将其添加到密码字符串中,然后再将其存储到数据库中,这样就不再需要salt列了.
但是,也可以提供全局salt作为构造函数参数(new StandardPasswordEncoder("12345");
),但我不知道如何设置我的安全配置以从bean中检索该值而不是提供静态字符串<constructor-arg name="secret" value "12345" />
.但我不知道还需要多少钱.
Pet*_*ete 34
我将此标记为已回答,因为我解决了我的问题而没有给出任何其他评论或答案:
编辑1 - 备选1回答原始问题
但我必须了解自定义密码salting是Spring Security 3.1中不再需要的遗留方法,正如我所描述的那样
编辑3我在哪里留下了一些关于如何使用StandardPasswordEncoder
与密码一起存储的自动Salts的指针.
无论如何,我写了这篇博文,详细介绍了您所描述的内容: http://rtimothy.tumblr.com/post/26527448708/spring-3-1-security-and-salting-passwords
归档时间: |
|
查看次数: |
37848 次 |
最近记录: |