RabbitMQ開啟SSL與SpringBoot連接測試的配置方法
楔子
近期公司程序被安全掃描出 遠程主機允許明文身份驗證
中風險漏洞,查瞭下修復方案,RabbitMQ官方提供瞭SSL連接方式,而且 SpringBoot AMQP 也支持 SSL 連接。以下將配置RabbitMQ開啟SSL 並使用 SpringBoot Demo 測試連接。
PS : 寫文章時此配置還未安全掃描復測,如果測試通過,本人將更新此文章狀態為驗證通過。
配置 RabbitMQ 開啟 SSL
本文基於 CentOS 7 + Git + OpenSSL + yum 安裝的 RabbitMQ,需要讀者提交安裝好。其他方式也可變通參考本文。
生成證書
#克隆生成證書的倉庫到當前目錄 git clone --depth 1 https://github.com/Berico-Technologies/CMF-AMQP-Configuration.git cd CMF-AMQP-Configuration/ssl #生成ca證書,“MyRabbitMQCA”為自定義名稱,名稱任意。在當前目錄下生成ca目錄 sh setup_ca.sh MyRabbitMQCA #生成服務端證書,第一個參數是服務端證書前綴,第二個參數是密碼。密碼任意,在當前目錄下生成server目錄 sh make_server_cert.sh rabbitmq-server 123456 #生成客戶端證書,第一個參數是客戶端證書前綴,第二個參數是密碼。密碼任意,在當前目錄下生成client目錄 sh create_client_cert.sh rabbitmq-client 654321
配置 RabbitMQ 服務端的證書如下:
ca/cacert.pem #CA證書 server/rabbitmq-server.cert.pem #服務端公鑰 server/rabbitmq-server.key.pem #服務端私鑰
使用 RabbitMQ 服務端公鑰證書生成 JKS 證書
# -alias後為別稱,-file後是服務端公鑰位置,-keystore後是輸出JSK證書位置,此處相對路徑 keytool -import -alias rabbitmq-server \ -file server/rabbitmq-server.cert.pem \ -keystore rabbitmqTrustStore -storepass changeit #輸入y回車
配置 RabbitMQ 客戶端的證書如下:
client/rabbitmq-client.keycert.p12 #PKCS12證書,包含客戶端所需公私鑰及中間證書 rabbitmqTrustStore #服務端JKS格式公鑰
默認 RabbitMQ 配置目錄在 /etc/rabbitmq
,我們創建個證書目錄存放服務端證書
mkdir -p /etc/rabbitmq/ssl #復制服務端必要證書 cp ca/cacert.pem \ server/rabbitmq-server.cert.pem \ server/rabbitmq-server.key.pem /etc/rabbitmq/ssl/
修改 RabbitMQ 配置文件
修改 RabbitMQ 配置文件 /etc/rabbitmq/rabbitmq.config
,此文件默認不存在,需要手動創建
[{rabbit, [
{ssl_listeners, [5671]},
{ssl_options, [
{cacertfile, "/etc/rabbitmq/ssl/cacert.pem"},
{certfile, "/etc/rabbitmq/ssl/rabbitmq-server.cert.pem"},
{keyfile, "/etc/rabbitmq/ssl/rabbitmq-server.key.pem"},
{verify, verify_peer},
{fail_if_no_peer_cert, true},
{ciphers, [
"ECDHE-ECDSA-AES256-GCM-SHA384","ECDHE-RSA-AES256-GCM-SHA384",
"ECDHE-ECDSA-AES256-SHA384","ECDHE-RSA-AES256-SHA384",
"ECDHE-ECDSA-DES-CBC3-SHA","ECDH-ECDSA-AES256-GCM-SHA384",
"ECDH-RSA-AES256-GCM-SHA384","ECDH-ECDSA-AES256-SHA384",
"ECDH-RSA-AES256-SHA384","DHE-DSS-AES256-GCM-SHA384",
"DHE-DSS-AES256-SHA256","AES256-GCM-SHA384",
"AES256-SHA256","ECDHE-ECDSA-AES128-GCM-SHA256",
"ECDHE-RSA-AES128-GCM-SHA256","ECDHE-ECDSA-AES128-SHA256",
"ECDHE-RSA-AES128-SHA256","ECDH-ECDSA-AES128-GCM-SHA256",
"ECDH-RSA-AES128-GCM-SHA256","ECDH-ECDSA-AES128-SHA256",
"ECDH-RSA-AES128-SHA256","DHE-DSS-AES128-GCM-SHA256",
"DHE-DSS-AES128-SHA256","AES128-GCM-SHA256",
"AES128-SHA256","ECDHE-ECDSA-AES256-SHA",
"ECDHE-RSA-AES256-SHA","DHE-DSS-AES256-SHA",
"ECDH-ECDSA-AES256-SHA","ECDH-RSA-AES256-SHA",
"AES256-SHA","ECDHE-ECDSA-AES128-SHA",
"ECDHE-RSA-AES128-SHA","DHE-DSS-AES128-SHA",
"ECDH-ECDSA-AES128-SHA","ECDH-RSA-AES128-SHA","AES128-SHA"
]}
]}
]}].
主要配置項說明:
ssl_listeners
指定 SSL協議的端口號,官方文檔5671
ssl_options
SSL 認證配置項cacertfile
CA 證書位置certfile
公鑰證書位置keyfile
密鑰證書位置verify
verify_peer
客戶端與服務端互相發送證書verify_none
禁用證書交換與校驗fail_if_no_peer_cert
true
不接受沒證書的客戶端連接false
接受沒證書的客戶端連接ciphers
加密器(這個翻譯不知道算不算對?)
重啟 RabbitMQ
#關閉 rabbitmqctl stop #啟動 rabbitmq-server -detached
驗證開啟 SSL 是否成功
使用 Rabbitmq 自帶的診斷工具查看端口監聽狀態及使用協議
#查看監聽 rabbitmq-diagnostics listeners #查看支持的TLS版本 rabbitmq-diagnostics --silent tls_versions
使用 OpenSSL CLI 工具驗證證書是否有效
cd 生成證書的ssl目錄 #使用客戶端證書+CA證書連接RabbitMQ驗證。本處MQ與生成證書是同一主機,其他情況請自行考慮。 openssl s_client -connect localhost:5671 \ -cert client/rabbitmq-client.cert.pem \ -key client/rabbitmq-client.key.pem \ -CAfile ca/cacert.pem
除瞭命令行查看外,還可以通過管理界面查看,不過隻能確定開啟瞭 SSL 監聽,無法確認證書是否通過驗證。
編寫 SpringBoot 代碼連接測試
代碼結構
隻是使用 start.spring.io 生成的 Maven 工程,依賴瞭 WEB 和 AMQP
代碼及配置
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.8</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
啟動類 DemoApplication.java
package com.hellxz.rabbitmq.ssl; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
RabbitMQ客戶端配置類 RabbitFanoutExchangeConfig.java
package com.hellxz.rabbitmq.ssl; import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.core.FanoutExchange; import org.springframework.amqp.core.Queue; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class RabbitFanoutExchangeConfig { public static final String FANOUT_EXCHANGE = "fanout.exchange"; public static final String FANOUT_QUEUE1 = "fanout.queue1"; @Bean(name = FANOUT_EXCHANGE) public FanoutExchange fanoutExchange() { return new FanoutExchange(FANOUT_EXCHANGE, true, false); } @Bean(name = FANOUT_QUEUE1) public Queue fanoutQueue1() { return new Queue(FANOUT_QUEUE1, true, false, false); } @Bean public Binding bindingSimpleQueue1(@Qualifier(FANOUT_QUEUE1) Queue fanoutQueue1, @Qualifier(FANOUT_EXCHANGE) FanoutExchange fanoutExchange) { return BindingBuilder.bind(fanoutQueue1).to(fanoutExchange); } }
發消息測試類 TestController.java
package com.hellxz.rabbitmq.ssl; import org.springframework.amqp.core.Message; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { @Autowired RabbitMQSenderService rabbitMQSenderService; @GetMapping("/test") public void sendMsg() { Message msg = new Message("hello world".getBytes()); try { rabbitMQSenderService.send(RabbitFanoutExchangeConfig.FANOUT_EXCHANGE, RabbitFanoutExchangeConfig.FANOUT_QUEUE1, msg); } catch (Exception e) { e.printStackTrace(); } } }
發消息服務 RabbitMQSenderService.java
package com.hellxz.rabbitmq.ssl; import java.util.UUID; import org.springframework.amqp.core.Message; import org.springframework.amqp.rabbit.connection.CorrelationData; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class RabbitMQSenderService { @Autowired private RabbitTemplate rabbitTemplate; public void send(String exchange, String routingkey, Message message) { CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString()); System.out.println("start send msg : " + message); rabbitTemplate.convertAndSend(exchange, routingkey, message, correlationId); System.out.println("end send msg : " + message); } }
消息接收者 RabbitMQReciver.java
package com.hellxz.rabbitmq.ssl; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component class RabbitMQReciver { @RabbitListener(queues = RabbitFanoutExchangeConfig.FANOUT_QUEUE1) public void reciveLogAll(String msg) throws Exception { System.out.println("received msg:" + msg); } }
配置文件 application.properties
server.port=8085 #基礎配置請根據實際配置 spring.rabbitmq.host=192.168.56.104 #ssl協議端口 spring.rabbitmq.port=5671 spring.rabbitmq.username=admin spring.rabbitmq.password=123456 spring.rabbitmq.virtual-host=/ #啟用rabbitmq客戶端SSL連接 spring.rabbitmq.ssl.enabled=true #客戶端PKCS12證書及密碼 spring.rabbitmq.ssl.key-store=classpath:ssl/rabbitmq-client.keycert.p12 spring.rabbitmq.ssl.key-store-password=654321 #公鑰證書及類型 spring.rabbitmq.ssl.trust-store=classpath:ssl/rabbitmqTrustStore spring.rabbitmq.ssl.trust-store-type=JKS #不校驗主機名,默認開啟會導致連接失敗 spring.rabbitmq.ssl.verify-hostname=false
src/main/resources 下創建 ssl 目錄,將 客戶端證書和服務端JKS公鑰復制到 ssl 目錄中。
執行代碼驗證
運行 DemoApplication.java
,查看控制臺是否有報錯:
如圖,提示創建連接成功,說明已經連接成功瞭。
我們再調用 TestController.java
中定義的 /test
接口
消息發送與消費成功。
參考
https://www.rabbitmq.com/access-control.html
https://www.rabbitmq.com/ssl.html
https://www.rabbitmq.com/troubleshooting-ssl.html
加密器部分參考 https://www.cnblogs.com/ybyn/p/13959135.html
代碼部分參考 Github,地址已不可考
到此這篇關於RabbitMQ開啟SSL與SpringBoot連接測試的文章就介紹到這瞭,更多相關RabbitMQ與SpringBoot連接測試內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- Spring Boot+RabbitMQ 通過fanout模式實現消息接收功能(支持消費者多實例部署)
- SpringBoot集成RabbitMQ和概念介紹
- spring boot整合log4j2及MQ消費處理系統日志示例
- springboot整合消息隊列RabbitMQ
- springBoot整合rabbitmq測試常用模型小結