发送验证码
  EzdpI95hZe8i 2023年11月22日 20 0
/**
 * 发送验证码
 * @param vo
 * @return
 * @throws Exception
 */
@RequestMapping(value = "/verificationCode", method = RequestMethod.POST)
public Result sendLoginPhoneCode(HttpServletRequest request,@RequestBody OpenRegisterVo vo) {
    if (vo.getUser_phone() == null) {
        return ResultGenerator.getFailResult("手机号不能为'null'!");
    }
    UserInfo userInfo = userInfoService.selectUserInfoByPhone(vo.getUser_phone());
    if (userInfo == null) {
        return ResultGenerator.getFailResult("请注册账号");
    }
    return verificationCode(request, vo);
}

@RequestMapping(value = "/verificationCode11", method = RequestMethod.POST)
public Result verificationCode(HttpServletRequest request,@RequestBody OpenRegisterVo vo){
    if (ObjectUtils.isEmpty(vo) || StringUtils.isNull(vo.getUser_phone())){
        return ResultGenerator.getFailResult("手机号不能为'null'!");
    }
    //取到登录用户的IP
    String ipAddr = ServletUtil.getClientIP(request);
    //手机号黑名单
    String blackListPhone = BLACKLISTPHONECODE + vo.getUser_phone();
    //ip黑名单
    String blackListIp = BLACKLISTIPCODE + ipAddr;
    //验证码
    String key = BcsConstants.PHONE_MESSAGE_PREFIX + vo.getUser_phone();
    //手机号累加值
    String growKey = GROW + vo.getUser_phone();
    //初始时间
    String initTimeKey = INITIALCODE + vo.getUser_phone();
    //ip累加值
    String addrIp = ADDRIP + ipAddr;
    //根据key从redis中查询黑名单
    String blackPhonr = redisdbUtil.getVStr(blackListPhone, 0);
    String blackIp = redisdbUtil.getVStr(blackListIp, 0);
    if (blackIp != null && blackIp.equals("1")) {
        return ResultGenerator.getFailResult("获取验证码过于频繁,IP已锁定,请30分钟后重新获取验证码");
    }
    if (blackPhonr != null && blackPhonr.equals("1")) {
        return ResultGenerator.getFailResult("获取验证码过于频繁,账户已锁定,请30分钟后重新获取验证码");
    }
    // 获取随机验证码
    vo.setAuth_code(SecurityUtil.getCode());
    vo.setAppkey(appkey);
    vo.setSecret(secret);
    // 调用发短信的方法
    SecurityUtil.sendCode(vo);
    String strPhone = redisdbUtil.getIncr(growKey, 0);
    Integer growPhone = (strPhone == null) ? 0 : Integer.valueOf(strPhone);
    if (growPhone < 5) {
        //每次调用发送短信方法后不论是否发送成功,都进行累加计数操作
        redisdbUtil.setIncr(growKey, 1, 60 * 10);
    }
    String strAddrIp = redisdbUtil.getIncr(addrIp, 0);
    Integer rAddrIpInt = (strAddrIp == null) ? 0 : Integer.valueOf(strAddrIp);
    if (rAddrIpInt < 10) {
        //统计ip的访问次数
        redisdbUtil.setIncr(addrIp, 1, 60 * 10);
    }
    // 1.取到当前时间戳,取到5分钟后的时间戳
    Map<String, String> initMap = redisdbUtil.hmGetAll(initTimeKey, 0);
    Long initialTime = initMap.get("initialTime") == null ? 0 : Long.valueOf(initMap.get("initialTime"));
    long nowTime = System.currentTimeMillis();
    long afterTime = initialTime + 300000;

    //2.如果在5分钟范围内,点击获取验证码超过5次,将手机号加入redis黑名单
    if (nowTime > initialTime && nowTime < afterTime) {
        if (growPhone >= 4) {
            // 设置过期时间 12小时
            redisdbUtil.set(blackListPhone, "1", 60 * 30);
            redisdbUtil.clearData(growKey);
        }
        if (rAddrIpInt >= 9) {
            redisdbUtil.set(blackListIp, "1", 60 * 30);
            redisdbUtil.clearData(addrIp);
        }
    }
    if (growPhone == 0 || nowTime > afterTime) {
        //第一次调用后将初始时间存储到redis中
        Map<String, String> mapVal = new HashMap<>();
        mapVal.put(INITIALTIME, System.currentTimeMillis() + "");
        redisdbUtil.hmsetData(initTimeKey, mapVal, 0, 60 * 10);
    }
    // 将验证码存入redis
    Map<String, String> map = new HashMap<>();
    map.put(BcsConstants.PHONE_MESSAGE_FIELD_CODE, vo.getAuth_code());
    // 设置过期时间 默认5分钟
    redisdbUtil.hmsetData(key, map, 0, 60 * 5);
    LOGGER.info("key[" + key + "]:save redis success!");
    return ResultGenerator.getSuccessResult();
}
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月22日 0

暂无评论

推荐阅读
EzdpI95hZe8i
作者其他文章 更多

2023-11-22