MongoDB Authentication Failed With Spring Data URI

when I try to connect spring boot application with the mongo-db container then the authentication failed exception has thrown.

2021-05-30 02:43:51.909  INFO 1 --- [nio-9090-exec-3] org.mongodb.driver.connection            : Closed connection [connectionId{localValue:5}] to mongo-db:27017 because there was a socket exception raised by this connection.

2021-05-30 02:43:51.924 ERROR 1 --- [nio-9090-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.mongodb.UncategorizedMongoDbException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='benz', source='admin', password=<hidden>, mechanismProperties=<hidden>}; nested exception is com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='benz', source='admin', password=<hidden>, mechanismProperties=<hidden>}] with root cause

com.mongodb.MongoCommandException: Command failed with error 18 (AuthenticationFailed): 'Authentication failed.' on server mongo-db:27017. The full response is {"ok": 0.0, "errmsg": "Authentication failed.", "code": 18, "codeName": "AuthenticationFailed"}
        at com.mongodb.internal.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:175) ~[mongodb-driver-core-4.2.3.jar!/:na]
        at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:358) ~[mongodb-driver-core-4.2.3.jar!/:na]
        at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:279) ~[mongodb-driver-core-4.2.3.jar!/:na]

please refer to this link to get more details authentication-failed

Have you tried with authSource in URI string?

yes, this is my URL

mongodb://benz:14292@mongo-db/producer_db?authSource=admin

Can you connect by shell or Compass using same connection details?

yes, it works with shell

Hi @Nafaz_M_N_M,

We hope that the problem has been resolved. So, we can close this thread for now.!!
In case of any further issues or queries, please feel free to reach out by creating new post in a relevant category.

All the Best,
Kushagra

The problem hasn’t been solved, it works with shell but not with application. Do not close the thread before getting an answer

1 Like

Hi @Nafaz_M_N_M,

Be sure, the thread will not be closed before getting resolved.
I have moved the thread to a specific category and added some tags for broader visibility.

All the Best,
Kushagra