我有一个启用了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
基础教程推荐
猜你喜欢
- Java中POST、GET、@RequestBody和@RequestParam区别详析 2023-06-17
- EasyExcel实现读写Excel文件的示例代码 2023-04-17
- SpringCloud笔记(Hoxton)Netflix之Ribbon负载均衡示例代码 2022-11-29
- JavaFx实现登录成功跳转到程序主页面 2023-01-13
- alibaba seata服务端具体实现 2022-11-01
- spring-boot-maven-plugin 配置有啥用 2023-04-12
- Java分布式服务框架Dubbo介绍 2023-02-10
- Windows下RabbitMQ安装及配置详解 2023-02-11
- SpringBoot如何对LocalDateTime进行格式化并解析 2023-02-11
- 告诉你为什么 ThreadLocal 可以做到线程隔离 2023-03-21