7
votes

I'm trying to create a server where my webpage / app can make calls, from now I'm starting with Login so, I've created the Server according my needs, the problem is on application.properties that I'm missing something, I have this :

spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.authentication-database=mydatabase
spring.data.mongodb.username=stuart
spring.data.mongodb.password=stuartdto
spring.data.mongodb.database=mydatabase

And the error is this one when trying to call the login call

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" }

I've started mongod using this command mongod --auth and I've created an user to do this (is working because if I do manually mongo -u user -p 'password' --authenticationDatabase "admin" works) it wasn't working until I add this --authenticationDatabase "admin" so maybe that's what I need from Spring.

What I'm missing?

2
A good way to troubleshoot authentication errors like these is to look into MongoDB log file (mongodb.log). Auth failures will be logged and will show if the issue is a bad password or bad username.Adam Harrison

2 Answers

12
votes

so maybe that's what I need from Spring

Of course, you are saying spring.data.mongodb.authentication-database = mydatabase and it's incorrect!

You want to use spring.data.mongodb.authentication-database = admin instead, and it will work.

6
votes

If you are using spring.data.mongodb.uri method,

spring.data.mongodb.uri=mongodb://stuart:stuartdto@localhost:27017/mydatabase?authSource=admin