首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >redis验证码模板

redis验证码模板

作者头像
一个风轻云淡
发布2022-11-13 11:11:52
发布2022-11-13 11:11:52
4070
举报
文章被收录于专栏:java学习javajava学习java
代码语言:javascript
复制
public class redisPhone {

    public static void main(String[] args) {
        verifyCode("1231","123");
        getRedisCode("1231","123");
    }
    //1. 生成6位验证码
    public static String getCode()
    {
        Random random=new Random();
        String code="";
        for(int i=0;i<6;i++)
        {
            code=random.nextInt(10)+code;
        }
        return code;
    }
    //2.每个手机只能每天发送三次验证码
    public static void verifyCode(String phone,String code)
    {
        //1.连接redis
        Jedis jedis = new Jedis("192.168.231.131", 6379);
        //2.发生次数key
        String countKey="VerifyCode"+phone+":count";
        String CodeKey="VerifyCode"+phone+":Code";
        String count=jedis.get(countKey);
        if(count==null)
        {
            //没有发生次数
            //设置发生第一次
            jedis.setex(countKey, 24*24*60,"1");



        }
        else if(Integer.parseInt(count)>2)
        {
            jedis.incr(countKey);
        }
        else
        {
            System.out.println("已经发生了三次,无法发送");
        }
        String vcode=getCode();
        jedis.setex(CodeKey,2*60,vcode);
        jedis.close();
    }
    //3.验证
    public static  void getRedisCode(String phone,String code)
    {
        Jedis jedis = new Jedis("192.168.231.131", 6379);
        String CodeKey="VerifyCode"+phone+":Code";
        String s = jedis.get(CodeKey);
        if(s.equals(code))
        {
            System.out.println("成功");

        }
        else {
            System.out.println("失败");
        }
    }



}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-07-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档