MongoDB副本集从Spring Boot进行身份验证失败

huangapple go评论85阅读模式
英文:

Mongodb replicaset authetication failing from spring boot

问题

我有3个实例的MongoDB副本集,包括3个不同的EC2实例中的1个仲裁者。从Mongo控制台,我能够连接到副本集。但是当我尝试在主要的EC2实例中构建/部署我的Docker化Spring Boot应用程序时,它会产生以下异常:

Caused by: org.springframework.data.mongodb.UncategorizedMongoDbException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='&lt;usrName&gt;', source='&lt;source&gt;', password=<hidden>, mechanismProperties=<hidden>}; nested exception is com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='&lt;usrName&gt;', source='&lt;source&gt;', password=<hidden>, mechanismProperties=<hidden>}
Caused by: com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='&lt;usrName&gt;', source='&lt;source&gt;', password=<hidden>, mechanismProperties=<hidden>}
Caused by: com.mongodb.MongoCommandException: Command failed with error 18 (AuthenticationFailed): 'Authentication failed.' on server &lt;Primary-Host&gt;: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 Data MongoDB属性如下:

spring.data.mongodb.authentication-database=admin
spring.data.mongodb.uri=mongodb://&lt;usrName&gt;:&lt;password&gt;@&lt;host-primary&gt;:27017,&lt;host-secondary&gt;:27017/&lt;dbName&gt;?&lt;replicaset name&gt;
spring.data.mongodb.auto-index-creation = true

然而,当我尝试使用以下属性进行构建/部署,即单节点连接,这是成功的:

spring.data.mongodb.host=&lt;Primary-Host&gt;
spring.data.mongodb.port=27017
spring.data.mongodb.database=&lt;database name&gt;
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=&lt;user name&gt;
spring.data.mongodb.password=&lt;password&gt;
spring.data.mongodb.auto-index-creation = true
英文:

I have 3 instance mongodb replicaset including 1 arbiter in 3 different ec2 instance. From mongo console I am able to connect to replica set.
But when I try to build/deploy my dockerized spring boot apllication in the primary ec2 instance it gives below exception

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

Spring data mongodb properties used while connecting to replicaset

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

Where as when I try to build/deploy using below properties i.e single node connection this is getting successfull

spring.data.mongodb.host=&lt;Primary-Host&gt;
spring.data.mongodb.port=27017
spring.data.mongodb.database=&lt;database name&gt;
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=&lt;user name&gt;
spring.data.mongodb.password=&lt;password&gt;
spring.data.mongodb.auto-index-creation = true

答案1

得分: 2

用户名(username)或密码(password)是否包含有 @、冒号(:)、斜杠(/)或百分号(%)字符?

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

还可以尝试在 URI 中添加 authSource,像这样:

?authSource=admin&amp;replicaSet=myRepl

英文:

Does username or password contains at sign @, colon :, slash /, or the percent sign % character ?

If yes check if you are using correct encoding.

Also try adding authSource in uri like so :

?authSource=admin&amp;replicaSet=myRepl

huangapple
  • 本文由 发表于 2020年9月27日 22:52:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64089783.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定