您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 茂名分类信息网,免费分类信息发布

Redis怎么实现验证码发送并限制每日发送次数

2024/3/15 13:15:21发布11次查看
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;
以上就是redis怎么实现验证码发送并限制每日发送次数的详细内容。
茂名分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录