回顾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) + "";
}

四 获取当前时间至第二天凌晨的时间(单位秒)

/**
 * 当前时间到第二天凌晨是时间长度(秒)
 *
 * @return
 */
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<String,String> checkSmsCode(String phone, String code) {

    String key = SmsConstant.SMS_CODE_CACHE_PREFIX + phone;

    Map<String,String> 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值