diff --git a/threadpool/message/src/main/java/cn/hippo4j/message/platform/LarkSendMessageHandler.java b/threadpool/message/src/main/java/cn/hippo4j/message/platform/LarkSendMessageHandler.java index dce2e378..1a16f3be 100644 --- a/threadpool/message/src/main/java/cn/hippo4j/message/platform/LarkSendMessageHandler.java +++ b/threadpool/message/src/main/java/cn/hippo4j/message/platform/LarkSendMessageHandler.java @@ -33,7 +33,13 @@ import lombok.Data; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.codec.binary.Base64; +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; +import java.nio.charset.StandardCharsets; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Arrays; @@ -81,7 +87,16 @@ public class LarkSendMessageHandler implements SendMessageHandler { } else { larkAlarmTxt = StringUtil.replace(larkAlarmTxt, larkAlarmTimeoutReplaceJson, ""); } + + String timestamp = String.valueOf(System.currentTimeMillis()).substring(0, 10); + String sign = ""; + if (notifyConfig.getSecret() != null) { + sign = genSign(notifyConfig.getSecret(), timestamp); + } + String text = String.format(larkAlarmTxt, + timestamp, + sign, alarmNotifyRequest.getActive(), alarmNotifyRequest.getNotifyTypeEnum(), alarmNotifyRequest.getThreadPoolId(), @@ -154,6 +169,20 @@ public class LarkSendMessageHandler implements SendMessageHandler { } } + /** + * generate Signature + */ + private String genSign(String secret, String timestamp) throws NoSuchAlgorithmException, InvalidKeyException { + // geneSign + String stringToSign = timestamp + "\n" + secret; + + // encode + Mac mac = Mac.getInstance("HmacSHA256"); + mac.init(new SecretKeySpec(stringToSign.getBytes(StandardCharsets.UTF_8), "HmacSHA256")); + byte[] signData = mac.doFinal(new byte[]{}); + return new String(Base64.encodeBase64(signData)); + } + /** * Lark robot response. */ diff --git a/threadpool/message/src/main/resources/message/robot/dynamic-thread-pool/lark-alarm.json b/threadpool/message/src/main/resources/message/robot/dynamic-thread-pool/lark-alarm.json index 2bc88c3a..a64661ae 100644 --- a/threadpool/message/src/main/resources/message/robot/dynamic-thread-pool/lark-alarm.json +++ b/threadpool/message/src/main/resources/message/robot/dynamic-thread-pool/lark-alarm.json @@ -1,5 +1,7 @@ { "msg_type": "interactive", + "timestamp": "%s", + "sign": "%s", "card": { "config": { "wide_screen_mode": true