具有Hibernate和散列密码的Spring Security DB身份验证?

时间:2010-04-27 21:59:29

标签: hibernate authentication hash passwords spring-security

我正在尝试设置spring security 3来根据我的hibernate 3数据库对用户进行身份验证。我只在数据库中存储密码的sha1哈希值(不是明文)。

我查看了thisthis,它们告诉我实现自己的UserDetailsS​​ervice。不幸的是,loadUserByUsername吐出的UserDetails似乎需要明文密码,我没有。

这通常如何处理? Spring Security可以实际做我需要的吗?我错过了什么吗?

2 个答案:

答案 0 :(得分:3)

当您设置UserDetailsS​​ervice时,spring使用它来加载用户,然后将它们与登录信息进行比较。这意味着,它会比较密码。 但是,您可以配置密码编码器:Doc: Adding a Password Encoder

或者您只需编写自己的AuthenticationManager or AuthenticationProvider来加载用户并决定用户是否已成功登录。只需实现接口 AuthenticationProvider并设置配置

<authentication-manager>
  <authentication-provider ref="myAuthenticationProvider"/>
</authentication-manager>

<bean id="myAuthenticationProvider"
  class="stackoverflow.SuperduperMegaAuthenticationProvider">
</bean>

答案 1 :(得分:2)

Normaly Userdetails包含一个散列密码,您只需要将Spring Security配置为使用正确的password encoder进行身份验证。

 <password-encoder hash="md5"/>

在Stack Overflow answer @ Spring Security 3 database authentication with Hibernate中查找上述密码编码器行。

在您遇到的情况下,您应该将此行替换为:

<password-encoder hash="sha"/>