当前位置:   article > 正文

使用redis实现手机短信验证码和lua完成重置功能

使用redis实现手机短信验证码和lua完成重置功能


前言

2024了,各种各样的门户网站和APP都需要登录,登录方式也各种各样,由于都要绑定用户手机号码,所以大部分都支持了手机验证码登录的方式,接下来我们使用redis来完成验证码的功能。


一、介绍

方法名描述
get获取验证码
match验证码配对
reset验证码重置
check手机号检测
sendVerificationCode发送验证码

二、代码

1.LoginController

@Controller
@RequestMapping("verificationCode")
public class LoginController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    private int maxRetryCount=3;
    private static final String COUNT="_COUNT";
    private static final String CODE="_CODE";
    private static final DefaultRedisScript<Boolean> RESET_SCRIPT;
    static {
        RESET_SCRIPT = new DefaultRedisScript<>();
        RESET_SCRIPT.setLocation(new ClassPathResource("redis/reset.lua"));
        RESET_SCRIPT.setResultType(Boolean.class);
    }
    @RequestMapping("get")
    @ResponseBody
    public boolean get(String phone){
        Optional.of(phone);
        if(check(phone)){
            return sendVerificationCode(phone);
        }
        return false;
    }
    @RequestMapping("match")
    @ResponseBody
    public boolean match(String phone, String code) {
        String redisCode = stringRedisTemplate.opsForValue().get(phone + CODE);
        System.out.println("用户["+phone+"],认证完成,跳转到首页!");
        return code.equals(redisCode);
    }
    @RequestMapping("reset")
    @ResponseBody
    public boolean reset(String phone) {
        boolean res=false;
        try {
            //用手机号拼接上字符串来作为key值,这是作为发送的次数
            String countKey = phone + COUNT;
            //用手机号拼接上字符串来作为key值,来表示你的验证码
            String codeKey = phone + CODE;
            if(stringRedisTemplate.execute(
                    RESET_SCRIPT,
                    Arrays.asList(countKey, codeKey))){
                res=true;
                System.out.println("重置已完成");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return res;
    }
    private boolean check(String phone) {
        String regex = "^1[0-9]{10}$"; // 定义手机号格式的正则表达式
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(phone);
        return matcher.matches();
    }
    private boolean sendVerificationCode(String phone){
        //用手机号拼接上字符串来作为key值,这是作为发送的次数
        String countKey = phone + COUNT;
        //用手机号拼接上字符串来作为key值,来表示你的验证码
        String codeKey = phone + CODE;
        try {
            //根据countKey来获取发送的次数
            String countStr = stringRedisTemplate.opsForValue().get(countKey);
            Integer count;
            try {
                //将String类型的次数来强转为Integer
                count = Integer.valueOf(countStr);
            }catch (NumberFormatException e){
                count = 0;
            }
            if(count > maxRetryCount){
                System.out.println("24小时内短信发送次数已达"+maxRetryCount+"次,不能继续发送!24小时后自动重置次数");
                //做判断来表示发送三次,再次发送失败
                return false;
            }
            //判断codeKey是否存在
            boolean isTtl = stringRedisTemplate.hasKey(codeKey);
            if(isTtl){
                //如果存在的获取还有多少秒过期
                long ttlCode = stringRedisTemplate.getExpire(codeKey);
                System.out.println("验证码在1分钟内保持有效,请于"+ttlCode+"秒之后再次发送");
                return false;
            }
            //获取6位数的随机数
            //这个地方如果换成手机短信的api接口的话就可以实现手机短信验证的功能
            String code = RandomUtil.randomNumbers(6);
            //保存在随机数中设置过期时间
            stringRedisTemplate.opsForValue().set(codeKey,code,1, TimeUnit.MINUTES);
            //设置过期时间为1天
            long timeOut = 24 * 60 * 60;
            //做一个判断如果不是第一次发送,就要根据countKey来获取他的剩余的过期时间
            if(count!=0){
                //如果这个地方是短信验证的功能的话就是可以换成根据countKey来获取它是否存在来判断是否验证码过期
                timeOut = stringRedisTemplate.getExpire(countKey);
            }
            stringRedisTemplate.opsForValue().set(countKey,String.valueOf(count+1),timeOut,TimeUnit.SECONDS);
            System.out.println("短信验证码发送完毕!请注意查收!");
            return true;
        }catch (Exception e){
            stringRedisTemplate.delete(codeKey);
            stringRedisTemplate.delete(countKey);
            return false;
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106

2.reset.lua

-- reset.lua
local count = redis.call('get',KEYS[1])
if (count ~= nil) then
    redis.call('del',KEYS[1])
end
local code = redis.call('get',KEYS[2])
if (code ~= nil) then
    redis.call('del',KEYS[2])
end
return true
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

总结

回到顶部
官方文档对脚本的描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/133927
推荐阅读
相关标签
  

闽ICP备14008679号