java连接mongoDB《安全验证》
来源:互联网 发布:矩阵型组织形式优缺点 编辑:程序博客网 时间:2024/06/10 02:32
1. 需要jar包:
maven依赖:
<dependency><groupId>org.mongodb</groupId><artifactId>mongo-java-driver</artifactId><version>2.12.4</version></dependency>
2.设置db数据库的安全验证机制:
①创建用户
②在开启mongoDB的时候使用安全模式:
3.java代码,实现CRUD。
package mongoDb;import java.net.UnknownHostException;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import com.mongodb.BasicDBObject;import com.mongodb.DB;import com.mongodb.DBCollection;import com.mongodb.DBCursor;import com.mongodb.DBObject;import com.mongodb.MongoClient;import com.mongodb.MongoCredential;import com.mongodb.ServerAddress;public class MongoDBUtil { static MongoClient client; static DB db; static DBCollection collection; static { try { // client = new MongoClient("192.168.20.114", 27017); System.out.println("连接服务器测试................."); ServerAddress serverAddress = new ServerAddress("192.168.20.114", 27017); List<ServerAddress> seeds = new ArrayList<ServerAddress>(); seeds.add(serverAddress); MongoCredential credentials = MongoCredential.createMongoCRCredential("testAdmin", "test", "123".toCharArray()); List<MongoCredential> credentialsList = new ArrayList<MongoCredential>(); credentialsList.add(credentials); client = new MongoClient(seeds, credentialsList); // (List<ServerAddress> seeds, List<MongoCredential// db = client.getDB("test"); // db.authenticate("testAdmin", "123".toCharArray()); collection = db.getCollection("user"); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void add(DBObject dbObject) { collection.insert(dbObject); } public static void del(DBObject dbObject) { collection.remove(dbObject); } public static void update(DBObject dbObject1, DBObject dbObject2) { collection.update(dbObject1, dbObject2); } public static DBObject queryOneByKey(DBObject dbObject, DBObject keys) { DBCursor dbCursor = collection.find(dbObject, keys); if (dbCursor.hasNext()) { return dbCursor.next(); } return null; } public static java.util.List<DBObject> query(DBObject dbObject) { DBCursor dbCursor = collection.find(dbObject); List<DBObject> ret = new ArrayList<DBObject>(); while (dbCursor.hasNext()) { DBObject dbObject2 = dbCursor.next(); ret.add(dbObject2); } return ret; } public static void main(String[] args) { BasicDBObject dbObject = new BasicDBObject(); dbObject.put("num", new BasicDBObject("$gt", 0)); List<DBObject> ret = MongoDBUtil.query(dbObject); for (Iterator<DBObject> it = ret.iterator(); it.hasNext();) { System.out.println(it.next()); } }}
1 0
- java连接mongoDB《安全验证》
- mongodb的连接和开启安全验证
- MongoDB之连接安全
- MongoDB之连接安全
- 【MongoDB】非验证连接和验证连接
- MongoDB 权限验证(KEY: Mongo; MongoDB; 安全验证; 权限)
- MongoDB 权限验证(KEY: Mongo; MongoDB; 安全验证; 权限)
- java安全证书验证
- Java连接MongoDB
- java连接mongoDB
- MongoDB Java 连接
- java连接mongodb
- java连接mongodb数据库
- Java连接MongoDB
- MongoDB 连接 for java
- java连接mongodb
- java连接MongoDB数据库
- Java连接,操作MongoDB
- WEBRTC 录音与会议录音
- linux下操作mysql和一些常规操作
- 重置IOS APP首次运行时系统弹出的“是否允许”提示框
- Nexus设备升级5.0方法
- 手动屏幕旋转的方法
- java连接mongoDB《安全验证》
- Azure WebJob
- 菜鸟nginx源码剖析数据结构篇(十) 自旋锁ngx_spinlock
- python-Django 小笔记
- android 时间转换
- 如何利用客户端缓存对网站进行优化? If-Modified-Since & If-None-Match
- ajaxFileUpload使用及注意点
- SmartFoxServer服务器端用户登录验证
- 高精度加法