Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 920
StuartDTO Avatar asked Jan 02 '19 18:01

StuartDTO


People also ask

Can MongoDB be used with spring boot?

It's Easy to Connect MongoDB Atlas with Spring Boot Through this article, we demonstrated that MongoDB Spring Boot integration is easy using: Starter data MongoDB artifactid (the dependency we added while creating the Spring Initializr project) in pom. xml. A property on application.

How does spring boot connect to MongoDB Atlas?

MongoDB and Spring Boot CRUD Examples To connect to MongoDB Atlas, we specify the connection string in the application. yaml file in the src/main/resources folder. The connection string for a cluster can be found in the Atlas UI. There is no need to write connection-related code in any other file.


1 Answers

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

spring.data.mongodb.uri=mongodb://stuart:stuartdto@localhost:27017/mydatabase?authSource=admin
like image 187
Nazeer Avatar answered Oct 08 '22 10:10

Nazeer