Connect to MongoDB 3.0 with Java Spring

I'm having problems using Spring to access MongoDB with credentials. Although without credentials this works like a charme, using them simply does not allow

Failed to authenticate to database [yourdatabase], username = [yourusername], password = [x******z] 

This should be due to the new auth default, which you can read about at http://docs.mongodb.org/manual/core/authentication/

Changed in version 3.0: SCRAM-SHA-1 is the default mechanism for MongoDB versions starting with the 3.0 series.

Question: Has anyone found out a way to use Spring with credentials? What version of spring-data-mongodb did you use to do the trick?

+7
java spring authentication mongodb
source share
5 answers

After many attempts and reading, I found a way to get MongoDB 3.0 to work with authentication.

This was a new installation of MongoDB 3.0, without an update.

I used these maven dependencies:

 <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-mongodb</artifactId> <version>1.6.2.RELEASE</version> </dependency> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>3.0.0</version> </dependency> 

having a parent

 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.2.2.RELEASE</version> </parent> 

Then in my configuration file I had

 /** * DB connection Factory * * @return a ready to use MongoDbFactory */ @Bean public MongoDbFactory mongoDbFactory() throws Exception { // Set credentials MongoCredential credential = MongoCredential.createCredential(mongoUser, databaseName, mongoPass.toCharArray()); ServerAddress serverAddress = new ServerAddress(mongoHost, mongoPort); // Mongo Client MongoClient mongoClient = new MongoClient(serverAddress,Arrays.asList(credential)); // Mongo DB Factory SimpleMongoDbFactory simpleMongoDbFactory = new SimpleMongoDbFactory( mongoClient, databaseName); return simpleMongoDbFactory; } /** * Template ready to use to operate on the database * * @return Mongo Template ready to use */ @Bean public MongoTemplate mongoTemplate() throws Exception { return new MongoTemplate(mongoDbFactory()); } 

And finally, wherever you have access to the MongoTemplate bean, you can do

 mongoTemplate.insert(objectToStore, collectionName); 
+15
source share

spring.data.mongodb.host and spring.data.mongodb.port not supported if you are using the Mongo 3.0 Java driver. In such cases, spring.data.mongodb.uri should be used to provide the entire configuration, for example:

 spring.data.mongodb.uri=mongodb://user: secret@mongo1.example.com :12345 

Just add spring.data.mongodb.uri to your application.yml and you will get the automatic configuration of MongoDbFactory and MongoTemplate .

+4
source share

using these versions in your pom:

 <!-- mongodb java driver --> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>3.2.1</version> </dependency> <!-- Spring data mongodb --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-mongodb</artifactId> <version>1.8.2.RELEASE</version> </dependency> 

And this configuration in spring:

  <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:mongo="http://www.springframework.org/schema/data/mongo" xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <!-- Seeds --> <bean id="mongoSeedListID" class="java.util.ArrayList"> <constructor-arg> <list> <ref bean="mongoSeedlID" /> </list> </constructor-arg> </bean> <bean id="mongoSeedlID" class="com.mongodb.ServerAddress"> <constructor-arg type="java.lang.String" name="host" value="DATABASE_HOST" /> <constructor-arg type="int" name="port" value="DATABASE_PORT" /> </bean> <!-- Credentials --> <bean id="mongoCredentialListID" class="java.util.ArrayList"> <constructor-arg> <list> <ref bean="mongoCredentialID" /> </list> </constructor-arg> </bean> <bean id="mongoCredentialID" class="com.mongodb.MongoCredential"> <constructor-arg name="mechanism" value = "#{T(com.mongodb.AuthenticationMechanism).SCRAM_SHA_1}" /> <constructor-arg type="java.lang.String" name="userName" value="DATABASE_USERNAME" /> <constructor-arg type="java.lang.String" name="source" value="DATABASE_SOURCE" /> <constructor-arg type="char[]" name="password" value="DB_USER_PASS" /> </bean> <!-- MongoClient --> <bean id="mongoClientID" class="com.mongodb.MongoClient"> <constructor-arg ref="mongoSeedListID" /> <constructor-arg ref="mongoCredentialID" /> </bean> <!-- MongoDbFactory --> <bean id="simpleMongoDbFactoryID" class="org.springframework.data.mongodb.core.SimpleMongoDbFactory"> <constructor-arg ref="mongoClientID" /> <constructor-arg name="databaseName" value="APP_DATABASENAME" /> </bean> <!-- MongoTemplate --> <bean id="mongoTemplateID" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg ref="simpleMongoDbFactoryID" /> </bean> <bean id="log4jInitializationID" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetClass" value="org.springframework.util.Log4jConfigurer" /> <property name="targetMethod" value="initLogging" /> <property name="arguments"> <list> <value>classpath:log4j/log4j_test.properties</value> </list> </property> </bean> </beans> 

In this configuration, you only need to enter MongoTemplate:

  @Autowired @Qualifier("mongoTemplateID") private MongoTemplate mongoTemplate; 

This should work fine =)

+3
source share

Here is the xml version for connecting MongoDB 3.0.7 with Spring (parameters are passed from the properties file):

 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mongo="http://www.springframework.org/schema/data/mongo" xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <mongo:mongo-client host="${mongo.url}" port="${mongo.port}" credentials="${mongo.user}:${mongo.pass}@${mongo.dbname}"> <mongo:client-options write-concern="NORMAL" /> </mongo:mongo-client> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg name="mongo" ref="mongo"/> <constructor-arg name="databaseName" value="${mongo.dbname}"/> </bean> 

Then in Java you will get mongoTemplate as follows:

  @Autowired MongoTemplate mongoTemplate; public String mongoTest() { DBCollection dc = mongoTemplate.getCollection("yourCollection"); logger.debug("--get collection name=" + dc.getFullName()); } 
+1
source share

There are two ways you can get Mongodb 3 to work with Spring data. Both options include lowering the authentication scheme:

  • start with mongodb 2.x and upgrade to 3.0; works like an authentication system, remains the same

  • if you have a new installation of mongo 3.0 installed, you can lower the authentication schemes before creating users.

To lower the authentication mechanism:

 var schema = db.system.version.findOne({"_id" : "authSchema"}) schema.currentVersion = 3 db.system.version.save(schema) 

Or you may have mixed users, some of which are created with version 5 (mongo version 3), and some with version 3. In addition, you can connect (from Spring data) only to users created using version 3.

You can try the 3.0 + mongo 3 beta: in general, this combination works with "outdated code", but I was not able to get it to work with Spring Data.

0
source share

All Articles