網(wǎng)站首頁 編程語言 正文
1、功能
- 輸入手機號,點擊發(fā)送后隨機生成六位數(shù)字碼,2分鐘有效
- 輸入驗證碼,點擊驗證,返回成功或失敗
- 每個手機號每天只能輸3次
2、分析
- 每個手機每天只能輸3次:incr每次發(fā)送之后+1,當(dāng)值為3時提示不能發(fā)送,過期時間為當(dāng)天結(jié)束
- 隨機生成6位數(shù)字驗證碼:RandomUtil(hutool)
- 驗證碼2分鐘有效:放入redis里并設(shè)置過期時間2分鐘
- 判斷驗證碼是否一致:從redis里獲取驗證碼和輸入的驗證碼進(jìn)行比對
3、實現(xiàn)
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)) {
? ? ? ? ? ? // 今天第一次驗證,故之前緩存中無該鍵
? ? ? ? ? ? // 距離今天結(jié)束剩下多少毫秒
? ? ? ? ? ? long expire = DateUtil.endOfDay(new Date()).between(new Date(), DateUnit.MS);
? ? ? ? ? ? redisTemplate.opsForValue().set(captureLimitKey, 1, expire, TimeUnit.MILLISECONDS);
? ? ? ? } else if (counts < 3) {
? ? ? ? ? ? // 沒有超過限制次數(shù)
? ? ? ? ? ? redisTemplate.opsForValue().increment(captureLimitKey);
? ? ? ? } else {
? ? ? ? ? ? // 超過限制次數(shù),不生成驗證碼,直接返回
? ? ? ? ? ? return new Result<String>().error(ErrorCodeEnum.OVER_LIMITS);
? ? ? ? }
? ? ? ? // 生成驗證碼
? ? ? ? String code = RandomUtil.randomNumbers(6); // 隨機生成六位數(shù)
? ? ? ? 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
相關(guān)推薦
- 2022-06-21 Android?Studio實現(xiàn)簡易登錄界面制作_Android
- 2022-08-16 python中的屬性管理機制詳解_python
- 2022-07-06 Flutter?DateTime日期轉(zhuǎn)換的詳細(xì)使用_Android
- 2022-05-22 分享幾種python?變量合并方法_python
- 2022-07-09 python沒有g(shù)pu,如何改用cpu跑代碼_python
- 2022-09-17 oracle臨時表空間的作用與創(chuàng)建及相關(guān)操作詳解_oracle
- 2022-11-06 pandas?dataframe?drop函數(shù)介紹_python
- 2022-04-12 python?獲取list?長度_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支