Exception authenticating MongoCredential{mechanism=null, userName=‘zhupeng‘, source=‘ksc‘, password=

本文介绍了一种解决MongoDB配置中出现的权限异常问题的方法。通过正确设置用户名、密码及数据库认证方式,解决了MongoSecurityException异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

配置中配置账号和密码连接数据库,出现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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值