【问题标题】:MongoDB config for Spring Boot - 'url' with and without authentication credentials用于 Spring Boot 的 MongoDB 配置 - 带有和不带有身份验证凭据的“url”
【发布时间】:2020-10-29 16:01:57
【问题描述】:

我想让 application.yml 在运行测试时使用和不使用用户名/密码。我的服务器需要凭据,但是当我在本地主机上时,我希望它在没有身份验证和默认设置的情况下工作。弹簧轮廓(“integrationtest”)应该保持不变。

我有以下 Spring Boot MongoDB 配置:

mongodb:
  uri: mongodb://${mongodb_username:testuser}:${mongodb_password:testpassword}@${mongodb_ip:localhost}:${mongodb_port:27017}/testDB

如果我删除用户名和密码,我会收到此错误:

org.springframework.data.mongodb.UncategorizedMongoDbException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='testuser', source='testDB', password=<hidden>, mechanismProperties=<hidden>}; nested exception is com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='testuser', source='testDB', password=<hidden>, mechanismProperties=<hidden>}
Caused by: 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"}

我怎样才能使它与相同的配置文件一起工作?也许有嵌套变量的方法?谢谢。

【问题讨论】:

  • 我可以将完整的 uri 放入一个变量中

标签: mongodb spring-boot


【解决方案1】:

一个简单的解决方案是将完整的 uri 放在一个变量中,localhost 使用默认配置,服务器在服务器启动时指定 uri。配置如下:

  uri: ${mongodb_uri:mongodb://localhost:27017/testDB}

在服务器启动时覆盖它:

-Dmongodb_uri=mongodb://testUser:testPassword@example.com:27017/testDB

【讨论】:

    猜你喜欢
    • 2018-03-11
    • 1970-01-01
    • 2023-03-16
    • 2018-07-13
    • 2021-11-19
    • 1970-01-01
    • 2017-10-29
    • 1970-01-01
    相关资源
    最近更新 更多