gpt4 book ai didi

java - MongoSocketReadException : Prematurely reached end of stream (Java to Mongo using ssl)

转载 作者:行者123 更新时间:2023-12-04 22:43:37 29 4
gpt4 key购买 nike

尝试使用 ssl true 将我的 java 应用程序与 mongo 集群连接时出现上述错误。
以下是我正在遵循的步骤:

Note: The used path are the dummy data I used the actual or correct path for each.


  • 创建了我自己的 Java keystore 。
  • 使用以下 cmd 在 keystore 中导入 CAFile:
    "%JAVA_HOME%"\bin\keytool -import -trustcacerts -file "..\..CAFile.pem"-keystore my-keystore -storepass test@123
  • 现在将我的 PEMKeyFile.pem 转换为 .pkcs12 甲酸盐
    openssl pkcs12 -export -in "..\..PEMKeyfile.pem"-out ..\..PEMKeyfile.pkcs12
  • Java 代码:
  • @Bean
    public MongoClient mongoClient() {
    System.setProperty ("javax.net.ssl.trustStore","..\\..\\my-keystore");
    System.setProperty ("javax.net.ssl.trustStorePassword","test@123");
    System.setProperty ("javax.net.ssl.keyStore","..\\..\\PEMKeyfile.pkcs12");
    System.setProperty ("javax.net.ssl.keyStorePassword","test@123");

    MongoClientOptions mongoClientOptions = MongoClientOptions.builder().sslEnabled(true).build();

    MongoCredential credential = MongoCredential.createCredential(username, db, pwd.toCharArray());
    return new MongoClient(return Arrays.asList(new ServerAddress("host-1", 123),
    new ServerAddress("host-2", 123),
    new ServerAddress("host-3", 123));, credential, mongoClientOptions);
    }

    错误:
    所有三个主机都出现相同的错误
    INFO 4088 --- [xyz:27051] org.mongodb.driver.cluster               : Exception in monitor thread while connecting to server host-1:123

    com.mongodb.MongoSocketReadException: Prematurely reached end of stream
    at com.mongodb.internal.connection.SocketStream.read(SocketStream.java:112) ~[mongo-java-driver-3.10.2.jar:na]
    at com.mongodb.internal.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:570) ~[mongo-java-driver-3.10.2.jar:na]
    at com.mongodb.internal.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:441) ~[mongo-java-driver-3.10.2.jar:na]
    at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:295) ~[mongo-java-driver-3.10.2.jar:na]
    at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:255) ~[mongo-java-driver-3.10.2.jar:na]
    at com.mongodb.internal.connection.CommandHelper.sendAndReceive(CommandHelper.java:83) ~[mongo-java-driver-3.10.2.jar:na]
    at com.mongodb.internal.connection.CommandHelper.executeCommand(CommandHelper.java:33) ~[mongo-java-driver-3.10.2.jar:na]
    at com.mongodb.internal.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:105) ~[mongo-java-driver-3.10.2.jar:na]
    at com.mongodb.internal.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:62) ~[mongo-java-driver-3.10.2.jar:na]
    at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:127) ~[mongo-java-driver-3.10.2.jar:na]
    at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongo-java-driver-3.10.2.jar:na]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_221]

    最佳答案

    我错误地删除了一个实际生成的 pem 文件的证书表单,当我尝试通过终端连接时,它能够连接一个证书,但是当在代码中尝试相同的事情时,我不知道它为什么以及如何改变自然它突然停止工作,所以不容易检测到哪个部分有故障,但是在使用两个证书撤消我生成的 pem 文件后是的,它可以工作。

    关于java - MongoSocketReadException : Prematurely reached end of stream (Java to Mongo using ssl),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60446872/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com