使用 ssl 从 Spring Boot 应用程序连接到 MongoDB [英] Connecting to MongoDB from spring boot app using ssl

查看:117
本文介绍了使用 ssl 从 Spring Boot 应用程序连接到 MongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ssl 将我的 Spring Boot 应用程序连接到 mongodb.我按照此处描述的步骤操作,但它们对我不起作用.

im trying to connect my spring boot app to mongodb using ssl. I followed the steps described here, but they dont work for me.

https://www.compose.com/articles/how-to-connecting-to-compose-mongodb-with-java-and-ssl/

有什么想法吗?

谢谢阿莱姆

推荐答案

我建议你看一下 Accessing Data with MongoDB 可用这里 https://spring.io/guides/gs/accessing-data-mongodb/ 基本用法示例.spring-boot-starter-data-mongodb 会让你走得很远,你需要做的是像这样配置一个 MongoClientOptions bean

I would suggest that you look at Accessing Data with MongoDB available here https://spring.io/guides/gs/accessing-data-mongodb/ for basic usage examples. spring-boot-starter-data-mongodb will get you a long way, what you need to do is configure a MongoClientOptions bean like this

    @Bean
    public  MongoClientOptions mongoClientOptions(){
        System.setProperty ("javax.net.ssl.keyStore","<<PATH TO KEYSTOR >>");
        System.setProperty ("javax.net.ssl.keyStorePassword","PASSWORD");   
        MongoClientOptions.Builder builder = MongoClientOptions.builder();
        MongoClientOptions options=builder.sslEnabled(true).build();        
        return options;
    }

并将 mongo 客户端选项作为参数传递给 MongoClient 实例,如下所示

and pass the mongo client options to MongoClient instance as an argument as follows

public MongoClient(ServerAddress addr, MongoClientOptions options) {
        super(addr, options);
    }

进一步添加,当 mongo 进程启动时

Adding further, when mongo processs is started with

mongo --ssl --sslAllowInvalidCertificates --host --port

mongo --ssl --sslAllowInvalidCertificates --host --port

连接到 mongo 进程的客户端不必设置任何选项来支持这一点.

clients connecting to the mongo process dont have to set any options to support this.

我用过这个帖子Spring数据mongodb,如何设置SSL? 和这个 spring.io 指南作为参考.

I used this post Spring data mongodb, how to set SSL? and this spring.io guide as reference.

希望对你有帮助

这篇关于使用 ssl 从 Spring Boot 应用程序连接到 MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆