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

1 Like

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

@Nafaz_M_N_M did you resolve the issue?
I get the same error as you did. I can connect by shell using the same connection details, but not via Spring application.
My application.properties specify:
spring.data.mongodb.authentication-database=admin

UPDATE:
I have now resolved my problem. This is what happend:
I wanted to learn how to use a MongoDB instance from a Docker container instead of using my local MongoDB. I mapped the MongoDB container’s port 27017 to the computers localhost 27017. When running the Spring Application, MongoDB seemed to respond. It was only when I tried to performe CRUD-operations that authentication failed. Finally I realized that the localhost 27017 was already in use by the local MongoDB. Thus, I mapped the MongoDB container to port 8094 on localhost, and now it seems to work fine. I hope this could be of any help to anyone.

I seem to be having the same problem, I just fired a spring jpa mogo app and I have authentication issues when I try to perform any crud operation, I am running mongo on docker and I dont have any instances running locally also I checked my ports and one other app is using it. I am able to log on successfully when I using the mongo app.

By the way running spring 2.7.4 with java 17 on an m1 mac

The error I get is shown below

2022-12-24 13:30:35.002 ERROR 86462 — [nio-8080-exec-1] 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=‘adebola’, source=‘onecard’, password=, mechanismProperties=}; nested exception is com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName=‘adebola’, source=‘onecard’, password=, mechanismProperties=}] with root cause

com.mongodb.MongoCommandException: Command failed with error 18 (AuthenticationFailed): ‘Authentication failed.’ on server localhost:27017. The full response is {“ok”: 0.0, “errmsg”: “Authentication failed.”, “code”: 18, “codeName”: “AuthenticationFailed”}

Hello,
Did you resolve this error? Im stuck on a similar error with code 18

I faced similar error. And i resolve it by adding ?authSource=admin to the end of my mongo connection string.

mongodb://<username>:<password>@<host>:27017/image_hosting_db?authSource=admin

I had the same problem with spring boot and solved it like this:
replicaset “rset”
username user
password pw
Dtatabase workdb
i have mongodb on pc “mini-pc” and 2 replica “hostReplica1” “hostReplica2” .

THIS NOT WORK

  • spring.data.mongodb.uri=mongodb://user:pw@mini-pc:27017,hostReplica1:27017,hostReplica2:27017/workdb?authSource=admin?replicaSet=rset

ERROR 18 authentication fail

THIS NOT WORK

  • spring.data.mongodb.uri=mongodb://user:pw@mini-pc:27017,hostReplica1:27017,hostReplica2:27017/workdb?replicaSet=rset?authSource=admin

IN THIS CASE IT START BUT DIE AFTER SOME TIME FOR REPLICASET ERROR

THIS WORK

  • spring.data.mongodb.uri=mongodb://user:pw@mini-pc:27017,hostReplica1:27017,hostReplica2:27017/workdb?authSource=admin

Hopefully it helps someone

1 Like