Java用redis 实现发送手机验证码的功能

package com.example.demo.Entity;
import redis.clients.jedis.Jedis;
import java.util.Random;
public class ValidationTest {
    public static void main(String[] args) {
//        getValidation("11012012012");
//        checkValidation("001478","11012012012");
    }
    static void getValidation(String tel) {
        //主机、端口
        Jedis jedis = new Jedis("localhost", 6379);
        //密码
//        jedis.auth("root");
        try {
            //获取电话号码
            String phoneNo = tel;
            //本人用1库进行测试
            jedis.select(1);
            String countKey = phoneNo + ":count";
            String codeKey = phoneNo + ":code";
            //获取指定的电话号码发送的验证码次数
            String cnt = jedis.get(countKey);
            //对次数进行判断
            if (cnt == null) {
                //没有发送过验证码
                jedis.setex(countKey, 60 * 60 * 24, "1");
                //发送验证码,假设生成的验证码
                StringBuffer code = new StringBuffer();
                for (int i = 0; i < 6; i++) {
                    code.append(new Random().nextInt(10));
                }
                System.out.println("code:" + code);
                //缓存中添加验证码
                jedis.setex(codeKey, 60 * 2, code.toString());
            } else {
                if (Integer.parseInt(cnt) < 3) {
                    //发送验证码,假设生成的验证码
                    StringBuffer code = new StringBuffer();
                    for (int i = 0; i < 6; i++) {
                        code.append(new Random().nextInt(10));
                    }
                    System.out.println("code:" + code);
                    //缓存中添加验证码
                    jedis.setex(codeKey, 60 * 2, code.toString());
                    //递增手机发送数量
                    jedis.incr(countKey);
                } else {
                    //返回超出3次,禁止发送
                    System.out.println("超出3次,禁止发送");
                }
            }
        } catch (Exception e) {
            //这边其实是需要回滚下redis
            e.printStackTrace();
        } finally {
            //关闭redis
            if (jedis != null) {
                jedis.close();
            }
        }
    }
    static void checkValidation(String code, String tel) {
        Jedis jedis = null;
        try {
            jedis = new Jedis("localhost", 6379);
            //密码
//            jedis.auth("root");
            jedis.select(1);
            String codeKey = tel + ":code";
            String validation = jedis.get(codeKey);
            if (validation == null) {
                System.out.println("验证码未发送或者失效");
            } else {
                if (validation.equals(code)) {
                    System.out.println("验证成功");
                } else {
                    System.out.println("验证失败");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }
}

在这里插入图片描述

在这里插入图片描述
一定要启动cmd命令的redis

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值