我目前正在尝试使用(相对)较新的3.0 Java驱动程序连接到MongoDB副本集。但是,当用户提供错误的凭据时,我似乎无法捕获发生的MongoSecurityExceptions异常。这是我的当前代码。
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());
}
当使用正确的凭据运行此代码时,它可以正常工作,但是当提供错误的凭据时,在try-catch之外会抛出异常。
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)
有任何想法在哪里处理身份验证异常吗?