網站首頁 編程語言 正文
1、功能
- 輸入手機號,點擊發送后隨機生成六位數字碼,2分鐘有效
- 輸入驗證碼,點擊驗證,返回成功或失敗
- 每個手機號每天只能輸3次
2、分析
- 每個手機每天只能輸3次:incr每次發送之后+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;
原文鏈接:https://blog.csdn.net/qq_25046827/article/details/124239040
相關推薦
- 2022-05-22 python_tkinter事件類型詳情_python
- 2022-09-23 Android?創建AIDL文件使用教程_Android
- 2022-05-20 jmeter連接數據庫The driver has not received any packets
- 2022-05-03 EF使用Code?First模式生成單數形式表名_實用技巧
- 2022-01-31 torch.save實現對網絡結構和模型參數的保存 & pytorch模型文件.pt .pt
- 2023-01-31 C#實現批量壓縮和解壓縮的示例代碼_C#教程
- 2021-11-08 深入解析golang中的標準庫flag_Golang
- 2022-03-30 python生成密碼字典詳解_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支