配置中配置账号和密码连接数据库,出现MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='zhupeng', source='ksc', password=<hidden>, mechanismProperties={}}权限异常

百度一下报错原因,看到了这条信息,

image-20211103161320195

当然,这个提示等于没有提示,给我的感觉就是我少了某些东西,于是从设置账密的标签入手。点进标签,查看一下标签的定义。

        <xsd:attribute name="credentials" type="xsd:string" use="optional">
            <xsd:annotation>
                <xsd:documentation><![CDATA[
The comma delimited list of username:password@database entries to use for authentication. Appending ?uri.authMechanism allows to specify the authentication challenge mechanism. If the credential you're trying to pass contains a comma itself, quote it with single quotes: '…'.
                            ]]></xsd:documentation>
            </xsd:annotation>
        </xsd:attribute>
    </xsd:complexType>

image-20211103161558099

其中有一段解释The comma delimited list of username:password@database entries to use for authentication. Appending ?uri.authMechanism allows to specify the authentication challenge mechanism. If the credential you're trying to pass contains a comma itself, quote it with single quotes: '…'.

这个格式是:username:password@database ,可能需要添加 ?uri.authMechanism权限(英语4级没有过,大致意思)。于是恍然大悟。原先从网上抄的写法是:

<mongo:mongo-client id="mongo" host="${mongo.host}" port="${mongo.port}"
                    credentials="${mongo.username}:${mongo.password}@${mongo.dbname}">
  .......
</mongo:mongo-client>

查看xml配置,再结合翻译后的内容,发现就少了权限的问题,我通过用MongoDB数据库的工具看一下权限

image-20211103162102499

添加权限后解决问题

<mongo:mongo-client id="mongo" host="${mongo.host}" port="${mongo.port}"
                    credentials="${mongo.username}:${mongo.password}@${mongo.dbname}?authMechanism=SCRAM-SHA-1">
  ....
</mongo:mongo-client>

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐