回顾Redis实现验证码功能

回顾Redis实现验证码功能,第1张

回顾Redis实现验证码功能 一 60s防刷功能
//1、接口防刷
String redisCode = stringRedisTemplate.opsForValue().get(SmsConstant.SMS_CODE_CACHE_PREFIX + phone);

Boolean existKey = false;

if (!StringUtils.isEmpty(redisCode)) {
    //活动存入redis的时间,用当前时间减去存入redis的时间,判断用户手机号是否在60s内发送验证码
    long currentTime = Long.parseLong(redisCode.split("_")[1]);
    if (System.currentTimeMillis() - currentTime < SmsConstant.SMS_LIMIT_TIME) {
        //60s内不能再发
        throw new SmsException("60s内只能获取一次验证码!");
    }
}
//保存验证码
String redisStorage = code + "_" + System.currentTimeMillis();
//存入redis,防止同一个手机号在60秒内再次发送验证码,验证码有效期为60秒
stringRedisTemplate.opsForValue().set(SmsConstant.SMS_CODE_CACHE_PREFIX + phone,
        redisStorage, 1, TimeUnit.MINUTES);
二 验证码当前限制发送次数
//判断验证码发送次数,验证码校验
String smsCountKey = CommonRedisConstants.SMS_COUNT + phone;
if (redisUtil.hasKey(smsCountKey)) {
    existKey = true;
    Long count = (Long) smsCodeRedisUtil.get(smsCountKey);
    if (count > SmsConstant.SMS_COUNT_LIMIT) {
        // 今日获取已上限,请明日再来
        throw new SmsException("验证码获取今天已达上限!");
    }
}

        SmsCodeRedisUtil

//value+delta
public long incr(String key, long delta) {
    if (delta < 0L) {
        throw new RuntimeException("递增因子必须大于0");
    } else {
        return this.redisTemplate.opsForValue().increment(key, delta);
    }
}
三 生成验证码
public static String getRandomCode() {
    return (int)((Math.random() * 9.0D + 1.0D) * 1000.0D) + "";
}
四 获取当前时间至第二天凌晨的时间(单位秒)
private Long getNowToTomorrowSeconds() {
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DAY_OF_YEAR, 1);
    // 改成这样就好了
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return (cal.getTimeInMillis() - System.currentTimeMillis()) / 1000;
}
//当前时间到第二天凌晨是时间长度(秒)
long duration = getNowToTomorrowSeconds();
//验证码次数达到最大值有效时间为当前时间到第二天凌晨
stringRedisTemplate.opsForValue().set(smsCountKey, "0", duration, TimeUnit.SECONDS);
五 校验验证码
public Map checkSmsCode(String phone, String code) {

    String key = SmsConstant.SMS_CODE_CACHE_PREFIX + phone;

    Map smsMap = new HashMap<>();
    smsMap.put("IsExpire","0");
    smsMap.put("IsErr","0");
    Boolean isKey = stringRedisTemplate.hasKey(key);
    if (!isKey) {
        //过期
        smsMap.put("IsExpire","1");
    }
    
    
    String oldCode = stringRedisTemplate.opsForValue().get(key);

    if(StringUtils.isNotEmpty(oldCode)){
        //验证码错误
        oldCode = oldCode.split("_")[0];
        if (!code.equals(oldCode)) {
            smsMap.put("IsErr","1");
        }
    }

    redisUtil.del(key);
    return smsMap;
}

        参考: java如何获取当前时间到第二天凌晨的秒数_酸奶灬的博客-CSDN博客_java获取第二天凌晨https://blog.csdn.net/qq_34560242/article/details/79475190

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5712344.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存