What property of the MongoClient can I test to see if it contains an authenticated connection? That's what it boils down to. That is what I can't seem to figure out.
--To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/04cb5fbc-85b9-4908-9014-05f0c8980b4b%40googlegroups.com.
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
package com.mkyong.core;
import java.net.UnknownHostException;
import java.util.Date;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
/**
* Java + MongoDB in Secure Mode
*
*/
public class JavaMongoDBAuthExample {
public static void main(String[] args) {
try {
Mongo mongo = new Mongo("localhost", 27017);
DB db = mongo.getDB("testdb");
boolean auth = db.authenticate("testdb", "password".toCharArray());
if (auth) {
DBCollection table = db.getCollection("user");
BasicDBObject document = new BasicDBObject();
document.put("name", "mkyong");
table.insert(document);
System.out.println("Login is successful!");
} else {
System.out.println("Login is failed!");
}
System.out.println("Done");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
}
}boolean auth = db.authenticate(“testdb”, “password”.toCharArray());
But this is not available in version 3.2. Is there any way to this in 3.2 java driver?
Hi Mohammad,
Normally, the use case for MongoDB is that the credentials represent ‘system’ users and are expected to be correct unless the application was mis-configured. In the case of misconfiguration, a manual intervention would be required to change the credentials.
However if the application is receiving credentials from an untrusted party and therefore expecting failure, should these users be given database credentials ? What are you trying to achieve ?
Kind regards,
Wan.