ホームページ  >  記事  >  データベース  >  Redis はどのように検証コードの送信を実装し、毎日の送信数を制限しますか?

Redis はどのように検証コードの送信を実装し、毎日の送信数を制限しますか?

WBOY
WBOY転載
2023-05-27 12:55:111808ブラウズ

1. 機能

  • 携帯電話番号を入力し、[送信] をクリックすると、2 分間有効な 6 桁のコードがランダムに生成されます。

    確認コードを入力し、確認をクリックし、成功または失敗を返します
  • 各携帯電話番号は 1 日に 3 回のみ入力できます
  • # #2. 分析

各携帯電話に入力できるのは 1 日に 3 回だけです。各増加分が送信されると、値は 1 になります。値が 3 の場合は、入力できないことを示すメッセージが表示されます。有効期限は一日の終わりです。

  • 6 桁の確認コードをランダムに生成します: RandomUtil (hutool)

  • 検証コードは 2 分間有効です: Redis に配置し、有効期限を 2 分間に設定します

  • 検証コードが一貫しているかどうかを判断します: Redis から検証コードを取得し、それを入力した確認コード

  • 3.実装

    package cn.ken.blog.controller.common;
    
    import cn.hutool.core.date.DateUnit;
    import cn.hutool.core.date.DateUtil;
    import cn.hutool.core.util.RandomUtil;
    import cn.ken.blog.common.constant.Constants;
    import cn.ken.blog.common.domain.Result;
    import cn.ken.blog.common.enums.ErrorCodeEnum;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.util.ObjectUtils;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.Date;
    import java.util.concurrent.TimeUnit;
    
    /**
     * 验证码控制器
     * @author Ken-Chy129
     * @date 2022/4/17 20:28
     */
    @RestController
    @SuppressWarnings(value = { "unchecked", "rawtypes" })
    public class CaptureController {
        
        @Autowired
        private RedisTemplate redisTemplate;
        
        // 生成验证码
        @GetMapping("getNumCode")
        public Result<String> getNumCode(String phone) {
            String captureLimitKey = Constants.CAPTCHA_LIMIT_KEY + phone;
            Integer counts = (Integer) redisTemplate.opsForValue().get(captureLimitKey);
            if (ObjectUtils.isEmpty(counts)) {
                // 今天第一次验证,故之前缓存中无该键
                // 距离今天结束剩下多少毫秒
                long expire = DateUtil.endOfDay(new Date()).between(new Date(), DateUnit.MS);
                redisTemplate.opsForValue().set(captureLimitKey, 1, expire, TimeUnit.MILLISECONDS);
            } else if (counts < 3) {
                // 没有超过限制次数
                redisTemplate.opsForValue().increment(captureLimitKey);
            } else {
                // 超过限制次数,不生成验证码,直接返回
                return new Result<String>().error(ErrorCodeEnum.OVER_LIMITS);
            }
            // 生成验证码
            String code = RandomUtil.randomNumbers(6); // 随机生成六位数
            String captureCodeKey = Constants.CAPTCHA_CODE_KEY + phone;
            redisTemplate.opsForValue().set(captureCodeKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
            return new Result<String>().success(captureCodeKey + ":" + code);
        }
        
        // 验证验证码
        @GetMapping("verify")
        public Result<String> verify(String phone, String code) {
            String captureCodeKey = Constants.CAPTCHA_CODE_KEY + phone;
            String realCode = (String) redisTemplate.opsForValue().get(captureCodeKey);
            if (ObjectUtils.isEmpty(realCode)) {
                // redis中不存在该用户生成的验证码,证明验证码以过期销毁
                return new Result<String>().error(ErrorCodeEnum.OVERDUE_CODE);
            }
            if (realCode.equals(code)) {
                return new Result<String>().success("验证成功");
            } else {
                return new Result<String>().error(ErrorCodeEnum.ERROR_CODE);
            }
        }
        
    //    @Scheduled(cron = "0 0 12 * * ?")
    //    private void clear() {
    //        redisTemplate.delete()
    //    }
    }
    // Constants类
    
    /**
     * 验证码 redis key
     */
    public static final String CAPTCHA_CODE_KEY = "captcha_codes:";
    
    /**
     * 每日限制 redis key
     */
    public static final String CAPTCHA_LIMIT_KEY = "captcha_limits:";
    /**
     * 验证码有效期(分钟)
     */
    public static final Integer CAPTCHA_EXPIRATION = 2;

以上がRedis はどのように検証コードの送信を実装し、毎日の送信数を制限しますか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はyisu.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。