尝试从 spring 引导 docker 容器连接到 mongo docker 容器时验证 MongoCredential 时出错?

Error authenticating MongoCredential when trying to connect from spring boot docker container to mongo docker container?

我有以下 docker compose 文件,用于连接到 mongoDB containerSpring boot container

#
# APIs
#----------------------------------------------
  pokerstats:
    image: pokerstats
    container_name: pokerstats
    ports:
      - 8080:8080
    depends_on: 
      - db

#
# Utilities
#----------------------------------------------
  db:
    image: mongo
    container_name: mongo
    volumes:
      - ./database:/data
    ports:
      - "27017:27017"
    environment:
      MONGO_INITDB_ROOT_USERNAME: admin
      MONGO_INITDB_ROOT_PASSWORD: admin
      MONGO_INITDB_DATABASE: pokerStats  

在我的 spring 引导 application.properties 我有:

server.port=8080
spring.main.allow-bean-definition-overriding=true

spring.data.mongodb.uri=mongodb://admin:admin@mongo:27017/pokerStats
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.database=pokerStats

当我尝试使用 Postman 访问我的端点时,出现以下错误:

{
        "timestamp": "2020-04-07T17:39:19.129+0000",
        "status": 500,
        "error": "Internal Server Error",
        "message": "Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='admin', source='pokerStats', password=<hidden>, mechanismProperties=<hidden>}; nested exception is com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='admin', source='pokerStats', password=<hidden>, mechanismProperties=<hidden>}",
        "path": "/rounds"
    }

请注意,在我将以下内容添加到我的 docker 撰写文件之前,它工作正常:

 environment:
      MONGO_INITDB_ROOT_USERNAME: admin
      MONGO_INITDB_ROOT_PASSWORD: admin
      MONGO_INITDB_DATABASE: pokerStats  

您的错误消息表明 authSource 是 pokerStats。但是根据 this which I found via .

,用户是在 admin 数据库中创建的

因此,我假设此配置不正确:

spring.data.mongodb.authentication-database=admin

我不熟悉 spring 数据,所以我不知道如何正确配置它。

我能够通过添加解决问题:

application.properties:

spring.data.mongodb.uri=mongodb://admin:admin@mongo:27017/pokerStats?authSource=admin
spring.data.mongodb.authentication-database = admin
spring.data.mongodb.database=pokerStats

我认为:?authSource=admin 需要添加到 mongo uri 的末尾。