0

使用 aws doc 链接https://docs.aws.amazon.com/documentdb/latest/developerguide/connect_programmatically.html#connect_programmatically-tls_enabled我可以使用链接中指定的 java 程序连接到文档数据库。

我也在下面的程序中为密钥库设置了属性,我可以连接到 documentDB,虽然密钥库和信任库是相同的。如果我在密钥库中也提供一些 jks 文件,我可以连接。没有 documentDB做客户端证书验证?

package mongotest;
import com.mongodb.client.*;
import org.bson.Document;
public class ssl {
    public static void main(String[] args) {        
        String template = "mongodb://%s:%s@%s/sampledb?ssl=true&replicaSet=rs0&readpreference=%s";
        String username = "usrname";
        String password = "pwd";
        String clusterEndpoint = "docdb.cluster-cq32.ap-south-1.docdb.amazonaws.com:27017";
       String readPreference = "Primary";
        String connectionString = String.format(template, username, password, clusterEndpoint, readPreference);

        String truststore = "D:/bwmongodev/rds-truststore.jks";
        String truststorePassword = "password";
        
        System.setProperty("javax.net.ssl.trustStore", truststore);
        System.setProperty("javax.net.ssl.trustStorePassword", truststorePassword);
        System.setProperty("javax.net.ssl.keyStore", truststore);
        System.setProperty("javax.net.ssl.keyStorePassword", truststorePassword);
        MongoClient mongoClient = MongoClients.create(connectionString);

        MongoDatabase testDB = mongoClient.getDatabase("sampledb");
        MongoCollection<Document> numbersCollection = testDB.getCollection("sample-collection");

        Document doc = new Document("name", "pi").append("value", 3.14159);
        numbersCollection.insertOne(doc);

        MongoCursor<Document> cursor = numbersCollection.find().iterator();
        try {
            while (cursor.hasNext()) {
                System.out.println(cursor.next().toJson());
            }
        } finally {
            cursor.close();
        }
    }

}
4

0 回答 0