首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >AWS DocumentDB TLS与Java的连接

AWS DocumentDB TLS与Java的连接
EN

Stack Overflow用户
提问于 2020-01-14 17:34:35
回答 3查看 6.6K关注 0票数 12

我面临着通过TLS/SSL与普通DocumentDB集群连接的问题

根据AWS博士,我遵循的步骤是:

我从AWS下载了.pem文件,并在我的java项目的根目录中复制了该文件,我在那里执行了以下操作:

代码语言:javascript
运行
复制
keytool -importcert -trustcacerts -file rds-combined-ca-bundle.pem -alias certAlias -keystore rds-ca-certs -storepass keyStorePassword

这在我的项目的根部创建了rds-ca-certs,现在看起来如下所示:

而且,Main.java中的java代码是:

代码语言:javascript
运行
复制
package com.example.documentdb;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.ServerAddress;
import com.mongodb.MongoException;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoCollection;
import org.bson.Document;


public final class Main {
    private Main() {
    }
    public static void main(String[] args) {

        String template = "mongodb://%s:%s@%s/test?ssl=true&replicaSet=rs0&readpreference=%s";
        String username = "myUsernameInCluster";
        String password = "myPasswordInCluster";
        String clusterEndpoint = "mycluster.node.us-east-1.docdb.amazonaws.com:27017";
        String readPreference = "secondaryPreferred";
        String connectionString = String.format(template, username, password, clusterEndpoint, readPreference);

        String keystore = "rds-ca-certs";
        String keystorePassword = "keyStorePassword";

        System.setProperty("javax.net.ssl.trustStore", keystore);
        System.setProperty("javax.net.ssl.trustStorePassword", keystorePassword);

        MongoClientURI clientURI = new MongoClientURI(connectionString);
        MongoClient mongoClient = new MongoClient(clientURI);

        MongoDatabase testDB = mongoClient.getDatabase("test");
        MongoCollection<Document> numbersCollection = testDB.getCollection("numbers");

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

    }
}

它给了我这个丑陋的错误:

com.mongodb.MongoSocketWriteException:异常发送消息 导致: sun.security.provider.certpath.SunCertPathBuilderException::PKIX路径生成失败:javax.net.ssl.SSLHandshakeException无法找到被请求目标的有效证书路径

有趣的是,当我使用mongo通过SSL/TLS进行连接时,如下所示:

代码语言:javascript
运行
复制
mongo --ssl --host mycluster.node.eu-central-1.docdb.amazonaws.com:27017 --sslCAFile rds-combined-ca-bundle.pem --username myUsernameInCluster --password myPasswordInCluster

它工作得很好,所以我已经放弃了一个网络问题,我认为问题在于Java代码本身或执行的keytool命令。

此外,在集群中禁用TLS后,此AWS提供的java代码 (仅在密钥库配置中不同)可以工作。

PD:我知道还有很多与AWS DocumentDB的SSL/TLS连接有关的问题,但这些问题都没有具体解决我的问题。此外,我还检查了简单的mongodb /SSL连接问题,进程不同,因此它们对我无效。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-02-12 09:47:06

这个问题似乎与在中以相同别名导入证书的过程有关。

因此,我不得不停止使用捆绑选项(rds-组合-ca-bundle.pem),开始使用这个rds-ca-2019-root.pem

使用以下命令导入密钥存储库之后:

代码语言:javascript
运行
复制
keytool -importcert -trustcacerts -file rds-ca-2019-root.pem -alias rds19 -keystore rds-ca-certs -storepass keyStorePassword

在TLS下实现了与数据库的连接。

票数 13
EN

Stack Overflow用户

发布于 2020-01-15 16:25:31

您可以将AWS提供的证书添加到java的信任存储区,然后java将默认地将请求信任到AWS服务。

您必须找到您的java仙人掌文件。取决于您的操作系统和java版本,它应该在..lib/security/cacerts中。

我正在使用OSX,它在

/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/lib/security/cacerts

然后使keytool导入它,默认的java密钥库密码是更改它:

keytool -import -trustcacerts -keystore /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/lib/security/cacerts -storepass changeit -noprompt -alias aws-rds -file rds-combined-ca-bundle.pem

要检查是否导入了证书,可以使用:

keytool -list -keystore /Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/lib/security/cacerts | grep aws

如果那有效的话就发回吧。

票数 1
EN

Stack Overflow用户

发布于 2021-08-21 14:24:18

https://github.com/hungbang/spring-boot-aws-docdb我已经创建了一个示例,用于在启用SSL的情况下从春季引导连接到amazon。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59739006

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档