从JAVA应用程序使用SSL连接到MongoDb
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了从JAVA应用程序使用SSL连接到MongoDb,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2083字,纯文字阅读大概需要3分钟。
内容图文
![从JAVA应用程序使用SSL连接到MongoDb](/upload/InfoBanner/zyjiaocheng/906/46d6634becfc4f828faba3de0a85da6c.jpg)
我有一个启用了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应用程序使用SSL连接到MongoDb所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。