当前位置:网站首页>Redis实现验证码发送并限制每日发送次数
Redis实现验证码发送并限制每日发送次数
2022-04-21 19:30:00 【得过且过的勇者y】
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;
版权声明
本文为[得过且过的勇者y]所创,转载请带上原文链接,感谢
https://ken-chy129.blog.csdn.net/article/details/124239040
边栏推荐
- Common shell commands - grep (I)
- Relationship between deep learning, multi machine and multi card batchsize and learning rate
- APM industry awareness series - eight - 25 advantages of Devops
- The courage to face Ali
- JVM topic (XI) - garbage collection (II)
- Pytoch convolution
- leetcode18. 四数之和
- 数据分析之数据预处理
- Ribbon原理与Nacos的服务发现原理分析
- APM industry awareness series - II
猜你喜欢

Flitter Xcode packaging and publishing failed error ninety thousand one hundred and sixty-five

JVM topic (XI) - garbage collection (II)

Switch branch

Release announcement of HMS core version 6.4.0

邮件在Exchange中的路由过程

robotframework日志输出中文乱码

MySQL throughput

【C语言进阶】⑥函数指针详解

The fifth chapter uses Matplotlib to draw pie chart

Edgeboard records
随机推荐
Learn MySQL performance tuning and make your database smooth
HMS Core 6.4.0版本发布公告
detectron2 中yacs的使用解读
邮件在Exchange中的路由过程
Automatic control principle Chapter 5 - frequency method (mind map)
Some keywords of the robotframework cannot be used or are black
Easygbs has closed the video recording plan. Why are video files still generated?
MusicPlayer2.1版本
[hand pose estimation] [paper interpretation] 3D human pose estimation in rgbd images for robotic task learning
Rk3399 - add USB to serial driver
Switch branch
Analyzing the practical process of maker Education
On the significance of sustainable development of maker Education
MySQL MHA high availability cluster deployment and failover
如何让Join跑的更快?(文末送书)
Week to MySQL date
Abbexa donkey anti goat IgG (H & L) antibody
Comparison of F (ab ') 2 IgG isotype in abbexa goat
APM industry awareness series - II
Alwaysinstallelevated