Тествано с mongodb-3.4.2 и mongo-java-driver-3.4.2.jar
(1) Използвайте MongoCredential
MongoCredential credential = MongoCredential.createCredential("user", "database", "passwd".toCharArray());
MongoClient mongoClient = new MongoClient(new ServerAddress("localhost", 27017), Arrays.asList(credential));
MongoDatabase db = mongoClient.getDatabase( "test" );
MongoCollection collection = db.getCollection("mycol");
FindIterable fi = collection.find();
MongoCursor cursor = fi.iterator();
(2) Използвайте MongoClientURI
MongoClientURI uri = new MongoClientURI("mongodb://user:[email protected]:27017/?authSource=test");
MongoClient mongoClient = new MongoClient(uri);
Има някои вариантни форми за използване на MongoCredential и MongoClientURI за различни механизми за удостоверяване, проверете тук за подробности