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

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

基础教程推荐