简体   繁体   中英

configuration for spring-data-mongo connection with replicaset + credential ( username & password )

I am using spring-data-mongo version 1.6.0.RELEASE. My spring-data-mongo configuration is like

<mongo:mongo id="mongoConnection" replica-set="${mongoDbReplicaSet}">
        <mongo:options socket-timeout="20000" />
</mongo:mongo>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg ref="mongoConnection" />
    <constructor-arg name="databaseName" value="${mongoDbName}" />
</bean>
<mongo:repositories base-package="com.my.package.mongo.repository"
    mongo-template-ref="mongoTemplate">
</mongo:repositories>

where mongoDbReplicaSet=172.229.226.221:17001,172.229.226.222:17001

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-mongodb</artifactId>
    <version>1.6.0.RELEASE</version>
</dependency>

mariadb-java-client-1.6.2.jar
mongo-java-driver-2.12.3.jar

I am looking for configration, how to use credential (username + password) in this configuration with spring-data-mongo version 1.6.0.RELEASE.

I have created a mongo replicaset of 3 nodes(1 primary, 2 secondaries), with secutiry enabled (authentication required)

I am able to connect to mongo-replicaset with following spring configuration and able to read and write records to mongo-replicaset.

<mongo:mongo id="mongo" replica-set="localhost:27041,localhost:27042,localhost:27043"/>
<mongo:db-factory id="mongoDbFactory"
                  mongo-ref="mongo"
                  dbname="testdb"
                  username="testuser"
                  password="testuserpass12"/>
<mongo:template db-factory-ref="mongoDbFactory" id="mongoTemplate"/>

Spring-Data-Mongo version used is

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-mongodb</artifactId>
    <version>1.10.11.RELEASE</version>
</dependency>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM