Salt encryption with password MD5 configured in spring security

Source: Internet
Author: User
Tags md5 encryption

In spring security, configure a salt-encrypted service with a password of MD5:

Private Md5passwordencoder encoder; Spring Security MD5

Public Md5passwordencoder Getencoder () {

Return encoder;

}

@Resource

public void Setencoder (Md5passwordencoder encoder) {

This.encoder = encoder;

}

@Override

public void AddUser (user user) {

Assign the encrypted password to user password

Salt is user name

User.setpassword (Encoder.encodepassword (User.getpassword (), User.getname ());

Userdao.adduser (user);

}

Jt.executeupdate (New Stringsql (
"Insertinto vdb_users (Userid,password) VALUES (?,?)",
Uid,new Md5passwordencoder (). Encodepassword (up, UID)));

New Md5passwordencoder (). Encodepassword (Up,uid)

The Md5passwordencoder here is the spring-brought MD5 encryption class, where the first parameter up is a password and the UID is a salt value

Applicationcontext-security.xml:

<!--Configure the authentication manager to achieve the user authentication of the portal, the main implementation of the Userdetailsservice interface can-

<authentication-manageralias= "AuthenticationManager" >

<!--

<authentication-providerref= "Principalprovider" ></authentication-provider>

-

<!--with custom Userdetailservice--

<authentication-provideruser-service-ref= "UserService" >

<!--encrypt your password with MD5-

<password-encoderhash= "MD5" >

<!--salt, according to user name as salt--

<salt-sourceuser-property= "Name"/>

</password-encoder>

<!--

<security:user-service>

<security:username= "Admin"

Password= "2L232F297A57A5A748394A0E4A80LFC3"

authorities= "Role_user"/>

<security:username= "User" password= "user"

authorities= "Role_user"/>

</security:user-service>

-

<!--default Test.user table ... -

<!--

Salt encryption with password MD5 configured in spring security

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.