Redis模拟发送手机验证码

使用redis模拟验证码发送

1、需求

1、输入手机号,点击发送后随机生成6位数字码,2分钟有效
2、输入验证码,点击验证,返回成功或失败
3、每个手机号每天只能输入3次

2、导入依赖

<!-- jedis -->
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>3.7.0</version>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13</version>
</dependency>

总体分为三步

// 1.生成6位数字验证码
// 2.每个手机号每天只能发送三次验证码,验证码放到redis中,设置过期时间为2min
// 3、校验验证码

3、生成6位数字验证码

// 1.生成6位数字验证码
public static StringBuffer getCode() {
    StringBuffer code = new StringBuffer();
    Random random = new Random();
    for (int i=0; i<6;i++) {
        int rand = random.nextInt(10);
        code.append(rand);
    }

    return code;
}

4、设置验证码(难点)

怎么来实现验证码每天只能输入三次呢?

  1. 创建一个变量 countKey ,用来统计发送验证码的次数
  2. 计算当天的时间,距离第二天凌晨12点的秒数,设置为countKey的过期时间

计算时间差

// 获取当前时间到第二天凌晨12点的 秒 数,用来设置验证码次数的刷新时间
public Long getRefreshTime() {
    Calendar cal = Calendar.getInstance();
    // +1 获取第二天的时间
    cal.add(Calendar.DAY_OF_YEAR, 1);
    // 设置时间为 0 时 0 分 0 秒 0 毫秒
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.MILLISECOND, 0);

    // cal.getTimeInMillis(): 1970 年 1 月 1 日的 00:00:00.000到当前第二天凌晨12点的毫秒数
    // System.currentTimeMillis():1970 年 1 月 1 日的 00:00:00.000到系统当前时间的毫秒数
    return (cal.getTimeInMillis() - System.currentTimeMillis()) / 1000;

}

功能实现

//2.每个手机号每天只能发送三次验证码,验证码放到redis中,设置过期时间为2min
public static void veriftCode(String phone) {
    PhoneCode phoneCode = new PhoneCode();
    //1.连接redis
    Jedis jedis = new Jedis("192.168.154.65", 6379);
    jedis.auth("123456");

    //2.拼接key
    //手机发送次数key
    String countKey = "VerifyCode" + phone + ":count";
    //验证码key
    String codeKey = "VerifyCode" + phone + ":code";

    // 每个手机每天只能发送三次
    String count = jedis.get(countKey);
    if (count == null || count == "") {
        Long refreshTime = phoneCode.getRefreshTime();
        // 该手机号当天没有申请发送验证码,系统第一次发送验证码
        jedis.setex(countKey, refreshTime, "1");
        System.out.println("第" + jedis.get(countKey) + "次" + "验证码已发送,请耐心等待");
    } else if (Integer.parseInt(count) <= 2) {
        jedis.incr(countKey);
        System.out.println("第" + jedis.get(countKey) + "次" + "验证码已发送,请耐心等待");
    } else if (Integer.parseInt(count) > 2) {
        System.out.println("今日已经发送超过三次,请明日再试");
        jedis.close();
    }

    // 获取随机生成的验证码
    String code = getCode().toString();
    // 设置验证码有效时间
    Long expireTime = (long) 2 * 60;
    jedis.setex(codeKey, expireTime, code);
    jedis.close();

}

5、校验验证码

//3、校验验证码
public static void getRedisCode(String phone, String code) {

    Jedis jedis = new Jedis("192.168.154.65", 6379);
    jedis.auth("123456");

    String codeKey = "VerifyCode" + phone + ":code";
    String redisCode = jedis.get(codeKey);
    if(code.equals(redisCode)){
        System.out.println("验证码输入正确");
    }else{
        System.out.println("验证码输入错误");
    }
    jedis.close();

}

6、测试

public static void main(String[] args) {
    // 1、模拟手机号
     veriftCode("13923459867");

    // 2、去redis中,查看验证码复制过来
    
    // 3、模拟验证码校验
    getRedisCode("13923459867", "784449");

}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值