User authentication information is added to the SPRINGMVC Mongodb configuration

Source: Internet
Author: User

Adding user authentication information to the Spring MONGODB configuration

For project construction of Spring MongoDB, please refer to: http://blog.csdn.net/h348592532/article/details/39344823.

In the linked article in detail, such as building a set of Springmvc+mongodb+maven
But it does not include MongoDB user information configuration, and we actually need to have a user authentication configuration, do some debugging, configuration, actually very simple, the following into the topic.

Main content

In the existing configuration, 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.threadsallowedtoblockforconnectionmultiplier}"Connect-timeout="${mongo.connecttimeout}"            Max-wait-time="${mongo.maxwaittime}"Auto-connect-retry="${mongo.autoconnectretry}"Socket-keep-alive="${mongo.socketkeepalive}"Socket-timeout="${mongo.sockettimeout}"Slave-ok="${mongo.slaveok}"Write-number="1"Write-timeout="0"Write-fsync="true" />      </mongo:mongo>      <Mongo:db-factorydbname="Database"Mongo-ref="MONGO" />      <Bean ID="Mongotemplate"Class="Org.springframework.data.mongodb.core.MongoTemplate">          <Constructor-argRef="MONGO" />          <Constructor-argName="DatabaseName"Value="Db_mongo" />      </bean> 

Just started trying to add the username password on the MONGO instance, and found that MONGO is not supported at all, without these two attributes.

Refer to the relevant data will find that Java to handle MongoDB connection, the user information authentication is placed on the top of the DB block:

db.authenticate("sa", "sa".toCharArray());

So, just find out how to get the db, you can find the way to configure the user name password, here, the DB is obtained by:

mongoTemplate.getDb();

Here, we know how to get the db and get it through mongotemplate. Looking back at the configuration file, I found that mongotemplate configured a constructor with two parameters MONGO, DatabaseName, which is as follows:

public MongoTemplate(Mongo mongo, String databaseName) {    this(new SimpleMongoDbFactory(mongo, databaseName), null);}

Right now, absolutely there is a constructor with user information, found after that, in the configuration inside the configuration is OK, so found a:

public MongoTemplate(Mongo mongo, String databaseName, UserCredentials userCredentials) {    this(new SimpleMongoDbFactory(mongo, databaseName, userCredentials));}

This will have the user information usercredentials , and then look at this class, and found it with a user name, password constructor:

public UserCredentials(String username, String password) {    this.username = StringUtils.hasText(username) ? username : null;    this.password = StringUtils.hasText(password) ? password : null;}

OK, so just declare a bean of the Usercredentials class inside the spring configuration, and after that, put two parameters on it OK:

First, declare a user information bean

 <bean  id  = "usercredentials"  class  =" org.springframework.data.authentication.UserCredentials ";  <constructor-arg  name  = value  = "${mongo.uname}" />  <constructor-arg  name  =" password "  value  = "${mongo.upass}" />  Span class= "Hljs-tag" ></bean ;   

Modify the configuration of the mongotemplate, adding more than one 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 mail to [email protected]

User authentication information is added to the SPRINGMVC Mongodb configuration

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.