猿问

如何使用 Java 连接到启用 SSL 和身份验证的 MongoDB

问题:


我有 CA 证书作为cafile.pem,PEM 证书作为mongodb.pem。我不确定如何在 java 中使用它来验证和连接 MongoDB。任何建议将不胜感激。如果您想提供更多信息,请告诉我。


设想:


我正在使用mongo-java-driver-3.4.3.jar连接到启用 SSL、启用身份验证的 MongoDB 3.7.9 分片集群,其中包含 3 个实例。下面是我试图连接到该服务器的 Java 代码。


import java.util.ArrayList;

import java.util.List;


import org.bson.Document;


import com.mongodb.MongoClient;

import com.mongodb.MongoClientOptions;

import com.mongodb.MongoCredential;

import com.mongodb.ReadPreference;

import com.mongodb.ServerAddress;

import com.mongodb.client.MongoCollection;

import com.mongodb.client.MongoDatabase;


public class Starter {


    public static void main(String[] args){


        List<MongoCredential> mongoCredentials = new ArrayList<MongoCredential>();

        mongoCredentials.add(MongoCredential.

        createScramSha1Credential("admin", "admin", "admin".toCharArray()));


        List<ServerAddress> serverAddressArray = new ArrayList<ServerAddress>();


        serverAddressArray.add(new ServerAddress("xyz.domain.com", 27017));


        MongoClientOptions options = new MongoClientOptions.Builder()

                                        .sslInvalidHostNameAllowed(false)

                                        .sslEnabled(true)

                                        .readPreference(ReadPreference.primaryPreferred())

                                        .build();

        MongoClient mongoClient = new MongoClient(serverAddressArray, mongoCredentials, options);

        try {

            System.out.println("----- Step 1 ------");

            MongoDatabase db = mongoClient.getDatabase("CIM");

            System.out.println("----- Step 2 ------");

            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());

          }

    }       

}

无论如何,我可以在 RoboMongo3T 中按照以下配置连接 MongoServer。

千巷猫影
浏览 406回答 1
1回答

湖上湖

我首先使用 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 {&nbsp; &nbsp; private static MongoClient mongoClient;&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; System.setProperty("javax.net.ssl.trustStore", "mongodb.pkcs12");&nbsp; &nbsp; &nbsp; &nbsp; System.setProperty("javax.net.ssl.trustStorePassword", "yourPassword");&nbsp; &nbsp; &nbsp; &nbsp; System.setProperty("javax.net.ssl.keyStore", "mongodb.pkcs12");&nbsp; &nbsp; &nbsp; &nbsp; System.setProperty("javax.net.ssl.keyStorePassword", "yourPassword");&nbsp; &nbsp; &nbsp; &nbsp; MongoClientURI mongoClientURI = new MongoClientURI(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "mongodb://admin:admin@hostname3.xyz.com,hostname2.xyz.com:27017,hostname3.xyz.com:27017/admin?ssl=true");&nbsp; &nbsp; &nbsp; &nbsp; mongoClient = new MongoClient(mongoClientURI);&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MongoDatabase db = mongoClient.getDatabase("CIM");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MongoCollection<Document> collection = db.getCollection("orders");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("No of Documents in orders collection: "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + collection.count());&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception ex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(ex.getMessage());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}上面的代码运行良好,下面是我得到的输出,&nbsp; &nbsp; Sep 24, 2018 3:49:13 PM com.mongodb.diagnostics.logging.JULLogger log&nbsp; &nbsp; INFO: Discovered cluster type of SHARDED&nbsp; &nbsp; Sep 24, 2018 3:49:15 PM com.mongodb.diagnostics.logging.JULLogger log&nbsp; &nbsp; INFO: Opened connection [connectionId{localValue:4}] to hostname3.xyz.com:27017&nbsp; &nbsp; No of Documents in orders collection: 3
随时随地看视频慕课网APP

相关分类

Java
我要回答