当前位置: 首页 > news >正文

SpringBoot 整合 MongoDB 6 以上版本副本集及配置 SSL / TLS 协议

续上一篇 Linux 中使用 docker-compose 部署 MongoDB 6 以上版本副本集及配置 SSL / TLS 协议

前提:此篇文章是对上一篇文章的实战和项目中相关配置的使用,我这边针对 MongoDB 原有基础上做了增强,简化了 MongoDB 配置 SSL / TLS 协议上的支持, 目前支持 SpringBoot、SpringCloud 等场景下,支持 MongoDB 单机、副本集、分片集群部署上的使用,具体功能可通过 https://github.com/mbql/mbql-mongodb-model 链接访问。

一、项目中引入 MongoDB 依赖

 <dependency><groupId>io.github.mbql</groupId><artifactId>mbql-mongodb-model</artifactId><version>1.0.2</version></dependency>

二、通过 yaml 文件配置(有三种模式配置,同时也支持 Java API 方式配置)

# mongodb 默认模式配置(适用于单机模式)
mongodb:url: mongodb://root:123456@127.0.0.1:27017/test?authSource=admin
# 或者
mongodb:url: mongodb://127.0.0.1:27017/testauth-user-name: rootpassword: 123456database: admin# mongodb SSL / TLS 静态模式配置(适用于单机、副本集、分片模式)
mongodb:url: mongodb://root:123456@127.0.0.1:27017,127.0.0.1:27018,127.0.0.1:27019/test?   replicaSet=rs0&tls=true&authSource=admin&tlsAllowInvalidHostnames=true&compressors=zstd&zlibCompressionLevel=6enable-ssl: true # 开启 SSL / TLS 模式use-static-mode: true # 使用静态模式 cluster-type: replica_set # 集群类型cluster-connection-mode: multiple # 集群连接模式certs:password: 123456  # 证书密钥密码trustStoreName: cacerts  # JVM 信任证书名称keyStoreName: keystore.pkcs12 # JVM 密钥证书名称
# 或者
mongodb:url: mongodb://127.0.0.1:27017,127.0.0.1:27018,127.0.0.1:27019/testenable-ssl: trueuse-static-mode: truecluster-type: replica_setcluster-connection-mode: multipledatabase: admin # 认证数据库repl-set-name: rs0 # 副本集名称invalid-host-name-allowed: true # 允许无效主机连接, 默认是只能本机auth-user-name: root # 认证用户名password: 123456 # 认证用户密码certs:password: 123456  # 证书密钥密码trustStoreName: cacerts  # JVM 信任证书名称keyStoreName: keystore.pkcs12 # JVM 密钥证书名称# mongodb SSL / TLS 动态模式配置(适用于单机、副本集、分片模式)
mongodb:url: mongodb://127.0.0.1:27017,127.0.0.1:27018,127.0.0.1:27019/testinvalid-host-name-allowed: trueenable-ssl: truecluster-type: replica_setcluster-connection-mode: multiplerepl-set-name: rs0certs:password: 123456trustStoreName: cacertskeyStoreName: keystore.pkcs12
# 或者
mongodb:url: mongodb://127.0.0.1:27017,127.0.0.1:27018,127.0.0.1:27019/test?   replicaSet=rs0&tlsAllowInvalidHostnames=true&compressors=zstd&zlibCompressionLevel=6enable-ssl: true # 开启 SSL / TLS 模式cluster-type: replica_set # 集群类型cluster-connection-mode: multiple # 集群连接模式certs:password: 123456  # 证书密钥密码trustStoreName: cacerts  # JVM 信任证书名称keyStoreName: keystore.pkcs12 # JVM 密钥证书名称# 具体配置可参考:https://github.com/mbql/mbql-mongodb-model/blob/master/README.md

三、演示上面 3 种不同模式的 Monogo 客户端配置及使用

1、默认模式使用(单机模式示列)
配置 mongo 客户端连接信息
mongodb:url: mongodb://127.0.0.1:27017/testauth-user-name: rootpassword: 123456database: admin
启动项目并且没有出现错误,就正常连接成功,如下图:

在这里插入图片描述

2、SSL / TLS 静态模式(副本集模式演示)
配置 mongo 客户端连接信息
mongodb:url: mongodb://127.0.0.1:27017,127.0.0.1:27018,127.0.0.1:27019/testenable-ssl: trueuse-static-mode: truecluster-type: replica_setcluster-connection-mode: multiplerepl-set-name: rs0 # 副本集名称invalid-host-name-allowed: true # 允许无效主机连接, 默认是只能本机certs:password: 123456  # 证书密钥密码trustStoreName: cacerts  # JVM 信任证书名称keyStoreName: keystore.pkcs12 # JVM 密钥证书名称

在项目 classpath 路径下创建 certs 目录,并将 JVM 信任证书 cacerts 和 客户端密钥证书 keystore.pkcs12 放到该目录里面,具体怎么生成自签名证书可参考上一篇文章,并且 JVM 相关证书的生成可参考 https://github.com/mbql/mbql-mongodb-model/blob/master/README.md

启动项目并且没有出现错误,就正常连接成功,如下图:

在这里插入图片描述

3、SSL / TLS 动态模式(副本集模式演示)
mongodb:url: mongodb://127.0.0.1:27017,127.0.0.1:27018,127.0.0.1:27019/testinvalid-host-name-allowed: trueenable-ssl: truecluster-type: replica_setcluster-connection-mode: multiplerepl-set-name: rs0certs:password: 123456trustStoreName: cacertskeyStoreName: keystore.pkcs12

在项目 classpath 路径下创建 certs 目录,并将 JVM 信任证书 cacerts 和 客户端密钥证书 keystore.pkcs12 放到该目录里面,具体怎么生成自签名证书可参考上一篇文章,并且 JVM 相关证书的生成可参考 https://github.com/mbql/mbql-mongodb-model/blob/master/README.md

启动项目并且没有出现错误,就正常连接成功,如下图:

在这里插入图片描述

http://www.lryc.cn/news/19563.html

相关文章:

  • C语言static关键字
  • 【华为OD机试模拟题】用 C++ 实现 - 单词接龙(2023.Q1)
  • PHP基础(2)
  • Java8(JDK1.8)新特性
  • 【C语言】指针的定义和使用
  • Parameter ‘zpspid‘ not found
  • 23、高自由度下的E类波形理论计算(附Matlab代码)
  • 软件测试:用“bug”来表示“在电脑程序里的错误”
  • Git命令
  • Java的异常概念和类型
  • 【Leedcode】环形链表必备的面试题和证明题(附图解)
  • Vulnhub靶场----7、DC-7
  • 【Unity VR开发】结合VRTK4.0:创建滑块
  • Latex中的表格(2)
  • (七)输运定理
  • ABBYYFineReader15免费电脑pdf文档文字识别软件
  • 顺序表(超详解哦)
  • Compose-Animation高级别动画
  • c++11 标准模板(STL)(std::unordered_set)(八)
  • Python每日一练(20230225)
  • 基于博客系统的测试用例
  • C语言运算符算术运算符关系运算符
  • C语言 深度剖析数据在内存中的存储
  • MyBatis快速开发
  • 大数据常见应用场景及架构改进
  • 【华为OD机试模拟题】用 C++ 实现 - 挑选字符串(2023.Q1)
  • 程序员是世界上最理性、最睿智的群体,耶稣也反驳不了我,我说的!
  • 人工智能到底是什么?
  • 在动态规划的海洋中遨游(三)
  • enable_if模板编程实现字节序转换模板