2017-05-23 2 views
0

Ich versuche hier die Anmeldedaten eines bestimmten Benutzers zu validieren.Wie erhalte ich eine MongoSecurityException?

Dies wird einfach nicht funktionieren. Ich habe keine Ahnung warum, es erreicht nie den Catch-Block, obwohl es eine MongoSecurityException gibt. Weiß jemand warum?

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(); 
      } 

Update:

Stapelüberwachung:

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 
+0

Griff Haben Sie Spur haben stapeln Sie uns für weitere Informationen zu zeigen? Sind Sie sicher, dass MongoSecurityException in diesen try-Block und nicht woanders geworfen wird? – quinz

+0

Was ist dein Problem? Sie erhalten eine MongoSecurityException, fangen sie ab und drucken den Stacktrace. Das ist dein Code. Oder woher hast du den Stacktrace? –

+0

I ' ich fange es nicht, ich don ' t weiß wo der StackTrace herkommt. Als ich versuchte, etwas anderes im Catch-Block zu machen, gewann ich gerade ' t Arbeit. Als ich versuchte ...} catch (MongoSecurityException e) { System.out.println ("Zugriff verweigert"); } Es hat einfach nicht gedruckt und das ist, was ich eigentlich tun möchte, zu wissen, ob die Verbindung hergestellt wurde oder nicht. – Max

Antwort

0

Sie können nicht MongoSecurityException fangen, wie es in einem Hintergrund-Thread geworfen wird.

Sie können für eine MongoTimeoutException warten zu handhaben ‚synchron‘:

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); 
    } 

Oder Sie können eine ServerListener implementieren und asynchron

{ 
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); 
} 
Verwandte Themen