之前在写h5的发送短信业务的时候,发现有人通过脚本生成手机号,然后不断的调用发送短信的业务。
解决方案有两种
一,在发送短信的时候,需要获取对方的ip,然后通过ip去进行限制,每一个ip每天只能发送多少条数据,这种方案能够解决一些问题。
二,使用第三方插件
这里我们使用的顶像的插件(自己可以注册一个)
顶象:https://console.dingxiang-inc.com/
引入jar包
<!-- 顶象行为验证 -->
<dependency>
<groupId>com.dingxiang-inc</groupId>
<artifactId>ctu-client-sdk</artifactId>
<version>2.1</version>
</dependency>
后端业务代码
@Value("${app.dingxaing.appId}")
private String appId;
@Value("${app.dingxaing.appSecret}")
private String appSecret;
/**
* @Description: H5短信发送
* @param: userId
* @return:
*/
@GetMapping("/sendCode")
@ApiOperation(value = "H5发送验证码,需要顶象的行为验证 v2")
public boolean sendCode(String userPhone, Integer type, String token, HttpServletRequest request) throws Exception {
String ip = IpUtils.getIpAddr(request);
if (VerifyUtils.dingxiangVerify(token, ip, appId, appSecret) && VerifyUtils.phoneVerify(userPhone)) {
return userService.sendCode(userPhone, type);
} else {
throw new CloudException(ExceptionConstant.USER_NOT_REGIST);
}
}
顶象校验工具类
public class VerifyUtils {
/**
* 顶象行为认证 每天1万次
* @param token
* @return
*/
public static Boolean dingxiangVerify(String token, String ip, String appId, String appSecret) throws Exception {
CaptchaClient captchaClient = new CaptchaClient(appId, appSecret);
CaptchaResponse response = captchaClient.verifyToken(token, ip);
return response.getResult();
}
public static Boolean phoneVerify(String phone){
String regExp = "^1(3|4|5|6|7|8|9)\\d{9}$";
Pattern p = Pattern.compile(regExp);
Matcher m = p.matcher(phone);
return m.matches();
}
}
这个是java的后端代码,至于前段的代码官方有详细的讲解,同时后端也有详细讲解
官方地址:https://www.dingxiang-inc.com/docs/detail/captcha#doc-h3-30