当前位置:   article > 正文

Redis存储验证码

redis存储验证码

import redis.clients.jedis.Jedis;

import java.util.Random;

public class RedisConfig {
    static String code = null;
    public static void main(String[] args) {
        //1,先判断该手机验证次数几次了,有没有超时间
        //2,然后生成随机码
        //3,再判断填写的随机码是否和生成的随机码相同。
        verifyPhone("12345678912");
    }
    public static void verifyPhone(String phone){
        Jedis jedis = new Jedis("192.168.110.110", 6379);
        String keyPhone = "verify" + phone + "count";
        String codePhone = phone + "code";

        String s = jedis.get(keyPhone);
        //第一次,没有key的记录,所以需要判断null
        if(s == null){
            jedis.setex(keyPhone, 24*60*60, "1");
        }else if(Integer.valueOf(s) < 3){
            //第二,三次,有记录了
            jedis.incr(keyPhone);
        }else {
            System.out.println("今天已经超过三次");
            jedis.close();
            return;
        }
        //如果还在三次之内可以,可以执行这个获取值的操作
        code = getRandomCode();
        jedis.setex(codePhone, 60, code);
        jedis.close();
    }


    public static String getRandomCode(){
        Random random = new Random();
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < 6; ++i){
            int tmp = random.nextInt(10);
            builder.append(tmp);
        }
        return builder.toString();
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/511129
推荐阅读
相关标签
  

闽ICP备14008679号