2013-03-12 112 views
0

我介绍了Web应用程序的Spring安全性。首先,我有我的身份验证管理器如下。Spring如何使用盐编码密码

<authentication-manager> 
    <authentication-provider> 
     <password-encoder hash='md5'> 
     </password-encoder> 
     <jdbc-user-service data-source-ref="dataSource"/> 
    </authentication-provider> 
</authentication-manager> 

对于测试,我将使用'1'作为用户名和密码。我使用了一个在线md5哈希生成器,并为md5(1)获得'c4ca4238a0b923820dcc509a6f75849b'。使用此配置登录正常工作。我想尝试盐,我修改身份验证管理器如下。

<authentication-manager> 
    <authentication-provider> 
     <password-encoder hash='md5'> 
      <salt-source user-property="username"/> 
     </password-encoder> 
     <jdbc-user-service data-source-ref="dataSource"/> 
    </authentication-provider> 
</authentication-manager> 

因此,正如我在网络阅读中使用的盐是怎么样的哈希(盐+密码)。因此,使用相同的工具我散列'11',然后得到散列值'6512bd43d9caa6e02c990b0a82652dca'。我用该值更新数据库。但是现在,登录失败,并显示错误“导致:错误凭据”。这意味着密码与数据库不匹配。所以我的问题是,这是否意味着Spring使用不同的方式进行腌制?

+2

使用[BCrypt](http://stackoverflow.com/a/8528804/241990)。它更安全,腌制自己照顾。 MD5对于哈希密码并不是一个好的选择。 – 2013-03-12 17:51:43

+0

应该试试。谢谢卢克 – 2013-03-13 04:55:53

回答

0

该死的无论如何,春季盐腌的方法是不同的。用户遵循Java代码来计算与盐的哈希值。

public static void main(String[] args) { 
    Md5PasswordEncoder md5 = new Md5PasswordEncoder(); 
    String hash = md5.encodePassword("1", "1"); 
    System.out.println(hash); 
} 

我得到了 '6a8a1f634e38d30e87b450899b31f810' 作为加密的密码(右不同?)。然后我将它插入数据库并尝试我的应用程序登录。佛拉!登录成功。

2

由于您使用的是spring安全性,因此您可以考虑使用PasswordEncoder bean。

<beans:bean id="passwordEncoder" class="org.springframework.security.authentication.encoding.ShaPasswordEncoder"> 
</beans:bean> 

和你的认证管理器会是这样(其他城市码):

<authentication-manager> 
    <authentication-provider> 
     <password-encoder ref="passwordEncoder"> 
      <salt-source user-property="username"/> 
     <password-encoder/> 
     <jdbc-user-service data-source-ref="dataSource"/> 
    </authentication-provider> 
</authentication-manager> 

访问Spring Security Custom Authentication and Password Encoding & Spring Security:password encoding in DB and in applicationContext知道更多。

+0

使用财产“哈希”也是这样吗?我认为这与明确创建一个bean是一样的。注入一个org.springframework.security.authentication.encoding.ShaPasswordEncoder作为PasswordEncoder可以轻松做到以下几点吗? <认证管理器> <认证提供商> <密码编码器的散列= “SHA”> <盐源用户属性= “用户名”/> <密码编码器/> 2013-03-13 05:00:46

+0

你就在那里。 – 2013-03-13 05:03:20