资讯 小学 初中 高中 语言 会计职称 学历提升 法考 计算机考试 医护考试 建工考试 教育百科
栏目分类:
子分类:
返回
空麓网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
空麓网 > 计算机考试 > 软件开发 > 后端开发 > Java

手机号码发送验证码限制, 对于每一个手机号码1分钟只能发一次,有效期5分钟 当天发送验证码的次数不超过了三次 ,使用 SpringBoot 和 Redis 进行开发

Java 更新时间: 发布时间: 计算机考试归档 最新发布

手机号码发送验证码限制, 对于每一个手机号码1分钟只能发一次,有效期5分钟 当天发送验证码的次数不超过了三次 ,使用 SpringBoot 和 Redis 进行开发

package com.example.tanwu.t4;import java.util.concurrent.TimeUnit;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.stereotype.Service;@Servicepublic class SmsVerificationCodeService {    @Autowired    private RedisTemplate redisTemplate;        public boolean sendVerificationCode(String mobile) {        // 1. 判断是否能够发送验证码        String lastSendTimeStr = redisTemplate.opsForValue().get("sms:sendtime:" + mobile);        if (lastSendTimeStr != null) {            long lastSendTime = Long.parseLong(lastSendTimeStr);            if (System.currentTimeMillis() - lastSendTime < 60 * 1000) {                // 距离上次发送时间不足1分钟,不能发送验证码                return false;            }        }        // 2. 判断该手机号码是否超过发送次数限制        String countStr = redisTemplate.opsForValue().get("sms:count:" + mobile);        if (countStr != null && Integer.parseInt(countStr) >= 3) {            // 今天已经发送了3次,不能发送验证码            return false;        }        // 3. 生成验证码并存储        String verificationCode = generateVerificationCode();        redisTemplate.opsForValue().set("sms:code:" + mobile, verificationCode, 5, TimeUnit.MINUTES);        // 更新发送时间和发送次数        if (lastSendTimeStr == null) {            redisTemplate.opsForValue().set("sms:sendtime:" + mobile, String.valueOf(System.currentTimeMillis()));        } else {            redisTemplate.opsForValue().set("sms:sendtime:" + mobile, String.valueOf(System.currentTimeMillis()), 1, TimeUnit.MINUTES);        }        if (countStr == null) {            redisTemplate.opsForValue().set("sms:count:" + mobile, "1", 1, TimeUnit.DAYS);        } else {            redisTemplate.opsForValue().increment("sms:count:" + mobile);        }        // 4. TODO:发送短信验证码        return true;    }        public boolean verifyVerificationCode(String mobile, String verificationCode) {        String code = redisTemplate.opsForValue().get("sms:code:" + mobile);        if (code != null && code.equals(verificationCode)) {            redisTemplate.delete("sms:code:" + mobile);            return true;        }        return false;    }        private String generateVerificationCode() {        // TODO:生成随机数字验证码        return "123456";    }}
package com.example.tanwu.t4;import com.baomidou.mybatisplus.core.toolkit.StringUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.http.ResponseEntity;import org.springframework.web.bind.annotation.RequestParam;import java.util.Random;import java.util.concurrent.TimeUnit;//@RestControllerpublic class SmsController {    @Autowired    private RedisTemplate redisTemplate;    //@PostMapping("/sms")    public ResponseEntity sendSms(@RequestParam("phone") String phone) {        // 判断该手机号码是否已经发送过验证码        String lastSendTime = redisTemplate.opsForValue().get("sms:lastSendTime:" + phone);        if (StringUtils.isNotBlank(lastSendTime)) {            long interval = System.currentTimeMillis() - Long.parseLong(lastSendTime);            if (interval < 60000) { // 一分钟只能发送一次                return ResponseEntity.badRequest().body("短信发送频率过高,请稍后再试。");            }        }        // 判断该手机号码今天是否发送过超过三次验证码        String count = redisTemplate.opsForValue().get("sms:count:" + phone);        if (StringUtils.isNotBlank(count) && Integer.parseInt(count) >= 3) {            return ResponseEntity.badRequest().body("该手机号码今天发送验证码的次数已达到上限。");        }        // 生成验证码        String code = generateCode();        // 将验证码存入 Redis 中,并设置有效期为 5 分钟        redisTemplate.opsForValue().set("sms:code:" + phone, code, 5, TimeUnit.MINUTES);        // 更新该手机号码的发送时间和发送次数        redisTemplate.opsForValue().set("sms:lastSendTime:" + phone, String.valueOf(System.currentTimeMillis()));        redisTemplate.opsForValue().increment("sms:count:" + phone, 1);        // 发送短信验证码        sendSms(phone, code);        return ResponseEntity.ok("短信验证码已发送。");    }    // 生成随机验证码    private String generateCode() {        Random random = new Random();        return String.valueOf(100000 + random.nextInt(900000));    }    // 发送短信验证码    private void sendSms(String phone, String code) {        // 省略发送短信验证码的代码    }}
package com.example.tanwu.t4;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.scheduling.annotation.EnableScheduling;import org.springframework.scheduling.annotation.Scheduled;import java.util.Set;@Configuration@EnableSchedulingpublic class ScheduleConfig {    @Autowired    private RedisTemplate redisTemplate;    // 每天凌晨 0 点清除 Redis 中的验证码发送次数    @Scheduled(cron = "0 0 0 * * ?")    public void clearSmsCount() {        Set keys = redisTemplate.keys("sms:count:*");        redisTemplate.delete(keys);    }}
转载请注明:文章转载自 http://www.konglu.com/
本文地址:http://www.konglu.com/it/1097928.html
免责声明:

我们致力于保护作者版权,注重分享,被刊用文章【手机号码发送验证码限制, 对于每一个手机号码1分钟只能发一次,有效期5分钟 当天发送验证码的次数不超过了三次 ,使用 SpringBoot 和 Redis 进行开发】因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理,本文部分文字与图片资源来自于网络,转载此文是出于传递更多信息之目的,若有来源标注错误或侵犯了您的合法权益,请立即通知我们,情况属实,我们会第一时间予以删除,并同时向您表示歉意,谢谢!

我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2023 成都空麓科技有限公司

ICP备案号:蜀ICP备2023000828号-2