StuartDTO
StuartDTO

Reputation: 1031

AuthenticationFailed to mongoDB with Spring boot

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?

Upvotes: 24

Views: 39566

Answers (2)

Nazeer
Nazeer

Reputation: 583

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

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

Upvotes: 37

Skizo-ozᴉʞS ツ
Skizo-ozᴉʞS ツ

Reputation: 20626

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.

Upvotes: 39

Related Questions