10

Is it possible to make something like :

MongoClient mongo = new MongoClient(ip, port, usrName, password)

in JAVA similar to the MongoVUE or other SQL based databases' authentication method.

There the authentication is done during connection to DB instance.

I don't see an appropriate instance method in MongoClient java doc

And the way in Authentication (Optional) Official docs

doesn't fit my goals, because it requires to change all the existing query methods in my application which don't use authentication now.

The way in Authenticate to MongoDB with the Java Driver looks exactly what i need, but there's no com.mongodb.MongoCredential class in mongo 2.10.1 distribution.

rok
  • 9,403
  • 17
  • 70
  • 126

3 Answers3

29

You shouldn't need to change all your existing queries, you should only need to change the logic that establishes your MongoClient. Most applications do this as some sort of Singleton so adding authentication is just a matter of modifying the Singleton. It is a pain-in-the-butt that there isn't a signature that takes just String, String for username password, but its the Mongo Java API, get used to disappointment.

You can either go the MongoURI path which gets you the shortest signature...

MongoClient mongo = new MongoClient(
  new MongoClientURI( "mongodb://app_user:bestPo55word3v3r@localhost/data" )
);

Or go with the more verbose List<MongoCredential> path

List<ServerAddress> seeds = new ArrayList<ServerAddress>();
seeds.add( new ServerAddress( "localhost" );
List<MongoCredential> credentials = new ArrayList<MongoCredential>();
credentials.add(
    MongoCredential.createMongoCRCredential(
        "app_user",
        "data",
        "bestPo55word3v3r".toCharArray()
    )
);
MongoClient mongo = new MongoClient( seeds, credentials );
Bob Kuhar
  • 10,838
  • 11
  • 62
  • 115
  • thanks. Can you say, please, which jar contains definition of **MongoCredential** type? – rok Feb 18 '14 at 17:14
  • 1
    MongoClient is in the mongo-java-driver-2.11.3.jar, but the JavaDocs say its been there since 2.10.0. My project gets the JARs through gradle. – Bob Kuhar Feb 18 '14 at 17:19
  • I'm confused. There's MongoClient, but not MongoCredential in mongo-2.10.1.jar. – rok Feb 19 '14 at 15:12
  • Bummer. You're screwed. If you are stuck with 2.10.1 than the MongoClientURI is your only path to Mongo Authentication. Looking again at the JavaDocs, it appears MongoCredential shows up at 2.11.0: http://api.mongodb.org/java/2.11.3/com/mongodb/MongoCredential.html. We are running 2.11.4 here and do our authentication through MongoCredentials. – Bob Kuhar Feb 19 '14 at 17:06
  • @Bob Kuhar: What about the port number `port` in the accepted solution? – Gruber May 25 '15 at 13:46
  • @Gruber I think its the default; 27017. You don't have to specify port if you are using the default, or at least you didn't need to a year ago. – Bob Kuhar May 25 '15 at 18:16
2

Following on from Bob Kuhar's accepted answer, in Mongo3 the mechanism has change to SHA1 from challenge response as shown in the code snippet. I need to update the code snippet as follows:

...
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
...

// Manage the mongo db connection...
List<ServerAddress> seeds = new ArrayList<ServerAddress>();
seeds.add( new ServerAddress(configuration.getMongoHost(), configuration.getMongoPort() ));
List<MongoCredential> credentials = new ArrayList<MongoCredential>();
credentials.add(
    MongoCredential.createScramSha1Credential(
        configuration.getMongoUser(),
        configuration.getMongoDb(),
        configuration.getMongoPassword().toCharArray()
    )
);
MongoClient mongo = new MongoClient( seeds, credentials );
António Ribeiro
  • 4,129
  • 5
  • 32
  • 49
theINtoy
  • 3,388
  • 2
  • 37
  • 60
0

I needed to connect to multiple HOSTs, but also handle authentication:

Using version 3.12:

List<ServerAddress> seeds = new ArrayList<>();
seeds.add(new ServerAddress("localhost"))

credential = MongoCredential.createScramSha1Credential(
      user,
      db,
      pass.toCharArray()
);

mongoClient = MongoClients.create(
      MongoClientSettings.builder()
           .applyToClusterSettings(builder -> 
                 builder.hosts(seeds))
           .credential(credential)
           .build());
hestellezg
  • 3,309
  • 3
  • 33
  • 37