如何在运行时切换mongo中的身份验证机制?

时间:2016-03-01 11:09:09

标签: java mongodb

我想测试我的MongoClient是否使用默认身份验证机制进行了正确的身份验证,如果不是,我想尝试连接不同的身份验证机制。

我有以下用于打开mongo连接的代码,该代码使用 MONGODB-CR

MongoCredential credential = MongoCredential.createMongoCRCredential(this.user, this.db, this.pass.toCharArray());
ServerAddress serverAddress = new ServerAddress(this.host, this.port.intValue());
MongoClient mongoClient = new MongoClient(serverAddress, Arrays.asList(new MongoCredential[]{credential}));

我需要找到一种方法来测试身份验证是否完整。如果不是我想尝试不同的机制 - SCRAM-SHA-1 。问题是我不知道如何测试连接。调用大多数mongoClient方法会导致MongoCommandException

com.mongodb.MongoSecurityException: Exception authenticating
    at com.mongodb.connection.NativeAuthenticator.authenticate(NativeAuthenticator.java:48)
    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:128)
    at java.lang.Thread.run(Thread.java:745)
Caused by: com.mongodb.MongoCommandException: Command failed with error 18: 'auth failed' on server localhost:27017. The full response is { "ok" : 0.0, "errmsg" : "auth failed", "code" : 18 }
    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.NativeAuthenticator.authenticate(NativeAuthenticator.java:46)
    ... 5 common frames omitted

但实际上这不是例外它只是日志中的信息,所以我无法抓住它。

有关如何测试MongoClient是否已正确验证的任何想法

0 个答案:

没有答案