视频049~(template&phone校验)

master
Administrator 2 years ago
parent de62a46366
commit 8be98a8c69

@ -1,8 +1,12 @@
package com.mashibing.api.filter.impl;
import com.mashibing.api.filter.CheckFilter;
import com.mashibing.api.util.PhoneFormatCheckUtil;
import com.mashibing.common.enums.ExceptionEnums;
import com.mashibing.common.exception.ApiException;
import com.mashibing.common.model.StandardSubmit;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
/**
@ -17,5 +21,13 @@ public class MobileCheckFilter implements CheckFilter {
@Override
public void check(StandardSubmit submit) {
log.info("【接口模块-校验手机号】 校验ing…………");
String mobile = submit.getMobile();
if(!StringUtils.isEmpty(mobile) && PhoneFormatCheckUtil.isChinaPhone(mobile)){
// 如果校验进来,代表手机号么得问题
log.info("【接口模块-校验手机号】 手机号格式合法 mobile = {}",mobile);
return;
}
log.info("【接口模块-校验手机号】 手机号格式不正确 mobile = {}",mobile);
throw new ApiException(ExceptionEnums.ERROR_MOBILE);
}
}

@ -36,6 +36,11 @@ public class SignCheckFilter implements CheckFilter {
*/
private final String CLIENT_SIGN_INFO = "signInfo";
/**
* id
*/
private final String SIGN_ID = "id";
@Override
@ -63,6 +68,9 @@ public class SignCheckFilter implements CheckFilter {
//4. 判断~
for (Map map : set) {
if(sign.equals(map.get(CLIENT_SIGN_INFO))){
// 走到这,说明匹配上了具体的签名信息
submit.setSign(sign);
submit.setSignId(Long.parseLong(map.get(SIGN_ID) + ""));
log.info("【接口模块-校验签名】 找到匹配的签名 sign = {}",sign);
return;
}

@ -1,21 +1,79 @@
package com.mashibing.api.filter.impl;
import com.mashibing.api.client.BeaconCacheClient;
import com.mashibing.api.filter.CheckFilter;
import com.mashibing.common.constant.ApiConstant;
import com.mashibing.common.constant.CacheConstant;
import com.mashibing.common.enums.ExceptionEnums;
import com.mashibing.common.exception.ApiException;
import com.mashibing.common.model.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 zjw
* @description
* @description
*/
@Service(value = "template")
@Slf4j
public class TemplateCheckFilter implements CheckFilter {
@Autowired
private BeaconCacheClient cacheClient;
/**
*
*/
private final String TEMPLATE_TEXT = "templateText";
private final String TEMPLATE_PLACEHOLDER = "#";
@Override
public void check(StandardSubmit submit) {
log.info("【接口模块-校验模板】 校验ing…………");
// 1、从submit中获取到短信内容签名信息签名id
String text = submit.getText();
String sign = submit.getSign();
Long signId = submit.getSignId();
// 2、将短信内容中的签名直接去掉获取短信具体内容
text = text.replace(ApiConstant.SIGN_PREFIX + sign + ApiConstant.SIGN_SUFFIX, "");
// 3、从缓存中获取到签名id绑定的所有模板
Set<Map> templates = cacheClient.smember(CacheConstant.CLIENT_TEMPLATE + signId);
// 4、在tempaltes不为null时遍历签名绑定的所有模板信息
if(templates != null && templates.size() > 0) {
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);
}
}

@ -0,0 +1,28 @@
package com.mashibing.api.util;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
* @author zjw
* @description
*/
public class PhoneFormatCheckUtil {
/**
*
*/
private final static Pattern CHINA_PATTERN = Pattern.compile("^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$");
/**
*
* @param number
* @return
*/
public static boolean isChinaPhone(String number){
Matcher matcher = CHINA_PATTERN.matcher(number);
return matcher.matches();
}
}

@ -108,7 +108,13 @@ public class StandardSubmit {
* 0- 1- 2-
*/
private int state;
/**
* id
*/
private Long signId;
// 后续再做封装~~~~
}

Loading…
Cancel
Save