我有一个启用了SSL的MongoDb实例(单实例).我可以使用RoboMongo连接到它,在SSL选项卡上我提供以下内容:CA File : /path to my certificate/testCA.pem PEM certificate/key: /path to my key/testKey.pem哪个成功连...

我有一个启用了SSL的MongoDb实例(单实例).我可以使用RoboMongo连接到它,在SSL选项卡上我提供以下内容:
CA File : /path to my certificate/testCA.pem
PEM certificate/key: /path to my key/testKey.pem
哪个成功连接.现在我正在尝试从java应用程序连接到相同的mondodb.我使用以下命令将testCA.pem导入cacerts:
keytool -import -keystore cacerts -file testCA.pem -storepass changeit
我可以看到一个新的条目添加到商店.试图将其他密钥添加到其中,它表示无效的证书.在Java应用程序中,我将系统属性设置如下:
System.setProperty ("javax.net.ssl.trustStore","C:\\Program Files\\Java\\jre1.8.0_91\\lib\\security\\cacerts");
System.setProperty ("javax.net.ssl.trustStorePassword","changeit");
我收到以下错误:
org.springframework.dao.DataAccessResourceFailureException: Timed out after 10000 ms while waiting to connect. Client view of cluster state is {type=Unknown, servers=[{address=test.mongo.com:27017, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket}, caused by {java.io.EOFException}}]; nested exception is com.mongodb.MongoTimeoutException: Timed out after 10000 ms while waiting to connect. Client view of cluster state is {type=Unknown, servers=[{address=test.mongo.com:27017, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket}, caused by {java.io.EOFException}}]
at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:75)
at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:2075)
at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:1918)
我在这里想念的是什么,提前谢谢!
解决方法:
您需要配置monog db驱动程序以使用SSL.您可以在@Configuration类中手动配置它来执行此操作.
public @Bean MongoClient mongo() {
MongoClientOptions.Builder options = MongoClientOptions.builder().sslEnabled(true);
// add more options to the builder with your config
MongoClient mongoClient = new MongoClient("localhost", options.build());
return mongoClient;
}
沃梦达教程
本文标题为:从JAVA应用程序使用SSL连接到MongoDb


猜你喜欢
- SpringBoot如何切换成其它的嵌入式Servlet容器(Jetty和Undertow) 2023-02-20
- Spring Cloud Stream消息驱动组件使用方法介绍 2023-05-08
- 使用Java8 Stream流的skip + limit实现批处理的方法 2023-02-19
- Spring AOP实现记录操作日志 2023-05-08
- SpringBoot使用Shiro实现动态加载权限详解流程 2023-03-21
- Spring AOP中三种增强方式的示例详解 2023-02-11
- java中Calendar与Date类型互相转换的方法 2023-05-19
- Java异常架构和异常关键字图文详解 2022-11-25
- Java中BigDecimal的舍入模式解析(RoundingMode) 2023-01-13
- 图解Java经典算法快速排序的原理与实现 2023-05-14