开发者

Spring Security DB Authentication w/Hibernate and hashed passwords?

开发者 https://www.devze.com 2022-12-28 15:22 出处:网络
I\'m trying to set up spring security 3 to authe开发者_如何学JAVAnticate users against my hibernate 3 database. I\'m storing only sha1 hashes of the passwords in the database (not plaintext).

I'm trying to set up spring security 3 to authe开发者_如何学JAVAnticate users against my hibernate 3 database. I'm storing only sha1 hashes of the passwords in the database (not plaintext).

I've looked at this and this, which tell me to implement my own UserDetailsService. Unfortunately, the UserDetails that loadUserByUsername spits out seem to need the plaintext password, which I don't have.

How is this usually handled? Can Spring Security actually do what I need here? Am I missing something?


When you setup an UserDetailsService, spring uses that to load users and then compares them against the login information. That means, it compares the passwords. However, you can configure a password encoder: Doc: Adding a Password Encoder

or you simply write your own AuthenticationManager or AuthenticationProvider which loads the user and decides if the user has logged in successfully. Just implement the Interfaces AuthenticationProvider and set up the config

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

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


Normaly the Userdetails contain a hashed password and you just need to configure Spring Security to use the correct password encoder to authenticate with it.

 <password-encoder hash="md5"/>

Look for the above password-encoder line in Stack Overflow answer @ Spring Security 3 database authentication with Hibernate.

In you're case you should replace this line with:

<password-encoder hash="sha"/>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号