Spring security developed in Java realizes the authentication function based on mongodb

This paper describes the spring security developed by java to realize the authentication function based on mongodb. Share with you for your reference, as follows:

Spring security's support for database-based authentication is limited to JDBC, and many projects do not use JDBC. For example, many NoSQL databases use Mongo Java driver, so the default < JDBC User Service > cannot be used for supporting authentication.

If the project does not use JDBC, the solution is to define an authentication service.

Create a new customuserdetailsservice class

This class implements the userdetailsservice interface. The code is as follows:

Modify spring security XML file

Login is defined here through the < form login > tag, and its related attributes are described as follows:

Use our customized authentication service through < authentication provider user service ref = "customuserdetailsservice" >

Finally, take a look at login jsp

It can be seen that this login page is no different from that used normally, which is due to the two configurations of username parameter = "phone" and password parameter = "password".

summary

As can be seen from the above code, the only task of the customized user authentication service customuserdetailsservice is to obtain user data from the database according to the user name and generate a userdetails instance.

The userdetails instance contains the user name and password. Spring security obtains the user data in the database from this object and compares it with the user name and password submitted from the form to authenticate the user.

Readers interested in more Java related content can view the topics on this site: introduction and advanced tutorial of spring framework, tutorial of Java data structure and algorithm, summary of Java DOM node operation skills, summary of java file and directory operation skills, and summary of Java cache operation skills

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>