解决H5发送短信被脚本恶意盗刷(java)

之前在写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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值