1 回答
TA贡献2003条经验 获得超2个赞
我首先使用 command创建了一个mongodb.pkcs12文件openssl pkcs12 -export -out mongodb.pkcs12 -in mongodb.pem。
我必须在下面的代码中将此文件包含在keyStore和trustStore的文件路径中(注意:我的代码也已更改并最小化)。
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
public class Starter {
private static MongoClient mongoClient;
public static void main(String[] args) {
System.setProperty("javax.net.ssl.trustStore", "mongodb.pkcs12");
System.setProperty("javax.net.ssl.trustStorePassword", "yourPassword");
System.setProperty("javax.net.ssl.keyStore", "mongodb.pkcs12");
System.setProperty("javax.net.ssl.keyStorePassword", "yourPassword");
MongoClientURI mongoClientURI = new MongoClientURI(
"mongodb://admin:admin@hostname3.xyz.com,hostname2.xyz.com:27017,hostname3.xyz.com:27017/admin?ssl=true");
mongoClient = new MongoClient(mongoClientURI);
try {
MongoDatabase db = mongoClient.getDatabase("CIM");
MongoCollection<Document> collection = db.getCollection("orders");
System.out.println("No of Documents in orders collection: "
+ collection.count());
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
上面的代码运行良好,下面是我得到的输出,
Sep 24, 2018 3:49:13 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Discovered cluster type of SHARDED
Sep 24, 2018 3:49:15 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Opened connection [connectionId{localValue:4}] to hostname3.xyz.com:27017
No of Documents in orders collection: 3
添加回答
举报