diff --git a/hippo4j-common/pom.xml b/hippo4j-common/pom.xml index 2564b9e0..ba5d8651 100644 --- a/hippo4j-common/pom.xml +++ b/hippo4j-common/pom.xml @@ -53,6 +53,12 @@ true + + + commons-codec + commons-codec + 1.14 + diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/notify/NotifyConfigDTO.java b/hippo4j-common/src/main/java/cn/hippo4j/common/notify/NotifyConfigDTO.java index 84d5e48c..4e96e95d 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/notify/NotifyConfigDTO.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/notify/NotifyConfigDTO.java @@ -43,6 +43,11 @@ public class NotifyConfigDTO { */ private String secretKey; + /** + * 加签 + */ + private String secret; + /** * 报警间隔 */ diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/notify/platform/DingSendMessageHandler.java b/hippo4j-common/src/main/java/cn/hippo4j/common/notify/platform/DingSendMessageHandler.java index 52f77d9a..a780688b 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/notify/platform/DingSendMessageHandler.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/notify/platform/DingSendMessageHandler.java @@ -1,6 +1,9 @@ package cn.hippo4j.common.notify.platform; -import cn.hippo4j.common.notify.*; +import cn.hippo4j.common.notify.NotifyConfigDTO; +import cn.hippo4j.common.notify.NotifyPlatformEnum; +import cn.hippo4j.common.notify.NotifyTypeEnum; +import cn.hippo4j.common.notify.SendMessageHandler; import cn.hippo4j.common.notify.request.AlarmNotifyRequest; import cn.hippo4j.common.notify.request.ChangeParameterNotifyRequest; import cn.hippo4j.common.toolkit.StringUtil; @@ -13,7 +16,12 @@ import com.google.common.base.Joiner; import com.google.common.collect.Lists; import com.taobao.api.ApiException; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.codec.binary.Base64; +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Objects; @@ -102,7 +110,7 @@ public class DingSendMessageHandler implements SendMessageHandler mobiles) { - String serverUrl = DingAlarmConstants.DING_ROBOT_SERVER_URL + secretKey; + private void execute(NotifyConfigDTO notifyConfig, String title, String text, List mobiles) { + String serverUrl = DingAlarmConstants.DING_ROBOT_SERVER_URL + notifyConfig.getSecretKey(); + String secret = notifyConfig.getSecret(); + if (StringUtil.isNotBlank(secret)) { + long timestamp = System.currentTimeMillis(); + String stringToSign = timestamp + "\n" + secret; + try { + 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(new String(Base64.encodeBase64(signData)), StandardCharsets.UTF_8.name()); + serverUrl = serverUrl + "×tamp=" + timestamp + "&sign=" + sign; + } catch (Exception ex) { + log.error("Failed to sign the message sent by nailing.", ex); + } + } DingTalkClient dingTalkClient = new DefaultDingTalkClient(serverUrl); OapiRobotSendRequest request = new OapiRobotSendRequest(); request.setMsgtype("markdown"); - OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown(); markdown.setTitle(title); markdown.setText(text); - OapiRobotSendRequest.At at = new OapiRobotSendRequest.At(); at.setAtMobiles(mobiles); - request.setAt(at); request.setMarkdown(markdown); - try { dingTalkClient.execute(request); } catch (ApiException ex) { diff --git a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/config/NotifyPlatformProperties.java b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/config/NotifyPlatformProperties.java index 157fff36..06976848 100644 --- a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/config/NotifyPlatformProperties.java +++ b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/config/NotifyPlatformProperties.java @@ -18,7 +18,19 @@ public class NotifyPlatformProperties { /** * Secret key. + * {@link NotifyPlatformProperties#token} */ + @Deprecated private String secretKey; + /** + * Token. + */ + private String token; + + /** + * Secret. + */ + private String secret; + } diff --git a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/notify/CoreNotifyConfigBuilder.java b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/notify/CoreNotifyConfigBuilder.java index 320797e0..65b528bd 100644 --- a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/notify/CoreNotifyConfigBuilder.java +++ b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/notify/CoreNotifyConfigBuilder.java @@ -3,6 +3,7 @@ package cn.hippo4j.core.starter.notify; import cn.hippo4j.common.api.NotifyConfigBuilder; import cn.hippo4j.common.notify.AlarmControlHandler; import cn.hippo4j.common.notify.NotifyConfigDTO; +import cn.hippo4j.common.toolkit.StringUtil; import cn.hippo4j.core.starter.config.BootstrapCoreProperties; import cn.hippo4j.core.starter.config.ExecutorProperties; import cn.hippo4j.core.starter.config.NotifyPlatformProperties; @@ -60,7 +61,8 @@ public class CoreNotifyConfigBuilder implements NotifyConfigBuilder { notifyConfig.setPlatform(platformProperties.getPlatform()); notifyConfig.setTpId(threadPoolId); notifyConfig.setType("ALARM"); - notifyConfig.setSecretKey(platformProperties.getSecretKey()); + notifyConfig.setSecret(platformProperties.getSecret()); + notifyConfig.setSecretKey(getToken(platformProperties)); int interval = Optional.ofNullable(executor.getNotify()) .map(each -> each.getInterval()) .orElseGet(() -> bootstrapCoreProperties.getAlarmInterval() != null ? bootstrapCoreProperties.getAlarmInterval() : 5); @@ -78,7 +80,8 @@ public class CoreNotifyConfigBuilder implements NotifyConfigBuilder { notifyConfig.setPlatform(platformProperties.getPlatform()); notifyConfig.setTpId(threadPoolId); notifyConfig.setType("CONFIG"); - notifyConfig.setSecretKey(platformProperties.getSecretKey()); + notifyConfig.setSecretKey(getToken(platformProperties)); + notifyConfig.setSecret(platformProperties.getSecret()); notifyConfig.setReceives(buildReceive(executor, platformProperties)); changeNotifyConfigs.add(notifyConfig); } @@ -115,4 +118,8 @@ public class CoreNotifyConfigBuilder implements NotifyConfigBuilder { return receive; } + private String getToken(NotifyPlatformProperties platformProperties) { + return StringUtil.isNotBlank(platformProperties.getToken()) ? platformProperties.getToken() : platformProperties.getSecretKey(); + } + } diff --git a/pom.xml b/pom.xml index 1246aade..ff7c66a3 100644 --- a/pom.xml +++ b/pom.xml @@ -26,7 +26,7 @@ - 1.2.0 + 1.2.0-RC2 1.8 6.5.0