Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-2414

Can not catch com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.'

    XMLWordPrintableJSON

Details

    • Icon: Task Task
    • Resolution: Works as Designed
    • Icon: Major - P3 Major - P3
    • None
    • 3.4.0
    • None
    • MongoDB server version is 3.2. mongo java driver is mongo-java-driver-3.4.0.jar. OS is windows 10.

    Description

      I have a test about authenticate by mongo-java-driver-3.4.0.jar as below

      try{
              ServerAddress sa = new ServerAddress("192.168.126.63", 27017);
              String db = "admin";
              MongoCredential mc = MongoCredential.createCredential("sunny", db, "s1234".toCharArray());
              List<MongoCredential> mcs = new ArrayList<MongoCredential>();
              mcs.add(mc);
              MongoClientOptions.Builder builder = MongoClientOptions.builder();
              builder.addServerListener(new ServerListenerImpl());
              builder.addCommandListener(new CommandListenerImpl());
              builder.addServerMonitorListener(new ServerMonitorListenerImpl());
              builder.addClusterListener(new ClusterListenerImpl());
              builder.serverSelectionTimeout(1000);
             
              MongoClientOptions options = builder.build();
              MongoClient client = new MongoClient(sa, mcs, options);
              System.out.println("Connect to database successfully="+client.getConnectPoint());
              }catch(Throwable e){
                  System.err.println( e.getClass().getName() + ": " + e.getMessage() ); 
              }
      

      If give a error username , passoword or databasename, it will throw the following exception:

      com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='sunny', 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 192.168.126.63:27017. The full response is { "ok" : 0.0, "code" : 18, "errmsg" : "Authentication failed." }
          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
      
      

      How to catch the com.mongodb.MongoSecurityException and do not let it throw to console? I need catch and deal with it in my program.

      Thanks in advance
      Martine

      Attachments

        Activity

          People

            Unassigned Unassigned
            Martine Martine [X]
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: