Redis实现简单手机验证码

手机验证码

1 需求
  • 六位随机数字,两分钟内有效
  • 输入验证码,进行验证
  • 一天只能请求三条验证码
2 思路
  • 生成六位随机数字 通过random实现

  • 验证码两分钟内有效 验证码放到redis中,expire

  • 比较是否一致 拿到redis中进行比较

  • 一天只能发送三条 incr设置大于2就不能发送了

3 实现
3.1 依赖
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>3.7.0</version>
</dependency>
3.2 代码
package com.xzzlx;

import redis.clients.jedis.Jedis;

import java.util.Random;

public class PhoneCode {
    public static void main(String[] args) {
        verifyCode("17888888888");
//        getRedisCode("17888888888", "290782");
    }

    /**
     * 生成6位随机数字验证码
     * @return 随机数字验证码
     */
    public static StringBuffer getCode(){
        StringBuffer code = new StringBuffer();

        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            int num = random.nextInt(10);
            code.append(num);
        }

        return code;
    }

    /**
     * 验证今天是否已经发送3次了
     * 验证码放到redis中,设置过期时间
     * @param phone 手机号
     */
    public static void verifyCode(String phone){
        Jedis jedis = new Jedis("192.168.17.135", 6379);

        // 手机发送次数
        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 if (Integer.parseInt(count) > 2){
            // 今天已经发送3次了,不能再发送了
            System.out.println("今天已经发送三次了");
            jedis.close();
        }

        StringBuffer vcode = getCode();
        jedis.setex(codeKey, 120, String.valueOf(vcode));
        jedis.close();
    }

    /**
     * 验证码得校验
     * @param phone
     * @param code
     */
    public static void getRedisCode(String phone, String code){
        Jedis jedis = new Jedis("192.168.17.135", 6379);

        // 手机验证码
        String codekey = "VerifyCode" + phone + ":code";
        String redisCode = jedis.get(codekey);
        System.out.println("验证码是:" + redisCode);
        System.out.println("输入的是:" + code);
        System.out.print("验证");

        if (redisCode.equals(code)){
            System.out.print("成功");
        }else {
            System.out.print("失败");
        }
        jedis.close();
    }

}
3.3 测试
3.3.1

启动项目,先注释掉第十行,然后运行

3.3.2

然后去redis中查看

keys *

getVerifyCode1788888888:count

getVerifyCode1788888888:code

3.3.3

注释掉第九行,将第十行打开,然后输入手机号和getVerifyCode1788888888:code得到的验证码即可
在这里插入图片描述

该Demo是按照尚硅谷的老师视频打出来的,视频链接是https://www.bilibili.com/video/BV1Rv41177Af有问题可以call我

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来回答你的问题。首先,我们需要在SpringBoot项目中引入Redis的依赖,可以在pom.xml文件中添加如下代码: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 接下来,我们可以在项目中编写一个验证码的生成工具类,例如: ```java @Component public class VerificationCodeGenerator { private static final String CODES = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; private static final int LENGTH = 6; public String generate() { StringBuilder sb = new StringBuilder(); Random random = new Random(); for (int i = 0; i < LENGTH; i++) { sb.append(CODES.charAt(random.nextInt(CODES.length()))); } return sb.toString(); } } ``` 该工具类可以生成一个包含数字和字母的6位验证码。 然后,我们可以在SpringBoot的Controller中编写登录的逻辑,例如: ```java @RestController public class LoginController { @Autowired private StringRedisTemplate redisTemplate; @Autowired private VerificationCodeGenerator verificationCodeGenerator; @PostMapping("/login") public String login(@RequestParam String username, @RequestParam String password, @RequestParam String code) { String key = "verification_code:" + username; String expectedCode = redisTemplate.opsForValue().get(key); if (!code.equals(expectedCode)) { return "验证码错误"; } // 验证码正确,继续登录逻辑 // ... return "登录成功"; } @GetMapping("/verificationCode") public String getVerificationCode(@RequestParam String username) { String code = verificationCodeGenerator.generate(); String key = "verification_code:" + username; redisTemplate.opsForValue().set(key, code, 5, TimeUnit.MINUTES); return code; } } ``` 在这个例子中,我们通过一个GET请求获取验证码,将其存入Redis中,并设置过期时间为5分钟。然后,通过一个POST请求进行登录时,我们从Redis中获取到该用户对应的验证码,与用户输入的验证码进行比较,如果一致,则登录成功,否则返回错误信息。 以上就是整合Redis实现使用随机验证码登录的示例,希望对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值