短信模板校验实现

main
heqijun 4 months ago
parent 54949890b6
commit 58c5cd258c

@ -53,14 +53,17 @@ public class SignSendCheckServiceImpl implements SendCheckService {
throw new ApiException(ExceptionEnums.ERROR_SIGN);
}
//取出签名
//取出签名做匹配
for (Map clientSign : clientSigns) {
String signInCache = (String) clientSign.get("signInfo");
if (signInCache.equals(signInText)) {
log.info("【接口模块】签名校验通过text = {}", text);
standardSubmit.setSign(signInText);
standardSubmit.setSignId(clientSign.get("id").toString());
return;
}
}
//没有匹配成功
log.info("【接口模块-校验签名】短信无可用签名 text = {}", text);
throw new ApiException(ExceptionEnums.ERROR_SIGN);
}

@ -1,11 +1,20 @@
package com.mashibing.api.service.sendCheck.impl;
import com.mashibing.api.feignClient.CacheClient;
import com.mashibing.api.pojo.SingleSendRequest;
import com.mashibing.api.service.sendCheck.SendCheckService;
import com.mashibing.common.constant.CacheConstant;
import com.mashibing.common.constant.SMSConstant;
import com.mashibing.common.enums.ExceptionEnums;
import com.mashibing.common.exception.ApiException;
import com.mashibing.common.pojo.StandardSubmit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.Set;
/**
* @author heqijun
* @ClassName: TemplateSendCheckServiceImpl
@ -16,8 +25,55 @@ import org.springframework.stereotype.Service;
@Slf4j
@Service("TemplateSendCheck")
public class TemplateSendCheckServiceImpl implements SendCheckService {
@Autowired
CacheClient cacheClient;
private final String TEMPLATE_TEXT = "templateText";
private final String TEMPLATE_PLACEHOLDER = "#";
@Override
public void check(StandardSubmit standardSubmit) {
log.info("Check TemplateSendCheckServiceImpl");
log.info("【接口模块-校验模板】 校验ing…………");
// 1、从submit中获取到短信内容签名信息签名id
String text = standardSubmit.getText();
String sign = standardSubmit.getSign();
String signId = standardSubmit.getSignId();
// 2、将短信内容中的签名直接去掉获取短信具体内容
text = text.replace(SMSConstant.SIGN_PREFIX + sign + SMSConstant.SIGN_SUFFIX, "");
// 3、从缓存中获取到签名id绑定的所有模板
Set<Map> templates = cacheClient.smember(CacheConstant.CLIENT_TEMPLATE + signId);
// 4、在tempaltes不为null时遍历签名绑定的所有模板信息
if (templates != null && !templates.isEmpty()) {
for (Map template : templates) {
// 4.1 将模板内容和短信具体内容做匹配-true-匹配成功
String templateText = (String) template.get(TEMPLATE_TEXT);
if (text.equals(templateText)) {
// 短信具体内容和模板是匹配的。
log.info("【接口模块-校验模板】 校验模板通过 templateText = {}", templateText);
return;
}
// 4.2 判断模板中是否只包含一个变量,如果是,直接让具体短信内容匹配前缀和后缀
// 例子您的验证码是123434。如非本人操作请忽略本短信
// 例子:您的验证码是#code#。如非本人操作,请忽略本短信
if (templateText != null && templateText.contains(TEMPLATE_PLACEHOLDER) && templateText.length() - templateText.replaceAll(TEMPLATE_PLACEHOLDER, "").length() == 2) {
// 可以确认模板不为空,并且包含#符号,而且#符号有2个代表是一个占位符变量
// 获取模板撇去占位符之后的前缀和后缀
String templateTextPrefix = templateText.substring(0, templateText.indexOf(TEMPLATE_PLACEHOLDER));
String templateTextSuffix = templateText.substring(templateText.lastIndexOf(TEMPLATE_PLACEHOLDER) + 1);
// 判断短信的具体内容是否匹配前缀和后缀
if (text.startsWith(templateTextPrefix) && text.endsWith(templateTextSuffix)) {
// 当前的短信内容匹配短信模板
log.info("【接口模块-校验模板】 校验模板通过 templateText = {}", templateText);
return;
}
}
}
}
// 5、 模板校验失败
log.info("【接口模块-校验模板】 无可用模板 text = {}", text);
throw new ApiException(ExceptionEnums.ERROR_TEMPLATE);
}
}

@ -37,6 +37,9 @@ public class StandardSubmit {
@Description("短信内容的签名")
private String sign;
@Description("签名id")
private String signId;
@Description("短信内容")
private String text;

Loading…
Cancel
Save