Spring security talking about user authentication mechanism

Source: Internet
Author: User

Step1: First applicationuserdetailsservice need to implement Userdetailsservice interface (in

Org.springframework.security.core.userdetails), the callback function that obtains the user details information must be implemented

Loaduserbyusername method, note that the user here implements the parameters of the Userdetails,credentialscontainer,user class, respectively:

Password,username,authorities,accountnonexpired,accountnonlocked,credentialsnonexpired,enable,

The code is as follows:

We can customize the addition of new user information, where we customize a Simpleuser class to inherit the user.

The code is as follows:

Note that the simpleuser must inherit the user, and must inherit the above 5 properties and the Hashcode,equals method, which must be given to these 5

Property assignment, Applicationuserdetailsservice calls the constructor method to generate a Simpleuser object. If the user does not exist, throw a

Usernamenotfoundexception exception.

The code is as follows:

When the Step2:loaduserbyusername method returns, it jumps to Daoauthenticationprovider (in

Org.springframework.security.authentication.dao) class in the Retrieveuser method

When the Step3:retrieveuser method returns, it jumps to Abstractuserdetailsauthenticationprovider (in

Org.springframework.security.authentication.dao) Authenticate method, this method will first look up the user from the cache.

If not, the user is checked out from the previous step of the Daoauthenticationprovider Loaduserbyusername method, which throws

Usernamenotfoundexception This exception, should be the user does not exist this error, that is bad credentials.

Step4:authenticate method Preauthenticationchecks.check (user), where basic validation is performed (valid, whether

is locked, expired) code is as follows:

In the Step5:authenticate method additionalauthenticationchecks (user,

(Usernamepasswordauthenticationtoken) authentication) method validation is performed here. The code is as follows:

Spring security talking about user authentication mechanism

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.