SPRINGMVC Mongodb configuration To add user authentication information

Source: Internet
Author: User
Tags mongodb
Add user authentication information in Spring MONGODB configuration

Spring MongoDB Project Setup Please refer to: http://blog.csdn.net/h348592532/article/details/39344823.

In the link to the article in detail, such as the use of a set of Springmvc+mongodb+maven
But inside did not include MongoDB user information configuration, and our actual demand is with the user authentication configuration, do some debugging, configuration well, in fact very simple, the following into the topic. Main content

In the existing configuration, the MongoDB configuration is as follows:

    <mongo:mongo id= "MONGO" replica-set= "${mongo.hostport}" > <mongo:options connections-per- Host= "${mongo.connectionsperhost}" threads-allowed-to-block-for-connection-multiplier= "${mongo.threadsAllowedT Oblockforconnectionmultiplier} "connect-timeout=" ${mongo.connecttimeout} "Max-wait-time=" ${mongo.ma Xwaittime} "auto-connect-retry=" ${mongo.autoconnectretry} "socket-keep-alive=" ${mongo.socketkeepal ive} "socket-timeout=" ${mongo.sockettimeout} "slave-ok=" ${mongo.slaveok} "Write-number = "1" write-timeout= "0" write-fsync= "true"/> </mongo:mongo> <mongo:db-fact Ory dbname= "Database" mongo-ref= "MONGO"/> <bean id= "mongotemplate" class= "ORG.SPRINGFRAMEWORK.DATA.MONGODB.C" Ore. Mongotemplate "> <constructor-arg ref=" Mongo "/> <constructor-arg name=" DatabaseName "value  
= "Db_mongo"/>    </bean>  

Just started trying to add a username password to the MONGO instance, and found that MONGO didn't support it at all, without these two attributes.

Access to relevant information will find that Java to deal with MongoDB connection, the user information certification is placed on the top of the DB block:

    Db.authenticate ("sa", "sa". ToCharArray ());

So, as long as you find out how to get db, you can find the way to configure the username password, where the DB is to obtain the way:

    Mongotemplate.getdb ();

Here, I know the way db is obtained by mongotemplate. Looking back at the configuration file, I found that mongotemplate configured a constructor with two parameters Mongo, DatabaseName, as follows:

    Public Mongotemplate (Mongo Mongo, String databaseName) {This
        (new Simplemongodbfactory (Mongo, databaseName), NULL) ;
    }

Well now, absolutely there is a constructor with user information, found after that, in the configuration inside configuration all of a sudden OK, so find such a:

    Public Mongotemplate (Mongo Mongo, String databaseName, usercredentials usercredentials) {This
        (new Simplemongodbfactory (MONGO, DatabaseName, usercredentials));
    }

So there's the user information. usercredentials , look at this class again, and find that it takes a constructor with a username and password:

    Public Usercredentials (string Username, string password) {
        this.username = Stringutils.hastext (username)? username : null;
        This.password = stringutils.hastext (password)? Password:null;
    }

All right, here's what you need to declare a usercredentials class bean in the spring configuration, and when you're done, take two parameters and then OK:

first declare a user information bean

    <bean id= "usercredentials" class= "Org.springframework.data.authentication.UserCredentials" >
        < Constructor-arg name= "username" value= "${mongo.uname}"/> <constructor-arg name= "password" value=
        Mongo.upass} "/>
    </bean>

Modify the configuration of the mongotemplate and add one more constructor parameter

    <bean id= "Mongotemplate" class= "Org.springframework.data.mongodb.core.MongoTemplate" >
        < Constructor-arg ref= "Mongo"/> <constructor-arg name= "databaseName" value=
        "${mongo.dbname}"/>
        <constructor-arg ref= "Usercredentials"/>
    </bean>

Above, the configuration is complete.

In my own local test is OK, if there is a problem, you can send a message to the oiiopro@live.cn note The way I get DB is done through template .

Mongotemplate.getdb ();
If you don't get a database connection through MONGOTEMPLATE.GETDB (), you need to analyze If you don't get a database connection through MONGOTEMPLATE.GETDB (), you need to analyze If you don't get a database connection through MONGOTEMPLATE.GETDB (), you need to analyze
Related Article

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.