Commit 7b5c8a80 authored by qinxunjia's avatar qinxunjia

签名校验,模板名称格式限制

parent c97c7204
package com.bgy.sms.channel.api;
import com.alibaba.fastjson.JSONObject;
import com.bgy.sms.channel.dmHub.config.DmHubConfig;
import com.bgy.sms.channel.dto.*;
import com.bgy.sms.config.ResponseCode;
import com.bgy.sms.service.MessageService;
import com.bgy.util.Md5Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
@SuppressWarnings("Duplicates")
@RestController
@RequestMapping()
......@@ -25,10 +29,14 @@ public class DmHubApi {
* @return
*/
@PostMapping("/sms/template")
public DmHubResponse template(@RequestBody DmHubTemplateRequest params) {
log.info("**********创建模板接口入参*******:{}", JSONObject.toJSONString(params));
public DmHubResponse template(@RequestBody DmHubTemplateRequest params, String appId, String timestamp, String signature) {
log.info("**********创建模板接口入参*******:{},\r\n appId:{},timestamp:{},signature:{}", JSONObject.toJSONString(params), appId, timestamp, signature);
DmHubResponse response;
try {
boolean checkResult = this.checkSignature(timestamp, signature);
if (!checkResult) {
return new DmHubResponse("555", "签名校验不通过");
}
response = messageService.createTemplate(params);
} catch (Exception e) {
log.error("创建模板短信异常", e);
......@@ -44,10 +52,14 @@ public class DmHubApi {
* @return
*/
@PostMapping("/sms/verifyCode")
public DmHubResponse verifyCode(@RequestBody DmHubCodeRequest params) {
log.info("**********发送短信验证码*******:{}", JSONObject.toJSONString(params));
public DmHubResponse verifyCode(@RequestBody DmHubCodeRequest params, String appId, String timestamp, String signature) {
log.info("**********发送短信验证码*******:{},\r\n appId:{},timestamp:{},signature:{}", JSONObject.toJSONString(params), appId, timestamp, signature);
DmHubResponse response;
try {
boolean checkResult = this.checkSignature(timestamp, signature);
if (!checkResult) {
return new DmHubResponse("555", "签名校验不通过");
}
response = messageService.sendCode(params);
} catch (Exception e) {
log.error("发送验证码短信异常", e);
......@@ -64,10 +76,14 @@ public class DmHubApi {
* @return
*/
@PostMapping("/sms/batch")
public DmHubResponse batch(@RequestBody DmHubBatchSendRequest request) {
log.info("**********批量发送入参*******:{}", JSONObject.toJSONString(request));
public DmHubResponse batch(@RequestBody DmHubBatchSendRequest request, String appId, String timestamp, String signature) {
log.info("**********批量发送入参*******:{},\r\n appId:{},timestamp:{},signature:{}", JSONObject.toJSONString(request), appId, timestamp, signature);
DmHubResponse response;
try {
boolean checkResult = this.checkSignature(timestamp, signature);
if (!checkResult) {
return new DmHubResponse("555", "签名校验不通过");
}
response = messageService.batchSendOneByOne(request);
} catch (Exception e) {
log.error("发送批量短信异常", e);
......@@ -77,16 +93,29 @@ public class DmHubApi {
return response;
}
public boolean checkSignature(String timestamp, String signature) {
String encrypt = Md5Util.encrypt(DmHubConfig.appId + DmHubConfig.appSecret + timestamp);
if (!encrypt.equals(signature)) {
return false;
}
return true;
}
/**
* 发送单条(通知或营销类)
*
* @return
*/
@RequestMapping("/sms/send")
public DmHubResponse send(@RequestBody DmHubSendRequest request) {
log.info("**********单条发送入参*******:{}", JSONObject.toJSONString(request));
public DmHubResponse send(@RequestBody DmHubSendRequest request, String appId, String timestamp, String signature) {
log.info("**********单条发送入参*******:{},\r\n appId:{},timestamp:{},signature:{}", JSONObject.toJSONString(request), appId, timestamp, signature);
DmHubResponse response;
try {
boolean checkResult = this.checkSignature(timestamp, signature);
if (!checkResult) {
return new DmHubResponse("555", "签名校验不通过");
}
response = messageService.send(request);
} catch (Exception e) {
log.error("发送单条短信异常", e);
......
......@@ -5,8 +5,7 @@ import com.bgy.sms.channel.bgy.dto.CLBizResponse;
public interface BgySmsService {
CLBizResponse sendSms(String mobile, String content) throws Exception;
CLBizResponse sendSms(String mobile, String content, String areaId) throws Exception;
}
......@@ -39,10 +39,10 @@ public class BgySmsServiceImpl implements BgySmsService {
private DmHubService dmHubService;
@Override
public CLBizResponse sendSms(String mobile, String content) throws Exception {
public CLBizResponse sendSms(String mobile, String content, String areaId) throws Exception {
log.info("进入碧桂园短信发送接口");
String appId = BgySMSConfig.appId;
String areaId = BgySMSConfig.areaId;
// String areaId = BgySMSConfig.areaId;
String securityCode = BgySMSConfig.securityCode;
String url = BgySMSConfig.url;
String api = BgySMSConfig.api;
......
......@@ -13,14 +13,14 @@ public class DmHubConfig {
public static String applicationKey;
public static String tokenUrl;
public static String report;
public static String appId;
public static String appSecret;
@Value("${system.config.dmHub.applicationId}")
public void setApplicationId(String applicationId) {
DmHubConfig.applicationId = applicationId;
}
@Value("{system.config.dmHub.applicationKey}")
public void setApplicationKey(String applicationKey) {
DmHubConfig.applicationKey = applicationKey;
......@@ -36,5 +36,15 @@ public class DmHubConfig {
DmHubConfig.report = report;
}
@Value("{system.config.dmHub.appId}")
public void setAppId(String appId) {
DmHubConfig.appId = appId;
}
@Value("{system.config.dmHub.appSecret}")
public void setAppSecret(String appSecret) {
DmHubConfig.appSecret = appSecret;
}
}
......@@ -63,7 +63,6 @@ public class DmHubServiceImpl implements DmHubService {
redisTemplate.opsForValue().set(TOKEN_KEY, accessToken, exTime, TimeUnit.SECONDS);
return accessToken;
} else {
// TODO 系统告警,获取API接口TOKEN失败。
return null;
}
}
......
......@@ -20,14 +20,6 @@ public interface MessageService {
*/
DmHubResponse send(DmHubSendRequest requestDTO);
/**
* DM hub批量发送短信接口
*
* @param requestDTO
* @return
*/
DmHubResponse batchSend(DmHubBatchSendRequest requestDTO);
/**
* DM hub批量发送短信接口
*
......
......@@ -6,8 +6,8 @@ import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.bgy.sms.channel.bgy.config.BgySMSConfig;
import com.bgy.sms.channel.bgy.dto.CLBizResponse;
import com.bgy.sms.channel.bgy.service.BgySmsService;
import com.bgy.sms.config.ResponseCode;
import com.bgy.sms.channel.dto.*;
import com.bgy.sms.config.ResponseCode;
import com.bgy.sms.repository.domain.DmBatchInfo;
import com.bgy.sms.repository.domain.SmsTemplateInfo;
import com.bgy.sms.repository.domain.SysBatchInfo;
......@@ -16,7 +16,6 @@ import com.bgy.sms.service.MessageService;
import com.bgy.sms.service.SmsTemplateService;
import com.bgy.sms.service.bean.TemplateChangeBean;
import com.bgy.util.id.IdHandler;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -105,7 +104,6 @@ public class MessageServiceImpl implements MessageService {
* @return
*/
private TemplateChangeBean dmHub2BgyTemplateSend(String content) {
//正则获取${***} 和 ${surl***'}格式的内容,其中:((?:\$\{surl.*'\}))獲取${surl***'}格式,\$\{[^}]+\} 获取${***}格式,顺序不能变,否则不能完成匹配格式一
String regex = "((?:\\$\\{surl.*'\\}))|\\$\\{[^}]+\\}";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(content);
......@@ -189,6 +187,13 @@ public class MessageServiceImpl implements MessageService {
}
// 2、根据请求信息获取用户的手机号码(此处不考虑配置DMHUB系统不是SMS的情况),直接拿请求体中的_audienceId字段值,该值为手机号码
// 模板参数占位符
String templateName = templateInfo.getTemplateName();
String[] split = templateName.split("_");
if (split == null || split.length <= 1) {
return new DmHubResponse("999", "短信模板名称命名不规范,请使用:项目ID_模板名称格式");
}
String areaId = split[0];
String params = templateInfo.getParams();
JSONArray paramsArr = JSONArray.parseArray(params);
String smsType = templateInfo.getType();
......@@ -239,7 +244,7 @@ public class MessageServiceImpl implements MessageService {
String upContent = templateInfo.getUpContent();
content = getMsg(upContent, paramList);
}
response = bgySmsService.sendSms(mobile, content);
response = bgySmsService.sendSms(mobile, content, areaId);
String code = response.getCode();
if (code.equals(ResponseCode.SUCCESS.getCode())) {
return new DmHubResponse(ResponseCode.SUCCESS);
......@@ -260,7 +265,7 @@ public class MessageServiceImpl implements MessageService {
CLBizResponse response = new CLBizResponse();
String content = "您的验证码是:" + code;
try {
response = bgySmsService.sendSms(mobile, content);
response = bgySmsService.sendSms(mobile, content, BgySMSConfig.areaId);
String retCode = response.getCode();
if (retCode.equals(ResponseCode.SUCCESS.getCode())) {
return new DmHubResponse(ResponseCode.SUCCESS);
......@@ -288,121 +293,11 @@ public class MessageServiceImpl implements MessageService {
return upContent;
}
@Override
public DmHubResponse batchSend(DmHubBatchSendRequest request) {
String batchId = request.getBatchId(); // DM hub 批次号
String templateId = request.getTemplateId(); // 此次短信对应的模板id
String audienceIdType = request.getAudienceIdType();
List<JSONObject> data = request.getData();
SmsTemplateInfo templateInfo = smsTemplateService.selectOne(new EntityWrapper<SmsTemplateInfo>().eq("dm_template_id", templateId));
if (null == templateInfo) {
return new DmHubResponse(ResponseCode.SYSTEM_ERROR);
}
// 模板参数占位符
String params = templateInfo.getParams();
JSONArray paramsArr = JSONArray.parseArray(params);
// 封装参数
// StringBuilder sendParams = new StringBuilder();
Map<String, List<String>> paramsMap = new HashMap();
int i = 0;
for (JSONObject json : data) {
i++;
Set<String> keys = json.keySet();
String mobile = json.getString("_audienceId");
json.remove("name");
json.remove("id");
json.remove("_audienceId");
if (!json.isEmpty()) {
for (Object parm : paramsArr) {
List<String> list = new ArrayList<>();
String s = parm.toString();
for (String key : keys) {
String newKey = key;
Object value = json.get(key);
if (value instanceof JSONObject || value instanceof Map) {
StringBuilder keyBuffer = new StringBuilder(key);
value = getKeyValue(value, keyBuffer);
newKey = keyBuffer.toString();
}
if (s.contains(newKey)) {
list.add(value.toString());
}
}
paramsMap.put(mobile, list);
}
} else {
if (i != data.size()) {
paramsMap.put(mobile, new ArrayList<>());
}
}
}
DmBatchInfo dmInfo = new DmBatchInfo();
dmInfo.setDmBatchId(batchId);
dmInfo.setDmTemplateId(templateId);
dmInfo.setSmsNum(data.size());
dmInfo.setId(IdHandler.nextId());
dmInfo.setDateCreated(new Date());
dmInfo.setLastUpdated(new Date());
dmBatchService.insert(dmInfo);
SysBatchInfo info = new SysBatchInfo();
info.setId(info.getBatchId());
info.setDmBatchId(batchId);
info.setSmsNum(data.size());
info.setDmTemplateId(templateId);
Long sysBatchId = IdHandler.nextId();
info.setBatchId(sysBatchId);
info.setDateCreated(new Date());
info.setLastUpdated(new Date());
sysBatchService.insert(info);
CLBizResponse response = new CLBizResponse();
if (paramsArr == null || paramsArr.isEmpty()) {
try {
String code = response.getCode();
String msg = response.getMsg();
// TODO 根据DM hub需要的返回数据封装
if (ResponseCode.SUCCESS.getCode().equals(code)) {
return new DmHubResponse(ResponseCode.SUCCESS);
} else {
return new DmHubResponse(code, msg);
}
} catch (Exception e) {
log.error("调用碧桂园逻辑错误", e);
// TODO 根据DM hub需要的返回数据封装
return new DmHubResponse(ResponseCode.SYSTEM_ERROR);
}
} else {
// 固定短信
Set<Map.Entry<String, List<String>>> entries = paramsMap.entrySet();
try {
// 此处不做拆分操作
for (Map.Entry<String, List<String>> entry : entries) {
String mobile = entry.getKey().toString();
List<String> paramsList = entry.getValue();
String sendMsg = getMsg(templateInfo.getUpContent(), paramsList);
response = bgySmsService.sendSms(mobile, sendMsg);
}
return new DmHubResponse(ResponseCode.SUCCESS);
} catch (Exception e) {
log.error("调用碧桂园逻辑错误:", e);
return new DmHubResponse(ResponseCode.SYSTEM_ERROR);
}
}
}
@Override
public DmHubResponse batchSendOneByOne(DmHubBatchSendRequest request) {
String batchId = request.getBatchId(); // DM hub 批次号
String templateId = request.getTemplateId(); // 此次短信对应的模板id
String audienceIdType = request.getAudienceIdType();
List<JSONObject> data = request.getData();
SmsTemplateInfo templateInfo = smsTemplateService.selectOne(new EntityWrapper<SmsTemplateInfo>().eq("dm_template_id", templateId));
......@@ -410,6 +305,12 @@ public class MessageServiceImpl implements MessageService {
return new DmHubResponse("999", "短信插件未获取到模板信息");
}
String smsType = templateInfo.getType();
String templateName = templateInfo.getTemplateName();
String[] split = templateName.split("_");
if (split == null || split.length <= 1) {
return new DmHubResponse("999", "短信模板名称命名不规范,请使用:项目ID_模板名称格式");
}
String areaId = split[0];
// 模板参数占位符
String params = templateInfo.getParams();
JSONArray paramsArr = JSONArray.parseArray(params);
......@@ -437,7 +338,7 @@ public class MessageServiceImpl implements MessageService {
for (String newKeyStr : paramsList) {
String newKey = newKeyStr.substring(1);
if (s.contains(newKey)) {
list.add(retMap.get(newKeyStr).toString());
list.add(retMap.get(newKeyStr));
}
}
paramsMap.put(mobile, list);
......@@ -472,19 +373,17 @@ public class MessageServiceImpl implements MessageService {
sysBatchService.insert(info);
CLBizResponse response = new CLBizResponse();
if (paramsArr == null || paramsArr.isEmpty()) {
try {
for (String mobile : mobileList) {
try {
response = bgySmsService.sendSms(mobile, templateInfo.getUpContent());
response = bgySmsService.sendSms(mobile, templateInfo.getUpContent(), areaId);
} catch (Exception e) {
log.error("短信发送异常", e);
}
}
} catch (Exception e) {
log.error("调用碧桂园逻辑错误", e);
// TODO 根据DM hub需要的返回数据封装
return new DmHubResponse("999", "插件服务系统异常");
}
} else {
......@@ -496,7 +395,7 @@ public class MessageServiceImpl implements MessageService {
String mobile = entry.getKey().toString();
List<String> paramsList = entry.getValue();
String sendMsg = getMsg(templateInfo.getUpContent(), paramsList);
response = bgySmsService.sendSms(mobile, sendMsg);
response = bgySmsService.sendSms(mobile, sendMsg, areaId);
String code = response.getCode();
String msg = response.getMsg();
} catch (Exception e) {
......@@ -504,7 +403,6 @@ public class MessageServiceImpl implements MessageService {
}
}
// TODO 待优化
return new DmHubResponse(ResponseCode.SUCCESS);
} catch (Exception e) {
log.error("调用碧桂园逻辑错误:", e);
......
......@@ -40,6 +40,8 @@ system:
applicationKey: 4017078e9dfd593b2d9a0ede58eff589644fbe50
tokenUrl: https://api.convertlab.com/security/accesstoken
report: https://api.convertlab.com/v1/sms/report
appId: 1smsdemo
appSecret: ac031765c3a8c9acc4747808e4fe5918
logging:
level: debug
......
......@@ -41,6 +41,8 @@ system:
applicationKey: 4017078e9dfd593b2d9a0ede58eff589644fbe50
tokenUrl: https://api.convertlab.com/security/accesstoken
report: https://api.convertlab.com/v1/sms/report
appId: 1smsdemo
appSecret: ac031765c3a8c9acc4747808e4fe5918
logging:
level: error
......
......@@ -40,6 +40,8 @@ system:
applicationKey: 4017078e9dfd593b2d9a0ede58eff589644fbe50
tokenUrl: https://api.convertlab.com/security/accesstoken
report: https://api.convertlab.com/v1/sms/report
appId: 1smsdemo
appSecret: ac031765c3a8c9acc4747808e4fe5918
logging:
level: debug
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment