Verify MongoDB Authentication Using Java 3.0 Driver

I'm currently trying to connect to the MongoDB replica set using the (relatively) new Java 3.0 driver. However, it seems that I cannot catch the MongoSecurityExceptions that occur when the user provides bad credentials. This is my current code.

try {
    MongoClientURI mongoClientURI = new MongoClientURI("mongodb://<user>:<password>@member1.com:27017/?authSource=db"
    this.mongoClient = new MongoClient(mongoClientURI);
}
catch(Exception e) {
    // TODO: some proper exception handling
    System.err.println(e.toLocalizedMessage());
}

This code works fine when run with the correct credentials, but an exception is thrown outside of try-catch when bad credentials are provided.

com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='<user>', source='<source>', password=<hidden>, mechanismProperties={}}
at com.mongodb.connection.SaslAuthenticator.authenticate(SaslAuthenticator.java:61)
at com.mongodb.connection.DefaultAuthenticator.authenticate(DefaultAuthenticator.java:32)
at com.mongodb.connection.InternalStreamConnectionInitializer.authenticateAll(InternalStreamConnectionInitializer.java:99)
at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:44)
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115)
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:127)
at java.lang.Thread.run(Thread.java:745)

Any idea where to handle authentication exceptions?

+3
source share
1 answer

MongoClient . , , .

, MongoClient MongoDB, . -, , . :

    MongoClient client = new MongoClient(asList(new ServerAddress("localhost"), new ServerAddress("localhost:27018")),
                                         singletonList(MongoCredential.createCredential("username",
                                                                                        "admin",
                                                                                        "bad".toCharArray())),
                                         MongoClientOptions.builder().serverSelectionTimeout(1000).build());


    try {
        client.getDB("admin").command("ping");
    } catch (MongoTimeoutException e) {
        // do something
    }

MongoTimeoutException 1 . MongoSecurityException , MongoTimeoutException . , , , , MongoTimeout :

1000 , ReadPreferenceServerSelector {readPreference = }. : {type = UNKNOWN, servers = [{address = localhost: 27017, type = UNKNOWN, state = CONNECTING, exception = {com.mongodb.MongoSocketOpenException: socket}, {java.net.ConnectException: }}, {address = localhost: 27018, type = UNKNOWN, state = CONNECTING, exception = {com.mongodb.MongoSecurityException: MongoCredential { = null, userName = 'username', source = 'admin', password =, Properties = {}}}, {com.mongodb.MongoCommandException: 18: " ". localhost: 27018. ( "ok": 0.0, "code": 18, "errmsg": " ". }}}]

+5

Source: https://habr.com/ru/post/1613360/


All Articles