博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring XML MongoDB连接配置指定用户名和密码注意事项
阅读量:6916 次
发布时间:2019-06-27

本文共 1208 字,大约阅读时间需要 4 分钟。

背景:

MongoDB改成使用用户名和密码连接后 修改遗留工程 使用Spring xml 配置

MongoDB客户端版本

org.springframework.data
spring-data-mongodb
1.10.3.RELEASE

刚开始做了如下的修改

增加了用户名和密码后 以为可以正常连接 但是始终报错

com.mongodb.MongoQueryException: Query failed with error code 13 and error message 'not authorized on test to execute command { find: "test", filter: { _id: ObjectId('5b75916f266c3a7c0345430f') }, limit: 1, singleBatch: true }' on server 127.0.0.1:27017

奇怪的是 终端中可以正常连接

➜  ~ mongo test  -u test -p 123456rs0:PRIMARY> dbtestrs0:PRIMARY> show collectionstest

于是改成直接使用Java代码连接 发现XML配置中使用的MongoDbFactory构造方法已经过期了

clipboard.png

提示

since 1.7. The credentials used should be provided by MongoClient.getCredentialsList().

于是改用了一个未过期的构造方法

MongoClient mongoClient = new MongoClient(new ServerAddress(), ImmutableList.of(MongoCredential.createCredential(username, databaseName, password.toCharArray())));SimpleMongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, databaseName);MongoOperations mongoOps = new MongoTemplate(mongoDbFactory);

此时能够成功连接 对应的XML配置为

参考文档

转载地址:http://zmicl.baihongyu.com/

你可能感兴趣的文章
SVN配置安装
查看>>
linux系统及特性简单介绍
查看>>
linux基础命令 grep
查看>>
CNCF启动K8s软件一致性项目,Rancher入选全球首批K8s认证平台
查看>>
制造业信息化到底需要的是什么?
查看>>
近期用到的linux命令
查看>>
用户和组的的权限
查看>>
下拉框
查看>>
Linux JDK安装及环境变量配置
查看>>
十一月个人考核
查看>>
2-3-运维必备核心技能-nginx配置文件全面讲解
查看>>
对Docker了解多少?10分钟带你从入门操作到实战上手
查看>>
SCI《科学引文索引》
查看>>
老男孩Python自动化开发12期完整版(含作业代码课件)
查看>>
实现Bootstrp的Switch效果
查看>>
postgresql中 from dual 报错的解决方案
查看>>
day33 Hibernate 一对一,检索,hql,离线(criteria),c3p0,隔离,锁
查看>>
Python模拟登录的几种方法
查看>>
五一劳动节专访:24小时劳模—VIKI-AI语音智能机器人
查看>>
Apache POI使用详解
查看>>