Details of spring security Advanced identity authentication Userdetailsservice (attached source)

Source: Internet
Author: User
Tags log4j

In the last spring security post, we used a configuration file to read users from the database and log in. Although the flexibility of this approach is much more flexible than the static account password, it is definitely not a good idea to expose the structure of the database to obvious locations. This article implements the Userdetailsservice interface through Java code to realize the identity authentication.


The role of 1.1 Userdetailsservice in identity authentication


The AuthenticationManager interface, which is authenticated in Spring security, is a default implementation of Providermanager, but it is not used to process identity authentication. Instead, delegate to the configured Authenticationprovider, and each authenticationprovider will take turns checking for identity authentication. After checking or returning an authentication object or throwing an exception.


Verifying the identity is the userdetails of loading the response to see if the user entered the account, password, permissions and other information to match. This step is handled by the Daoauthenticationprovider that implements Authenticationprovider (which uses Userdetailsservice to authenticate the user name, password, and authorization). The Userdetails object containing the grantedauthority fills in the data when the authentication object is built.


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/59/33/wKiom1TJ_G-QennHAALbrutxzyc077.jpg "title=" Userdetailsservice.png "width=" 650 "height=" 374 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" WIDTH:650PX;HEIGHT:374PX; " alt= "Wkiom1tj_g-qennhaalbrutxzyc077.jpg"/>


1.2 Configuring Userdetailsservice


1.2.1 Change the way identities are in Spring-security.xml , use custom Userdetailsservice.


<security:authentication-manager>
<security:authentication-provider user-service-ref= "Favuserdetailservice" >
</security:authentication-provider>
</security:authentication-manager>

<bean id= "Favuserdetailservice" class= "Com.favccxx.favsecurity.security.FavUserDetailService"/>


1.2.2 New Favuserdetailsservice.java to implement Userdetailsservice interface. To reduce the difficulty of learning, this is not integrated with the database, but instead uses impersonation to get the user from the database in a way that authenticates. The sample code is as follows:


Package com.favccxx.favsecurity.security;

Import java.util.ArrayList;
Import java.util.Collection;
Import java.util.List;

Import Org.apache.logging.log4j.LogManager;
Import Org.apache.logging.log4j.Logger;
Import org.springframework.security.core.GrantedAuthority;
Import org.springframework.security.core.authority.SimpleGrantedAuthority;
Import Org.springframework.security.core.userdetails.User;
Import Org.springframework.security.core.userdetails.UserDetails;
Import Org.springframework.security.core.userdetails.UserDetailsService;
Import org.springframework.security.core.userdetails.UsernameNotFoundException;

public class Favuserdetailservice implements Userdetailsservice {

private static final Logger Logger = Logmanager.getlogger (Favuserdetailservice.class);

/**
* Get user-user's role, permissions and other information according to user name
*/
Public userdetails Loaduserbyusername (String username)
Throws Usernamenotfoundexception {
Userdetails userdetails = null;
try {
Com.favccxx.favsecurity.pojo.User favuser = new Com.favccxx.favsecurity.pojo.User ();
Favuser.setusername ("Favccxx");
Favuser.setpassword ("Favccxx");
collection<grantedauthority> authlist = Getauthorities ();
Userdetails = new User (username, Favuser.getpassword (). toLowerCase (), true,true,true,true,authlist);
} catch (Exception e) {
E.printstacktrace ();
}


return userdetails;
}

/**
* Get the user's role permissions, in order to reduce the difficulty of the experiment, here to get rid of the role of the user name step
* @param
* @return
*/
Private Collection<grantedauthority> getauthorities () {
list<grantedauthority> authlist = new arraylist<grantedauthority> ();
Authlist.add (New Simplegrantedauthority ("Role_user"));
Authlist.add (New Simplegrantedauthority ("Role_admin"));

return authlist;
}



}


1.2.3 Start the application server, the following error is generated as long as the username and password are not all favccxx.


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/59/30/wKioL1TJ_giBpILOAAF2QL-ASRg527.jpg "style=" width : 650px;height:199px; "title=" springsecurity userdetailsservice.png "width=" 650 "height=" 199 "border=" 0 "hspace=" 0 " Vspace= "0" alt= "wkiol1tj_gibpiloaaf2ql-asrg527.jpg"/>


User name and password are entered Favccxx, the login is successful


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/59/33/wKiom1TJ_SexYSkWAAHYMLuVe1E670.jpg "style=" width : 650px;height:199px; "title=" Userdetailsservicesuccess.png "width=" 650 "height=" 199 "border=" 0 "hspace=" 0 "vspace=" 0 "alt=" Wkiom1tj_sexyskwaahymluve1e670.jpg "/>


1.3 Tracking Userdetailsservice.


The call flowchart for identity authentication is as follows, and the user can download spring security source code tracking debugging.


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/59/33/wKiom1TJ_WvBCs7aAAHZn6syY1Y174.jpg "title=" Identity verification flowchart. PNG "width=" 650 "height=" 325 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:650px;height:325px; "alt=" Wkiom1tj_wvbcs7aaahzn6syy1y174.jpg "/>


1.4 If it does not work properly, Point here look at the source code. .


This article is from the "Dust Wind with the Sky" blog, please be sure to keep this source http://favccxx.blog.51cto.com/2890523/1609692

Detailed userdetailsservice of spring security advanced authentication (with source code)

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.