Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to MongoDB 3.0 with Java Spring

I am having problems in using Spring to access MongoDB with credentials. While without credentials it works like a charme, using them just fails saying

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

It must be because of new auth default that you can read about in http://docs.mongodb.org/manual/core/authentication/

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

Question: Anybody found out a way to use Spring with credentials ? Which version of spring-data-mongodb did you use to make the trick ?

like image 943
Ing. Luca Stucchi Avatar asked Mar 10 '15 08:03

Ing. Luca Stucchi


People also ask

How do we make the connection to MongoDB in the spring application?

To connect to MongoDB Atlas, we specify the connection string in the application. properties file in the src/main/resources folder. The connection string for a cluster can be found in the Atlas UI. There is no need to write connection-related code in any other file.

Can we connect MongoDB with spring boot?

We need following APIs to work with Spring Boot and MongoDB database. There are two approaches through which we can connect to MongoDB database - MongoRepository and MongoTemplate . We will try to establish what one API offers over another and when should you choose any one of them for your use-case.

Can I connect MongoDB with Java?

Before you start using MongoDB in your Java programs, you need to make sure that you have MongoDB CLIENT and Java set up on the machine. You can check Java tutorial for Java installation on your machine. Now, let us check how to set up MongoDB CLIENT. You need to download the jar mongodb-driver-3.11.

Can I use spring data JPA with MongoDB?

Yes, DataNucleus JPA allows it, as well as to many other databases. You make compromises by using the JPA API for other types of datastores, but it makes it easy to investigate them.


1 Answers

After a lot of attempts and reading, I found a way to make MongoDB 3.0 work with authentication.

This was a new installation of MongoDB 3.0, no upgrade involved.

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 as 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'll be able to do

mongoTemplate.insert(objectToStore, collectionName);
like image 100
Ing. Luca Stucchi Avatar answered Sep 17 '22 13:09

Ing. Luca Stucchi