本文介绍了如何捕获MongoSecurityException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在此处验证特定用户的登录详细信息.
I'm trying to validate the login details of a specific user here.
这是行不通的.我不知道为什么,即使有MongoSecurityException
,它也永远不会到达catch块.有人知道为什么吗?
This just won't work. I have no idea why, it never reaches the catch block even though there is a MongoSecurityException
. Does someone know why?
try{
MongoCredential credential = MongoCredential.createCredential("user", "admin",
"password".toCharArray());
ServerAddress address = new ServerAddress("localhost", 27017);
mongoClient = new MongoClient(address, Arrays.asList(credential));
}catch (MongoSecurityException e) {
e.printStackTrace();
}
更新:
堆栈跟踪:
May 24, 2017 1:01:08 AM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
May 24, 2017 1:01:08 AM com.mongodb.diagnostics.logging.JULLogger log
INFO: Exception in monitor thread while connecting to server localhost:27017
com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='test', source='admin', password=<hidden>, mechanismProperties={}}
at com.mongodb.connection.SaslAuthenticator.wrapInMongoSecurityException(SaslAuthenticator.java:157)
at com.mongodb.connection.SaslAuthenticator.access$200(SaslAuthenticator.java:37)
at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:66)
at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:44)
at com.mongodb.connection.SaslAuthenticator.doAsSubject(SaslAuthenticator.java:162)
at com.mongodb.connection.SaslAuthenticator.authenticate(SaslAuthenticator.java:44)
at com.mongodb.connection.DefaultAuthenticator.authenticate(DefaultAuthenticator.java:32)
at com.mongodb.connection.InternalStreamConnectionInitializer.authenticateAll(InternalStreamConnectionInitializer.java:109)
at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:46)
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:116)
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:113)
at java.lang.Thread.run(Thread.java:745)
Caused by: com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server localhost:27017. The full response is { "ok" : 0.0, "errmsg" : "Authentication failed.", "code" : 18, "codeName" : "AuthenticationFailed" }
at com.mongodb.connection.CommandHelper.createCommandFailureException(CommandHelper.java:170)
at com.mongodb.connection.CommandHelper.receiveCommandResult(CommandHelper.java:123)
at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:32)
at com.mongodb.connection.SaslAuthenticator.sendSaslStart(SaslAuthenticator.java:117)
at com.mongodb.connection.SaslAuthenticator.access$000(SaslAuthenticator.java:37)
at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:50)
... 9 more
推荐答案
您无法捕获MongoSecurityException,因为它将在后台线程中抛出.
You cannot catch MongoSecurityException as it is thrown in a background thread.
您可以等待MongoTimeoutException同步处理:
You can wait for a MongoTimeoutException to handle 'synchronously':
MongoClientOptions clientOptions = new MongoClientOptions.Builder().serverSelectionTimeout(500).build();
mongoClient = new MongoClient(serverAddress, Collections.singletonList(credential), clientOptions);
try {
String address = mongoClient.getConnectPoint();
System.out.println(address);
}catch (Throwable e){
System.out.println(e);
}
或者您可以实现ServerListener并异步处理
Or you can implement a ServerListener and handle asynchronously
{
MongoClientOptions clientOptions = new MongoClientOptions.Builder().addServerListener(this).build();
mongoClient = new MongoClient(host1, Collections.singletonList(credential), clientOptions);
}
@Override
public void serverDescriptionChanged(ServerDescriptionChangedEvent event) {
Throwable exception = event.getNewDescription().getException();
handle(exception);
}
这篇关于如何捕获MongoSecurityException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!