Modify the token name and add the pin signature (#166)

pull/170/head
chen.ma 2 years ago
parent ea2e6b8de5
commit abf45e3d18

@ -53,6 +53,12 @@
</exclusions>
<optional>true</optional>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.14</version>
</dependency>
</dependencies>
<build>

@ -43,6 +43,11 @@ public class NotifyConfigDTO {
*/
private String secretKey;
/**
*
*/
private String secret;
/**
*
*/

@ -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<AlarmNotifyReq
DateUtil.now()
);
execute(notifyConfig.getSecretKey(), DingAlarmConstants.DING_ALARM_TITLE, text, Lists.newArrayList(receives));
execute(notifyConfig, DingAlarmConstants.DING_ALARM_TITLE, text, Lists.newArrayList(receives));
}
@Override
@ -148,25 +156,35 @@ public class DingSendMessageHandler implements SendMessageHandler<AlarmNotifyReq
DateUtil.now()
);
execute(notifyConfig.getSecretKey(), DingAlarmConstants.DING_NOTICE_TITLE, text, Lists.newArrayList(receives));
execute(notifyConfig, DingAlarmConstants.DING_NOTICE_TITLE, text, Lists.newArrayList(receives));
}
private void execute(String secretKey, String title, String text, List<String> mobiles) {
String serverUrl = DingAlarmConstants.DING_ROBOT_SERVER_URL + secretKey;
private void execute(NotifyConfigDTO notifyConfig, String title, String text, List<String> 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 + "&timestamp=" + 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) {

@ -18,7 +18,19 @@ public class NotifyPlatformProperties {
/**
* Secret key.
* {@link NotifyPlatformProperties#token}
*/
@Deprecated
private String secretKey;
/**
* Token.
*/
private String token;
/**
* Secret.
*/
private String secret;
}

@ -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();
}
}

@ -26,7 +26,7 @@
</modules>
<properties>
<revision>1.2.0</revision>
<revision>1.2.0-RC2</revision>
<java.version>1.8</java.version>
<dozer.version>6.5.0</dozer.version>

Loading…
Cancel
Save