【问题标题】:Mongodb replicaset authetication failing from spring bootSpring Boot 中的 Mongodb 副本集身份验证失败
【发布时间】:2020-09-27 14:52:39
【问题描述】:

我有 3 个实例 mongodb 副本集,包括 3 个不同 ec2 实例中的 1 个仲裁器。从 mongo 控制台我可以连接到副本集。 但是当我尝试在主 ec2 实例中构建/部署我的 dockerized spring boot 应用程序时,它会给出以下异常

Caused by: org.springframework.data.mongodb.UncategorizedMongoDbException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='<usrName>', source='<source>', password=<hidden>, mechanismProperties=<hidden>}; nested exception is com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='<usrName>', source='<source>', password=<hidden>, mechanismProperties=<hidden>}
Caused by: com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='<usrName>', source='<source>', password=<hidden>, mechanismProperties=<hidden>}
Caused by: com.mongodb.MongoCommandException: Command failed with error 18 (AuthenticationFailed): 'Authentication failed.' on server <Primary-Host>:27017. The full response is {"operationTime": {"$timestamp": {"t": 1601217500, "i": 1}}, "ok": 0.0, "errmsg": "Authentication failed.", "code": 18, "codeName": "AuthenticationFailed", "$clusterTime": {"clusterTime": {"$timestamp": {"t": 1601217500, "i": 1}}, "signature": {"hash": {"$binary": {"base64": "KSwBAZHnhcqmjdsAy9HHVB8+yZQ=", "subType": "00"}}, "keyId": 6876114453302083588}}}

连接副本集时使用的 Spring 数据 mongodb 属性

spring.data.mongodb.uri=mongodb://<usrName>:<password>@<host-primary>:27017,<host-secondary>:27017/<dbName>?<replicaset name>
spring.data.mongodb.auto-index-creation = true

当我尝试使用以下属性(即单节点连接)构建/部署时,这是成功的

spring.data.mongodb.host=<Primary-Host>
spring.data.mongodb.port=27017
spring.data.mongodb.database=<database name>
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=<user name>
spring.data.mongodb.password=<password>
spring.data.mongodb.auto-index-creation = true

【问题讨论】:

    标签: java mongodb spring-boot spring-data-mongodb replicaset


    【解决方案1】:

    usernamepassword 是否包含 at 符号 @、冒号 :、斜杠 / 或百分号 % 字符?

    如果是,请检查您是否使用了正确的编码。

    还可以尝试在 uri 中添加 authSource,如下所示:

    ?authSource=admin&amp;replicaSet=myRepl

    【讨论】:

    • 密码和用户名没问题。将尝试使用 authSource
    • authSource=admin 成功了。非常感谢@WASIM AKRAM KHAN
    猜你喜欢
    • 2015-07-09
    • 2021-06-24
    • 2019-05-29
    • 2022-08-16
    • 2019-01-06
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    • 1970-01-01
    相关资源
    最近更新 更多