'Spring boot controller with mongodb + mongo express (configured with docker-compose) returned an 'Authentication failed.' from postman

Im trying to create microservices with Spring Boot, so i prefered to use mongodb as a database for one of my microservices using docker compose :

Here is my docker-compose.yml file content :

 mongo:
    image: mongo
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: rootuser
      MONGO_INITDB_ROOT_PASSWORD: rootpass
    ports:
      - 27017:27017
    volumes:
      - data:/data


  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8075:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: rootuser
      ME_CONFIG_MONGODB_ADMINPASSWORD: rootpass
      ME_CONFIG_MONGODB_SERVER: mongo

And my application.yml content :

 server:
  port: 8098

spring:
  application:
     name: fraud
  data:
    mongodb:
      host: localhost
      port: 27017
      username: rootuser
      password: rootpass
      database: test
      authentication-database: admin

And i have already created my database "test" and the user "rootuser" is already exists in the admin database as shown below :

enter image description here

The Controller implementation :

@RestController
@RequestMapping("api/v1/fraud-check")
@AllArgsConstructor
public class FraudController {

    FraudCheckService fraudCheckService;
@GetMapping(path = "{customerId}")
    public FraudCheckResponse isFraudster(@PathVariable("customerId") Integer customerID){
boolean isFraudulentCustomer = fraudCheckService.isFraudulentCustomer(customerID);
return new FraudCheckResponse(isFraudulentCustomer);
}
}

but when i did a postman request i got this error below :

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


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source