Unable to authenticate user in MongoDB 3.0.2 using java connection

1 - Created user in mongo shell

> use admin switched to db admin > db.createUser( { user:"tiger", pwd:"secret", roles: [ { role:"readWrite", db:"zoo" } ] } ) Successfully added user: { "user" : "tiger", "roles" : [ { "role" : "readWrite", "db" : "zoo" } ] } 

2 - connected to a database with Java

 List<ServerAddress> seeds = new ArrayList<ServerAddress>(); seeds.add( new ServerAddress( "remoteserver" )); List<MongoCredential> credentials = new ArrayList<MongoCredential>(); credentials.add( MongoCredential.createMongoCRCredential( "tiger", "admin", "secret".toCharArray() ) ); MongoClient mongoClient = new MongoClient( seeds); //, credentials ); MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, "zoo"); 

3 - Insert a line

 mongoTemplate.insert(animal, "animal"); 

4 - Get this error (this code works when I do not use authentication)

 2015-05-13 23:11:36.166 ERROR 67846 --- [nio-8443-exec-4] oaccC[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.mongodb.UncategorizedMongoDbException: { "serverUsed" : "remoteserver:27017" , "ok" : 0.0 , "errmsg" : "auth failed" , "code" : 18}; nested exception is com.mongodb.CommandFailureException: { "serverUsed" : "remoteserver:27017" , "ok" : 0.0 , "errmsg" : "auth failed" , "code" : 18}] with root cause com.mongodb.CommandFailureException: { "serverUsed" : "remoteserver:27017" , "ok" : 0.0 , "errmsg" : "auth failed" , "code" : 18} at com.mongodb.CommandResult.getException(CommandResult.java:76) at com.mongodb.CommandResult.throwOnError(CommandResult.java:131) at com.mongodb.DBPort$NativeAuthenticator.authenticate(DBPort.java:652) at com.mongodb.DBPort.authenticate(DBPort.java:364) at com.mongodb.DBPort.checkAuth(DBPort.java:375) at com.mongodb.DBTCPConnector.doOperation(DBTCPConnector.java:206) at com.mongodb.DBCollectionImpl.writeWithCommandProtocol(DBCollectionImpl.java:424) at com.mongodb.DBCollectionImpl.insertWithCommandProtocol(DBCollectionImpl.java:389) at com.mongodb.DBCollectionImpl.insert(DBCollectionImpl.java:188) at com.mongodb.DBCollectionImpl.insert(DBCollectionImpl.java:167) at com.mongodb.DBCollection.insert(DBCollection.java:93) at com.mongodb.DBCollection.insert(DBCollection.java:78) at com.mongodb.DBCollection.insert(DBCollection.java:120) at org.springframework.data.mongodb.core.MongoTemplate$8.doInCollection(MongoTemplate.java:904) at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:410) at org.springframework.data.mongodb.core.MongoTemplate.insertDBObject(MongoTemplate.java:899) at org.springframework.data.mongodb.core.MongoTemplate.doInsert(MongoTemplate.java:721) at org.springframework.data.mongodb.core.MongoTemplate.insert(MongoTemplate.java:676) 
+5
source share
2 answers

Digging a bit, and I found that Mongo 3 does not like the call-answer and prefers MongoCredential.createScramSha1Credential '

 List<ServerAddress> seeds = new ArrayList<ServerAddress>(); seeds.add( new ServerAddress( "remoteServer" )); List<MongoCredential> credentials = new ArrayList<MongoCredential>(); credentials.add( MongoCredential.createScramSha1Credential( "username", "adminDatabase", "password".toCharArray() ) ); MongoClient mongoClient = new MongoClient( seeds, credentials ); MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, "applicationDatabase"); mongoTemplate = new MongoTemplate(mongoDbFactory); 
+15
source

When using the maven dependency with artefactId "spring-boot-starter-data-mongodb" MongoCredential.createScramSha1Credential was not available.

So here is what I had to do:

A) Include the maven dependency on mongodb in the pom.xml file:

 <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>3.4.0</version> </dependency> 

B) Using a solution from user4898172 ( MongoCredential.createScramSha1Credential )

 public Mongo mongo() throws Exception { List<MongoCredential> credentials = new ArrayList<>(); credentials.add(MongoCredential.createScramSha1Credential(username, database, password.toCharArray())); return new MongoClient(new ServerAddress(host, port), credentials); } 

These two changes helped me solve Exeption:

com.mongodb.CommandFailureException: {"serverUsed": "remoteserver: 27017", "ok": 0.0, "errmsg": "auth failed", "code": 18}

(I am using MongoDB server version: 3.4.0)

mongodb github site is also a good reference for implementation details.

0
source

All Articles