After upgrading from Spring Boot 3.0.7 to Spring Boot 3.1.0, my project failed to authenticate with the MongoDB database.
I'm using the spring-boot-starter-data-mongodb dependency.
This is part of my application.yml config related to MongoDB:
spring:
data:
mongodb:
database: academyDB
host: 127.0.0.1
port: 27017
username: user
password: password
authentication-database: admin
auto-index-creation: true
The error I'm getting:
Caused by: com.mongodb.MongoCommandException: Command failed with error 18 (AuthenticationFailed): 'Authentication failed.' on server 127.0.0.1:27017. The full response is {"ok": 0.0, "errmsg": "Authentication failed.", "code": 18, "codeName": "AuthenticationFailed"}
at com.mongodb.internal.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:205)
at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:443)
...
I just figured out, that if I'm using the uri attribute in my application.yml, the application starts successful and the MongoDB connection is set up as well:
spring:
data:
mongodb:
uri: mongodb://root:mongo@localhost:27017/${spring.data.mongodb.database}?authSource=${spring.data.mongodb.authentication-database}&readPreference=primaryPreferred&directConnection=true
database: academyDB
authentication-database: admin
If there are any changes in the way spring uses the configuration to authenticate against MongoDB, they are missing in the release notes and in the documentation. So probably it's a bug.
The alternative was to use the discrete properties is still specified in the documentation
I hope someone can clarify this issue. Thanks!
Comment From: scottfrederick
Duplicates #35567