Redis實現(xiàn)驗證碼發(fā)送并限制每日發(fā)送次數(shù)的示例代碼
更新時間:2022年04月18日 12:29:56 作者:得過且過的勇者y
本文主要介紹了Redis實現(xiàn)驗證碼發(fā)送并限制每日發(fā)送次數(shù)的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
1、功能
- 輸入手機號,點擊發(fā)送后隨機生成六位數(shù)字碼,2分鐘有效
- 輸入驗證碼,點擊驗證,返回成功或失敗
- 每個手機號每天只能輸3次
2、分析
- 每個手機每天只能輸3次:incr每次發(fā)送之后+1,當值為3時提示不能發(fā)送,過期時間為當天結束
- 隨機生成6位數(shù)字驗證碼:RandomUtil(hutool)
- 驗證碼2分鐘有效:放入redis里并設置過期時間2分鐘
- 判斷驗證碼是否一致:從redis里獲取驗證碼和輸入的驗證碼進行比對
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)) { ? ? ? ? ? ? // 今天第一次驗證,故之前緩存中無該鍵 ? ? ? ? ? ? // 距離今天結束剩下多少毫秒 ? ? ? ? ? ? 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;
到此這篇關于Redis實現(xiàn)驗證碼發(fā)送并限制每日發(fā)送次數(shù)的示例代碼的文章就介紹到這了,更多相關Redis驗證碼發(fā)送并限制次數(shù)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Redis 8種基本數(shù)據(jù)類型及常用命令和數(shù)據(jù)類型的應用場景小結
Redis是一種基于內存操作的數(shù)據(jù)庫,其中多虧于高效的數(shù)據(jù)結構,本文主要介紹了Redis 8種基本數(shù)據(jù)類型及常用命令和數(shù)據(jù)類型的應用場景小結,具有一定的參考價值,感興趣的可以了解一下2024-03-03Redis分布式鎖python-redis-lock使用方法
這篇文章主要介紹了Redis分布式鎖python-redis-lock使用方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-11-11Redis?HyperLogLog數(shù)據(jù)統(tǒng)計輕量級解決方案詳解
這篇文章主要為大家介紹了Redis?HyperLogLog數(shù)據(jù)統(tǒng)計輕量級解決方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12