微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从JAVA应用程序使用SSL连接到MongoDb

我有一个启用了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;
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐