Java连接Redis模拟手机号短信验证

需求分析: 输入手机号,点击发送后返回6位数字码,2分钟有效。用户输入验证码,点击验证,返回成功或失败,每个手机号每天只能输入3次,0点刷新次数。

package com.wenbin.jedis;

import redis.clients.jedis.Jedis;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Random;
import java.util.Scanner;

/**
 * 1、输入手机号,点击发送后随机生成6位数字码,2分钟有效
 * 2、输入验证码,点击验证,返回成功或失败
 * 3、每个手机号每天只能输入3次
 *
 * @author wenbin
 */
public class PhoneCode {
    //随机数
    private static final Random random = new Random();

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入您的手机号");
        String photo = input.next();
        String code = sendCode(photo);
        //次数用完 退出 不再执行验证操作
        if (code.length() == 0) {
            System.out.println("您今天的验证码发送次数已经用完");
            return;
        }
        System.out.println("您收到的验证码是" + code);
        System.out.println("请输入您的验证码");
        String inCode = input.next();
        boolean flag = checkCode(photo, inCode);
        System.out.println((flag) ? "验证码正确" : "验证码错误");
    }

    /**
     * 检查输入的验证码是否与发送的一致
     *
     * @param photo  手机号
     * @param inCode 输入的验证码
     * @return true正确 false错误
     */
    private static boolean checkCode(String photo, String inCode) {
        //获取redis链接
        Jedis jedis = getJedis();
        //该手机号验证码key
        String codeKey = photo + "code";
        //拿出redis中存入的验证码
        String codeValue = jedis.get(codeKey);
        //关闭redis连接
        closeJedis(jedis);
        //返回两个是否相等
        return inCode.equals(codeValue);
    }

    /**
     * 发送验证码 一天最多三次
     *
     * @param photo 手机号
     * @return 返回验证码
     */
    private static String sendCode(String photo) {
        //获取redis链接
        Jedis jedis = getJedis();
        //该手机号已发送次数key
        String countKey = photo + "count";
        //该手机号已发送次数value,如果没有返回null把他改为0 有则返回已发送次数
        String countValue = (jedis.get(countKey) == null) ? "0" : jedis.get(countKey);
        if (Integer.parseInt(countValue) >= 3) {
            return "";
        }
        //下面代码表示正常发送验证码
        //计算当前时间还有多少秒到24点 用作每天验证次数刷新 存储redis中设置key有效时间
        Calendar nowDate = Calendar.getInstance();
        Calendar nextDayDate = new GregorianCalendar(nowDate.get(Calendar.YEAR), nowDate.get(Calendar.MONTH), nowDate.get(Calendar.DATE) + 1);
        long s = (nextDayDate.getTimeInMillis() - nowDate.getTimeInMillis()) / 1000;
        //在redis中设置到24.过期的key 值为每日验证码发送次数
        jedis.setex(countKey, (int) s, String.valueOf(Integer.parseInt(countValue) + 1));
        //该手机号验证码key
        String codeKey = photo + "code";
        //该手机号验证码value,六位数随机生成
        String codeValue = getCode();
        //在redis中设置120s过期的key 值为验证码
        jedis.setex(codeKey, 2 * 60, codeValue);
        //关闭redis连接
        closeJedis(jedis);
        return codeValue;
    }

    /**
     * 生成六位数字验证码
     *
     * @return 六位数字验证码
     */
    private static String getCode() {
        //生成1000000-99999左闭右闭的随机数
        int code = random.nextInt(1000000) % (1000000 - 99999 + 1) + 99999;
        return String.valueOf(code);
    }


    /**
     * 获取redis连接
     *
     * @return redis连接
     */
    public static Jedis getJedis() {
        return new Jedis("192.168.188.100", 6379);
    }

    /**
     * 关闭redis连接
     *
     * @param jedis 要关闭的redis连接
     */
    public static void closeJedis(Jedis jedis) {
        jedis.close();
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值