配置中配置账号和密码连接数据库,出现MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='zhupeng', source='ksc', password=<hidden>, mechanismProperties={}}权限异常
百度一下报错原因,看到了这条信息,
当然,这个提示等于没有提示,给我的感觉就是我少了某些东西,于是从设置账密的标签入手。点进标签,查看一下标签的定义。
<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>
其中有一段解释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数据库的工具看一下权限
添加权限后解决问题
<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>