簡體   English   中英

使用Java 3.0驅動程序檢查MongoDB身份驗證

[英]Check MongoDB authentication with Java 3.0 driver

我目前正在嘗試使用(相對)新的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)

知道在哪里處理身份驗證異常?

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
    }

將在1秒后拋出MongoTimeoutException。 雖然沒有拋出MongoSecurityException,但MongoTimeoutException的消息將包含相關的詳細信息。 例如,當其中一個服務器關閉時連接到三個成員副本集,並且其余兩個服務器上的身份驗證失敗時,MongoTimeoutException的消息字段將類似於:

在等待與ReadPreferenceServerSelector {readPreference = primary}匹配的服務器時,在1000毫秒后超時。 集群狀態的客戶端視圖是{type = UNKNOWN,servers = [{address = localhost:27017,type = UNKNOWN,state = CONNECTING,exception = {com.mongodb.MongoSocketOpenException:Exception opening socket},由{java.net引起。 ConnectException:Connection refused}},{address = localhost:27018,type = UNKNOWN,state = CONNECTING,exception = {com.mongodb.MongoSecurityException:異常驗證MongoCredential {mechanism = null,userName ='username',source ='admin' ,password =,mechanismProperties = {}}},由{com.mongodb.MongoCommandException引起:命令失敗,錯誤18:'身份驗證失敗。' 在服務器localhost:27018。 完整的響應是{“ok”:0.0,“code”:18,“errmsg”:“身份驗證失敗。” }}}]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM