I have a project running perfectly fine on spring boot 2.5.6 but when I upgrade to 2.7.9 my connection to mongodb stops to work.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.9</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
....
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
....
</dependencies>
Console Error
spring.data.mongodb.uri=mongodb://admin:admin@localhost:27017/test
Caused by: 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"}
When I switch back to 2.5.6 my application works again
2023-03-02 12:20:44.448 INFO 3968 --- [ restartedMain] org.mongodb.driver.cluster : Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms'}
2023-03-02 12:20:44.482 INFO 3968 --- [localhost:27017] org.mongodb.driver.connection : Opened connection [connectionId{localValue:2, serverValue:34}] to localhost:27017
2023-03-02 12:20:44.482 INFO 3968 --- [localhost:27017] org.mongodb.driver.connection : Opened connection [connectionId{localValue:1, serverValue:33}] to localhost:27017
2023-03-02 12:20:44.482 INFO 3968 --- [localhost:27017] org.mongodb.driver.cluster : Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, minWireVersion=0, maxWireVersion=13, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=12042400}
Comment From: wilkinsona
One change in this area in Spring Boot 2.7 is https://github.com/spring-projects/spring-boot/issues/30067 but, from what you have described thus far, I cannot see how it would cause this problem. Your URI should passed as-is into the Mongo driver to create a ConnectionString where it will extract the username and password from the URI.
You could try debugging the creation of the ConnectionString to check that it ends up with a MongoCredential with the expected values. If that does not help you to identify the cause of the problem and you would like us to spend some more time investigating, please spend some time providing a complete yet minimal sample that reproduces the problem. You can share it with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue.
Comment From: LuisMSoares
I can't share the project here on github because its a private project on the company where I work, but I'll try to replicate this error in a new project later. All I can do for now is share the complete log error.
I debugged the ConnectionString on both versions and both ends with the same state:
-
Spring Boot 2.7.9
Application log: https://pastebin.com/9CFGt87f
-
Spring Boot 2.5.6
Application log: https://pastebin.com/F8YijUp9
One change in this area in Spring Boot 2.7 is #30067 but, from what you have described thus far, I cannot see how it would cause this problem. Your URI should passed as-is into the Mongo driver to create a
ConnectionStringwhere it will extract the username and password from the URI.You could try debugging the creation of the
ConnectionStringto check that it ends up with aMongoCredentialwith the expected values. If that does not help you to identify the cause of the problem and you would like us to spend some more time investigating, please spend some time providing a complete yet minimal sample that reproduces the problem. You can share it with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue.
Comment From: spring-projects-issues
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.
Comment From: spring-projects-issues
Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.
Comment From: marcodzuong
i have found the solution to fix this issue. You can try to config as same as below : spring.data:mongodb.password= '12345678' . If you config as same as : spring.data:mongodb.password= 12345678 . Spring can not read it . And Can not connecto Mongo.