From 0b967b48348b6fc29686cc205082f9e435dc506a Mon Sep 17 00:00:00 2001 From: AprilWind <2100166581@qq.com> Date: Fri, 26 Jul 2024 10:23:56 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=E5=8A=A0=E7=AD=BE?= =?UTF-8?q?=EF=BC=8C=E6=94=AF=E6=8C=81=E9=92=89=E9=92=89=EF=BC=8C=E9=A3=9E?= =?UTF-8?q?=E4=B9=A6=EF=BC=8C=E4=BC=81=E4=B8=9A=E5=BE=AE=E4=BF=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/properties/NotifyProperties.java | 7 +- .../monitor/admin/notifier/InfoNotifier.java | 95 +++++++++++++++---- .../src/main/resources/application.yml | 1 + 3 files changed, 86 insertions(+), 17 deletions(-) diff --git a/ruoyi-extend/ruoyi-monitor-admin/src/main/java/org/dromara/monitor/admin/config/properties/NotifyProperties.java b/ruoyi-extend/ruoyi-monitor-admin/src/main/java/org/dromara/monitor/admin/config/properties/NotifyProperties.java index 870122efe..40f7172a2 100644 --- a/ruoyi-extend/ruoyi-monitor-admin/src/main/java/org/dromara/monitor/admin/config/properties/NotifyProperties.java +++ b/ruoyi-extend/ruoyi-monitor-admin/src/main/java/org/dromara/monitor/admin/config/properties/NotifyProperties.java @@ -63,7 +63,12 @@ public class NotifyProperties { /** * 0默认 1密码 2签名密钥 */ - private String type; + private String type = "0"; + + /** + * 签名密钥 + */ + private String secret; /** * Post地址 diff --git a/ruoyi-extend/ruoyi-monitor-admin/src/main/java/org/dromara/monitor/admin/notifier/InfoNotifier.java b/ruoyi-extend/ruoyi-monitor-admin/src/main/java/org/dromara/monitor/admin/notifier/InfoNotifier.java index da917fe12..c944db2ba 100644 --- a/ruoyi-extend/ruoyi-monitor-admin/src/main/java/org/dromara/monitor/admin/notifier/InfoNotifier.java +++ b/ruoyi-extend/ruoyi-monitor-admin/src/main/java/org/dromara/monitor/admin/notifier/InfoNotifier.java @@ -12,10 +12,15 @@ import org.springframework.context.event.EventListener; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; import java.net.URI; +import java.net.URLEncoder; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; +import java.nio.charset.StandardCharsets; +import java.util.Base64; import java.util.HashMap; import java.util.Map; @@ -89,7 +94,7 @@ public class InfoNotifier { String message = StringUtils.format(webHook.getTemplate(), title, notifier.getRegistName(), notifier.getInstanceId(), notifier.getStatus(), notifier.getServiceUrl()); try { - sendWebHookMessage(webHook.getUrl(), title, message); + sendWebHookMessage(webHook, title, message); log.info("WebHook消息已发送至: {}", webHook.getUrl()); } catch (Exception e) { log.error("WebHook消息发送失败: ", e); @@ -97,32 +102,90 @@ public class InfoNotifier { } } - private void sendWebHookMessage(String url, String title, String markdownMessage) throws Exception { - // 构造消息体 + /** + * 发送WebHook消息 + * + * @param webHook WebHook配置信息 + * @param title 消息标题 + * @param markdownMessage 消息内容 + * @throws Exception 处理过程中可能抛出的异常 + */ + private void sendWebHookMessage(NotifyProperties.WebHook webHook, String title, String markdownMessage) throws Exception { + String jsonMessage = createJsonMessage(title, markdownMessage); + HttpClient client = HttpClient.newHttpClient(); + HttpRequest request = createHttpRequest(webHook, jsonMessage); + HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + handleResponse(response); + } + + /** + * 创建消息体的JSON字符串 + * + * @param title 消息标题 + * @param markdownMessage 消息内容 + * @return JSON格式的消息体 + * @throws Exception 处理过程中可能抛出的异常 + */ + private String createJsonMessage(String title, String markdownMessage) throws Exception { Map messageBody = new HashMap<>(); messageBody.put("msgtype", "markdown"); - Map markdownContent = new HashMap<>(); markdownContent.put("title", title + "通知"); markdownContent.put("text", markdownMessage); messageBody.put("markdown", markdownContent); - - // 将消息体转换为JSON字符串 ObjectMapper objectMapper = new ObjectMapper(); - String jsonMessage = objectMapper.writeValueAsString(messageBody); + return objectMapper.writeValueAsString(messageBody); + } - // 创建HTTP客户端 - HttpClient client = HttpClient.newHttpClient(); - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI(url)) + /** + * 根据WebHook配置创建HTTP请求 + * + * @param webHook WebHook配置信息 + * @param jsonMessage JSON格式的消息体 + * @return 创建的HTTP请求 + * @throws Exception 处理过程中可能抛出的异常 + */ + private HttpRequest createHttpRequest(NotifyProperties.WebHook webHook, String jsonMessage) throws Exception { + HttpRequest.Builder requestBuilder = HttpRequest.newBuilder() .header("Content-Type", "application/json") - .POST(HttpRequest.BodyPublishers.ofString(jsonMessage)) - .build(); + .POST(HttpRequest.BodyPublishers.ofString(jsonMessage)); - // 发送请求 - HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + if ("2".equals(webHook.getType())) { + String signedUrl = generateSignedUrl(webHook); + requestBuilder.uri(new URI(signedUrl)); + } else { + requestBuilder.uri(new URI(webHook.getUrl())); + } - // 处理响应 + return requestBuilder.build(); + } + + /** + * 生成带签名的URL + * + * @param webHook WebHook配置信息 + * @return 带签名的URL + * @throws Exception 处理过程中可能抛出的异常 + */ + private String generateSignedUrl(NotifyProperties.WebHook webHook) throws Exception { + long timestamp = System.currentTimeMillis(); + String secret = webHook.getSecret(); + String stringToSign = timestamp + "\n" + secret; + + Mac mac = Mac.getInstance("HmacSHA256"); + mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256")); + byte[] signData = mac.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8)); + String sign = URLEncoder.encode(Base64.getEncoder().encodeToString(signData), StandardCharsets.UTF_8); + + return String.format("%s×tamp=%d&sign=%s", webHook.getUrl(), timestamp, sign); + } + + /** + * 处理HTTP响应 + * + * @param response HTTP响应 + */ + private void handleResponse(HttpResponse response) { if (response.statusCode() == 200) { log.info("WebHook消息发送成功"); } else { diff --git a/ruoyi-extend/ruoyi-monitor-admin/src/main/resources/application.yml b/ruoyi-extend/ruoyi-monitor-admin/src/main/resources/application.yml index 18d845caa..f6730ce6d 100644 --- a/ruoyi-extend/ruoyi-monitor-admin/src/main/resources/application.yml +++ b/ruoyi-extend/ruoyi-monitor-admin/src/main/resources/application.yml @@ -84,6 +84,7 @@ notify: enabled: false type: 0 url: + secret: template: | ### {}通知 - **服务名**: {}