Jelajahi Sumber

修改短信的BUG

wukai 1 tahun lalu
induk
melakukan
2bf5751065

+ 114 - 114
doc-common/src/main/java/com/doc/common/utils/TencentSmsUtils.java

@@ -1,114 +1,114 @@
-//package com.doc.common.utils;
-//
-//import com.tencentcloudapi.common.Credential;
-//import com.tencentcloudapi.common.profile.ClientProfile;
-//import com.tencentcloudapi.common.profile.HttpProfile;
-//import com.tencentcloudapi.sms.v20210111.SmsClient;
-//import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
-//import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
-//import org.slf4j.Logger;
-//import org.slf4j.LoggerFactory;
-//
-///**
-// * 腾讯云短信接口
-// *
-// * @author wukai
-// * @date 2022-12-19
-// */
-//public class TencentSmsUtils {
-//    private static final String SECRET_ID = "AKIDKTMzLQErDJ8gxwe5F8j36sMZWCbjaBUW";
-//    private static final String SECRET_KEY = "mHVR6UbRzGGzRbU6RUGlzwc2A1astgIi";
-//    private static final String APPID = "1400871556";
-//    private static final String SIGN_NAME = "四川聚聚通科技有限公司";
-//    private static final Logger log = LoggerFactory.getLogger(TencentSmsUtils.class);
-//
-//    /**
-//     * 发送验证码
-//     * 您的当前验证码为:{1},若非本人操作,请勿泄露。
-//     */
-//    public static final String VERIFICATION_CODE = "1647196";
-//    /**
-//     * 发送系统监控信息
-//     * 您的服务器{1} {2}使用率{3},请尽快处理!
-//     */
-//    public static final String PERFORMANCE_MONITORING = "1454381";
-//    /**
-//     * 发送非法操作信息
-//     * 于北京时间{1}拦截到{2}非法操作:{3}
-//     */
-//    public static final String ALARM_ILLEGAL = "1999844";
-//    /**
-//     * 你的扩容申请成功,当前可使用容量为{1},快去看看吧!
-//     */
-//    public static final String CAP_EXPAND_SUCCESS = "2013992";
-//    /**
-//     * 很抱歉!管理员拒绝了您的扩容申请!
-//     */
-//    public static final String CAP_EXPAND_FAIL = "2013988";
-//    /**
-//     * {1}向您分享了名为"{2}"的文件,请注意查收!
-//     */
-//    public static final String FILE_SHARE = "2013984";
-//    /**
-//     * {1}向你发起了名为"{2}”的协作文件,请尽快登录系统查看!
-//     */
-//    public static final String FILE_ACTOR = "2013987";
-//
-//    public static SendSmsResponse sendSms(String phone, String templateId, String[] templateParamSet) throws Exception {
-//        // 实例化一个认证对象cred,入参需要传入腾讯云账户密钥 secretId,secretKey, 前往 API 密钥管理 页面,即可进行获取密钥。此处还需注意密钥对的保密。
-//        Credential cred = new Credential(SECRET_ID, SECRET_KEY);
-//        //  从3.1.16版本开始, 单独设置 HTTP 代理
-//        HttpProfile httpProfile = new HttpProfile();
-//        // get请求(默认为post请求)
-//        httpProfile.setReqMethod("POST");
-//        // 请求连接超时时间,单位为秒(默认60秒)
-//        httpProfile.setConnTimeout(60);
-//        // 指定接入地域域名(默认就近接入)
-//        httpProfile.setEndpoint("sms.tencentcloudapi.com");
-//        ClientProfile clientProfile = new ClientProfile();
-//        // 指定签名算法(默认为HmacSHA256)
-//        clientProfile.setSignMethod("HmacSHA256");
-//        // 自3.1.80版本开始,SDK 支持打印日志。
-//        clientProfile.setHttpProfile(httpProfile);
-//        clientProfile.setDebug(true);
-//        //实例化要请求产品的client对象。
-//        SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);
-//        SendSmsRequest req = new SendSmsRequest();
-//
-//        req.setSmsSdkAppId(APPID);
-//        req.setSignName(SIGN_NAME);
-//        req.setTemplateId(templateId);
-//        req.setTemplateParamSet(templateParamSet);
-//
-//        String preStr = "+86";
-//        String[] phoneNumberSet = new String[]{preStr + phone};
-//        req.setPhoneNumberSet(phoneNumberSet);
-//        String sessionContext = "";
-//        req.setSessionContext(sessionContext);
-//        String extendCode = "";
-//        req.setExtendCode(extendCode);
-//        String senderId = "";
-//        req.setSenderId(senderId);
-//        return client.SendSms(req);
-//    }
-//
-//    public static void send(String templateId, String phone, String... data) {
-//        try {
-//            SendSmsResponse res = sendSms(phone, templateId, data);
-//            log.info(SendSmsResponse.toJsonString(res));
-//        } catch (Exception e) {
-//            e.printStackTrace();
-//            log.error("passSms:{}", e.getMessage());
-//        }
-//    }
-//
-//    public static void main(String[] args) throws Exception {
-////        {"SendStatusSet":[{"SerialNo":"4249:221093732417050495444017078","PhoneNumber":"+8617760370787","Fee":1,"SessionContext":"","Code":"Ok","Message":"send success","IsoCode":"CN"}],"RequestId":"2ed7bbc6-5934-4853-aaa4-a89d0a63ae30"}
-//        String phone = "17760370787";
-//        TencentSmsUtils.send(TencentSmsUtils.ALARM_ILLEGAL, phone, DateUtils.getTime(), "192.168.188.88", "非法访问!");
-////        TencentSmsService.send("1647196", phone, "1234");
-////        TencentSmsService.send("1454381", phone, "192.168.188.11", "CPU", "80%");
-//        //service.alarmIllegal(phone, DateUtils.dateTime(), "192.168.1.11", "非法访问!");
-//        //service.performanceMonitoring(phone, "192.168.1.11", "CPU", "80%");
-//    }
-//}
+package com.doc.common.utils;
+
+import com.tencentcloudapi.common.Credential;
+import com.tencentcloudapi.common.profile.ClientProfile;
+import com.tencentcloudapi.common.profile.HttpProfile;
+import com.tencentcloudapi.sms.v20210111.SmsClient;
+import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
+import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * 腾讯云短信接口
+ *
+ * @author wukai
+ * @date 2022-12-19
+ */
+public class TencentSmsUtils {
+    private static final String SECRET_ID = "AKIDKTMzLQErDJ8gxwe5F8j36sMZWCbjaBUW";
+    private static final String SECRET_KEY = "mHVR6UbRzGGzRbU6RUGlzwc2A1astgIi";
+    private static final String APPID = "1400871556";
+    private static final String SIGN_NAME = "四川聚聚通科技有限公司";
+    private static final Logger log = LoggerFactory.getLogger(TencentSmsUtils.class);
+
+    /**
+     * 发送验证码
+     * 您的当前验证码为:{1},若非本人操作,请勿泄露。
+     */
+    public static final String VERIFICATION_CODE = "1647196";
+    /**
+     * 发送系统监控信息
+     * 您的服务器{1} {2}使用率{3},请尽快处理!
+     */
+    public static final String PERFORMANCE_MONITORING = "1454381";
+    /**
+     * 发送非法操作信息
+     * 于北京时间{1}拦截到{2}非法操作:{3}
+     */
+    public static final String ALARM_ILLEGAL = "1999844";
+    /**
+     * 你的扩容申请成功,当前可使用容量为{1},快去看看吧!
+     */
+    public static final String CAP_EXPAND_SUCCESS = "2013992";
+    /**
+     * 很抱歉!管理员拒绝了您的扩容申请!
+     */
+    public static final String CAP_EXPAND_FAIL = "2013988";
+    /**
+     * {1}向您分享了名为"{2}"的文件,请注意查收!
+     */
+    public static final String FILE_SHARE = "2013984";
+    /**
+     * {1}向你发起了名为"{2}”的协作文件,请尽快登录系统查看!
+     */
+    public static final String FILE_ACTOR = "2013987";
+
+    public static SendSmsResponse sendSms(String phone, String templateId, String[] templateParamSet) throws Exception {
+        // 实例化一个认证对象cred,入参需要传入腾讯云账户密钥 secretId,secretKey, 前往 API 密钥管理 页面,即可进行获取密钥。此处还需注意密钥对的保密。
+        Credential cred = new Credential(SECRET_ID, SECRET_KEY);
+        //  从3.1.16版本开始, 单独设置 HTTP 代理
+        HttpProfile httpProfile = new HttpProfile();
+        // get请求(默认为post请求)
+        httpProfile.setReqMethod("POST");
+        // 请求连接超时时间,单位为秒(默认60秒)
+        httpProfile.setConnTimeout(60);
+        // 指定接入地域域名(默认就近接入)
+        httpProfile.setEndpoint("sms.tencentcloudapi.com");
+        ClientProfile clientProfile = new ClientProfile();
+        // 指定签名算法(默认为HmacSHA256)
+        clientProfile.setSignMethod("HmacSHA256");
+        // 自3.1.80版本开始,SDK 支持打印日志。
+        clientProfile.setHttpProfile(httpProfile);
+        clientProfile.setDebug(true);
+        //实例化要请求产品的client对象。
+        SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);
+        SendSmsRequest req = new SendSmsRequest();
+
+        req.setSmsSdkAppId(APPID);
+        req.setSignName(SIGN_NAME);
+        req.setTemplateId(templateId);
+        req.setTemplateParamSet(templateParamSet);
+
+        String preStr = "+86";
+        String[] phoneNumberSet = new String[]{preStr + phone};
+        req.setPhoneNumberSet(phoneNumberSet);
+        String sessionContext = "";
+        req.setSessionContext(sessionContext);
+        String extendCode = "";
+        req.setExtendCode(extendCode);
+        String senderId = "";
+        req.setSenderId(senderId);
+        return client.SendSms(req);
+    }
+
+    public static void send(String templateId, String phone, String... data) {
+        try {
+            SendSmsResponse res = sendSms(phone, templateId, data);
+            log.info(SendSmsResponse.toJsonString(res));
+        } catch (Exception e) {
+            e.printStackTrace();
+            log.error("passSms:{}", e.getMessage());
+        }
+    }
+
+    public static void main(String[] args) throws Exception {
+//        {"SendStatusSet":[{"SerialNo":"4249:221093732417050495444017078","PhoneNumber":"+8617760370787","Fee":1,"SessionContext":"","Code":"Ok","Message":"send success","IsoCode":"CN"}],"RequestId":"2ed7bbc6-5934-4853-aaa4-a89d0a63ae30"}
+        String phone = "17760370787";
+        TencentSmsUtils.send(TencentSmsUtils.ALARM_ILLEGAL, phone, DateUtils.getTime(), "127-0-0-1", "多次非法访问");
+//        TencentSmsService.send("1647196", phone, "1234");
+//        TencentSmsService.send("1454381", phone, "192.168.188.11", "CPU", "80%");
+        //service.alarmIllegal(phone, DateUtils.dateTime(), "192.168.1.11", "非法访问!");
+        //service.performanceMonitoring(phone, "192.168.1.11", "CPU", "80%");
+    }
+}

+ 1 - 3
doc-framework/src/main/java/com/doc/framework/interceptor/WhiteListInterceptor.java

@@ -5,7 +5,6 @@ import com.doc.common.enums.EventLevel;
 import com.doc.common.utils.StringUtils;
 import com.doc.common.utils.ip.IpUtils;
 import com.doc.system.domain.SysAlarm;
-import com.doc.system.domain.SysConfig;
 import com.doc.system.service.ISysAlarmService;
 import com.doc.system.service.ISysConfigService;
 import lombok.extern.slf4j.Slf4j;
@@ -44,7 +43,6 @@ public class WhiteListInterceptor implements HandlerInterceptor {
         if (IpUtils.isMatchedIp(whiteList, IpUtils.getIpAddr())) {
             return true;
         } else {
-            SysConfig config = configService.selectConfig("alarm.config");
             SysAlarm alarm = new SysAlarm();
             alarm.setAlarmLevel(EventLevel.HIGH.ordinal());
             alarm.setAlarmType(AlarmType.IP_ILLEGAL.ordinal());
@@ -70,4 +68,4 @@ public class WhiteListInterceptor implements HandlerInterceptor {
         } catch (IOException ignored) {
         }
     }
-}
+}

+ 24 - 4
doc-system/src/main/java/com/doc/system/service/impl/SysAlarmServiceImpl.java

@@ -5,8 +5,11 @@ import com.doc.common.enums.AlarmType;
 import com.doc.common.enums.EventLevel;
 import com.doc.common.enums.SmsType;
 import com.doc.common.utils.DateUtils;
+import com.doc.common.utils.Tools;
+import com.doc.common.utils.encrypt.Sm2Util;
 import com.doc.sms.service.ISmsService;
 import com.doc.system.domain.SysAlarm;
+import com.doc.system.domain.SysAlarmSms;
 import com.doc.system.domain.SysConfig;
 import com.doc.system.domain.vo.AlarmVO;
 import com.doc.system.mapper.SysAlarmMapper;
@@ -73,7 +76,11 @@ public class SysAlarmServiceImpl implements ISysAlarmService {
         int r = sysAlarmMapper.insertSysAlarm(sysAlarm);
         SysConfig config = sysConfigService.selectConfig("alarm.config");
         try {
-            AlarmVO vo = JSONObject.parseObject(config.getConfigValue(), AlarmVO.class);
+            String value = config.getConfigValue();
+            //先解压缩,再SM2解密
+            value = Tools.uncompress(value);
+            value = Sm2Util.decrypt(value);
+            AlarmVO vo = JSONObject.parseObject(value, AlarmVO.class);
             String open = "1";
             //短信开启状态为1
             if (!open.equals(vo.getStatus())) {
@@ -88,6 +95,7 @@ public class SysAlarmServiceImpl implements ISysAlarmService {
             //2.在查询是否满足发送短信的条件
             if (type == AlarmType.MONITOR_HDD.ordinal()) {
                 if (Double.parseDouble(sysAlarm.getRemark()) > vo.getHdd_used()) {
+                    insertRecord(sysAlarm);
                     //发送短信
                     smsService.send(SmsType.PERFORMANCE_MONITORING, vo.getPhone(), "硬盘", "空间", sysAlarm.getRemark() + "%");
                 }
@@ -99,9 +107,11 @@ public class SysAlarmServiceImpl implements ISysAlarmService {
             if (type == AlarmType.IP_ILLEGAL.ordinal()) {
                 search.setRemark(sysAlarm.getRemark());
                 list = sysAlarmMapper.selectSysAlarmList(search);
+                String content = sysAlarm.getRemark().replace(".", "-");
                 if (list.size() > vo.getIp_times()) {
+                    insertRecord(sysAlarm);
                     //发送短信
-                    smsService.send(SmsType.ALARM_ILLEGAL, vo.getPhone(), DateUtils.getTime(), sysAlarm.getRemark(), "非法IP访问");
+                    smsService.send(SmsType.ALARM_ILLEGAL, vo.getPhone(), DateUtils.getTime(), content, "多次非法访问");
                 }
             } else {
                 list = sysAlarmMapper.selectSysAlarmList(search);
@@ -114,6 +124,7 @@ public class SysAlarmServiceImpl implements ISysAlarmService {
                         }
                     });
                     if (times.intValue() > vo.getCpu_times()) {
+                        insertRecord(sysAlarm);
                         smsService.send(SmsType.PERFORMANCE_MONITORING, vo.getPhone(), "CPU", "当前", sysAlarm.getRemark() + "%");
                     }
                 }
@@ -127,6 +138,7 @@ public class SysAlarmServiceImpl implements ISysAlarmService {
                         }
                     });
                     if (times.intValue() > vo.getMem_times()) {
+                        insertRecord(sysAlarm);
                         smsService.send(SmsType.PERFORMANCE_MONITORING, vo.getPhone(), "内存", "当前", sysAlarm.getRemark() + "%");
                     }
                 }
@@ -134,12 +146,20 @@ public class SysAlarmServiceImpl implements ISysAlarmService {
             }
             //3.如果满足,则发送短信
 
-        } catch (
-                Exception ignored) {
+        } catch (Exception e) {
+            e.printStackTrace();
         }
         return r;
     }
 
+    private void insertRecord(SysAlarm alarm) {
+        SysAlarmSms alarmSms = new SysAlarmSms();
+        alarmSms.setSendTime(new Date());
+        alarmSms.setAlarmType((long) alarm.getAlarmType());
+        alarmSms.setAlartKey(alarm.getRemark());
+        alarmSmsService.insertSysAlarmSms(alarmSms);
+    }
+
     /**
      * 修改系统告警
      *