8、Redis_Jedis_实例(手机验证码)

8. Redis_Jedis_实例

8.1. 完成一个手机验证码功能

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

思路:
在这里插入图片描述

1. 生成6位密码

    //1. 生成6位密码
    public static String getCode(){
        Random random = new Random();
        String code = "";
        for (int i=0;i<6;i++){
            int rand = random.nextInt(10);
            code+=rand;
        }
        return code;
    }

2.每个手机只能发送三次,验证码放到redis中,设置过期时间

public static void toRedis(String phone){
        //链接redis
        Jedis jedis = new Jedis("192.168.195.129",6379);
        //拼接key
        //手机发送次数key
        String countKey = "Verifycode"+phone+":count";
        //验证码key
        String codeKey = "VerifyCode"+phone+":code";

        //每个手机只能发三次每天
        String count = jedis.get(countKey);
        if (count == null){
            //证明第一次发送
            //设置发送次数为1
            jedis.setex(countKey,24*60*60,"1");
        }else if (Integer.parseInt(count)<=2){
            //发送次数+1
            jedis.incr(countKey);
        }else {
            //大于2,不能发送
            System.out.println("今天的发送次数已经超出3次");
            jedis.close();
            //结束,不再发送验证码
            return;
        }

        //发送的验证码要放到redis
        String vode = getCode();
        jedis.setex(codeKey,120,vode);
        jedis.close();

    }

在这里插入图片描述

3.验证码校验功能

 public static  void getRedisCode(String phone,String code){
        //从redis中获取验证码
        Jedis jedis = new Jedis("192.168.195.129",6379);

        String codeKey = "VerifyCode"+phone+":code";
        String redisCode = jedis.get(codeKey);
        //做判断
        if (redisCode.equals(code)){
            System.out.println("成功");
        }else {
            System.out.println("失败");
        }

        jedis.close();

    }

获取时间差

 @Test
    public void times() throws ParseException {
        long now = System.currentTimeMillis();
        SimpleDateFormat sdfOne = new SimpleDateFormat("yyyy-MM-dd");
        long overTime = (now - (sdfOne.parse(sdfOne.format(now)).getTime()))/1000;
        //当前毫秒数
        System.out.println(now);
        //当前时间  距离当天凌晨  秒数 也就是今天过了多少秒
        System.out.println(overTime);
        //当前时间  距离当天晚上23:59:59  秒数 也就是今天还剩多少秒
        long TimeNext = 24*60*60 - overTime;
        System.out.println(TimeNext);
        //当天凌晨毫秒数
        System.out.println(sdfOne.parse(sdfOne.format(now)).getTime());
        //当天凌晨日期
        SimpleDateFormat sdfTwo = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.print(sdfTwo.format(sdfOne.parse(sdfOne.format(now)).getTime()));
    }

使用redis对验证码进行存储,设置过期时间
过期时间:24小时,可以自定义为每天0:00过期(时间差)

完整代码

public class PhoneCode {
    public static void main(String[] args) {
        //模拟验证码发送
        toRedis("1234567890");
//        getRedisCode("1234567890","754065");

    }

    //验证码校验功能
    public static  void getRedisCode(String phone,String code){
        //从redis中获取验证码
        Jedis jedis = new Jedis("192.168.195.129",6379);

        String codeKey = "VerifyCode"+phone+":code";
        String redisCode = jedis.get(codeKey);
        //做判断
        if (redisCode.equals(code)){
            System.out.println("成功");
        }else {
            System.out.println("失败");
        }

        jedis.close();

    }



    //每个手机只能发送三次,验证码放到redis中,设置过期时间
    public static void toRedis(String phone){
        //链接redis
        Jedis jedis = new Jedis("192.168.195.129",6379);
        //拼接key
        //手机发送次数key
        String countKey = "Verifycode"+phone+":count";
        //验证码key
        String codeKey = "VerifyCode"+phone+":code";

        //每个手机只能发三次每天
        String count = jedis.get(countKey);
        if (count == null){
            //证明第一次发送
            //设置发送次数为1
            jedis.setex(countKey,24*60*60,"1");
        }else if (Integer.parseInt(count)<=2){
            //发送次数+1
            jedis.incr(countKey);
        }else {
            //大于2,不能发送
            System.out.println("今天的发送次数已经超出3次");
            jedis.close();
            //结束,不再发送验证码
            return;
        }

        //发送的验证码要放到redis
        String vode = getCode();
        jedis.setex(codeKey,120,vode);
        jedis.close();

    }

    //1. 生成6位密码
    public static String getCode(){
        Random random = new Random();
        String code = "";
        for (int i=0;i<6;i++){
            int rand = random.nextInt(10);
            code+=rand;
        }
        return code;
    }


    @Test
    public void times() throws ParseException {
        long now = System.currentTimeMillis();
        SimpleDateFormat sdfOne = new SimpleDateFormat("yyyy-MM-dd");
        long overTime = (now - (sdfOne.parse(sdfOne.format(now)).getTime()))/1000;
        //当前毫秒数
        System.out.println(now);
        //当前时间  距离当天凌晨  秒数 也就是今天过了多少秒
        System.out.println(overTime);
        //当前时间  距离当天晚上23:59:59  秒数 也就是今天还剩多少秒
        long TimeNext = 24*60*60 - overTime;
        System.out.println(TimeNext);
        //当天凌晨毫秒数
        System.out.println(sdfOne.parse(sdfOne.format(now)).getTime());
        //当天凌晨日期
        SimpleDateFormat sdfTwo = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.print(sdfTwo.format(sdfOne.parse(sdfOne.format(now)).getTime()));
    }


}

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
概要介绍:本门课程属于“Java分布式中间件大汇聚实战”系列课程,主要介绍了企业级项目中真实的应用场景的实现及主流的Java核心技术栈(Redis、RabbitMQ、Spring AOP、Redisson、ZooKeeper…)的实战等等。除此之外,还介绍了如何基于Redis设计并实战一款点赞系统(点赞、取消点赞、排行榜、用户中心、文章点赞用户列表…)可以说技术干货甚多,不仅可以巩固企业级应用系统的开发实战能力,相信在面试、跳槽涨薪方面也能带来相应的帮助!课程内容:传说中的金三银四、面试跳槽涨薪季已经来临,Debug特地为大家准备了一系列跟面试、跳槽、巩固核心技术栈相关的课程,本门课程属于第一季,其中的内容包括企业级项目中真实的应用场景实战、面试相关的技术点分享、主流的Java技术栈(Undertow、Redis、RabbitMQ、Spring AOP、Redisson、ZooKeeper…)实战等等。除此之外,我们还基于Redis设计并实战了一款点赞系统,可以说技术干货甚多。在课程的最后,Debug给大家整理了一份最新的面向BAT大厂招聘 ~ 2020年程序猿最新的Java面试题(附带目录和答案),希望对各位小伙伴的成长有所帮助!值得一提的是,本季课程实战的应用场景包括“日志记录”、“邮件发送”、“通告消息通知”、“短信验证码失效验证”、“会员到期自动提醒/到期前N天自动提醒”以及“点赞系统”的设计与实战,其大纲如下所示:其中,涉及到的技术栈包括Spring Boot2.0、Mybatis、Undertow、Redis、RabbitMQ、Redisson、Spring AOP、 Java8…下面罗列出本门课程重点介绍的价格应用案例以及业务场景的实现流程图!(1)基于Spring的消息驱动模型实现日志的异步记录:(2)基于消息中间件RabbitMQ的消息队列实现日志的异步记录:(3)基于缓存中间件Redis的订阅发布机制实现商户公告消息通知:(4)基于Redis的Key失效与定时任务实现实现短信验证码的过期失效验证:其他核心、典型的应用案例和业务场景的实战可以详细参考“课程目录”!除此之外,我们还基于缓存中间件Redis设计并实战实现了点赞系统中的点赞功能模块,下面罗列出其中涉及到的相关功能模块的实战流程图:其课程收益如下所示:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值