SpringBoot+Redis发送短信
SpringBoot+Redis发送短信
pom.xml
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-data-redis
application.xml
spring:
data:
redis:
host: 192.168.226.134
password: 123456
实体类
package com.sin.pojo;
/**
* @createTime 2024/6/5 9:48
* @createAuthor SIN
* @use 验证码信息
*/
public class VerificationCode {
private String phoneNumber;
private String code;
// setter getter方法
}
RandomCodeUtil.java
package com.sin.utils;
import org.springframework.stereotype.Component;
import java.util.Random;
/**
* @createTime 2024/6/5 9:50
* @createAuthor SIN
* @use
*/
@Component
public class RandomCodeUtil {
// 生成6位随机数字验证码
public static String generateRandomCode() {
Random random = new Random();
int code = 100000 + random.nextInt(900000);
return String.valueOf(code);
}
}
VerifcationCodeService.java
package com.sin.service;
import java.time.Duration;
/**
* @createTime 2024/6/5 9:52
* @createAuthor SIN
* @use
*/
public interface VerifcationCodeService {
// 保存验证码到 Redis 并设置过期时间为60秒
public String saveVerificationCode(String phoneNumber) ;
// 检查验证码是否正确
public boolean checkVerificationCode(String phoneNumber, String code) ;
}
VerifcationCodeServiceImpl.java
package com.sin.service.impl;
import com.sin.service.VerifcationCodeService;
import com.sin.utils.RandomCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.TimeoutUtils;
import org.springframework.stereotype.Service;
import java.time.Duration;
/**
* @createTime 2024/6/5 9:52
* @createAuthor SIN
* @use
*/
@Service
public class VerifcationCodeServiceImpl implements VerifcationCodeService {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private RandomCodeUtil randomCodeUtil;
@Override
public String saveVerificationCode(String phoneNumber) {
String codeInRedis = stringRedisTemplate.opsForValue().get(phoneNumber);
if (codeInRedis == null) {
String code = randomCodeUtil.generateRandomCode();
stringRedisTemplate.opsForValue().set(phoneNumber, code, Duration.ofSeconds(60));
long currentTimeMillis = System.currentTimeMillis();
stringRedisTemplate.opsForValue().set(phoneNumber + "_sentTime", String.valueOf(currentTimeMillis)); // 记录发送时间
return "验证码已发送至手机";
} else {
// 如果已经发送过验证码,则计算距离下次发送的时间间隔
long timeLeft = getTimeLeft(phoneNumber);
return "信息已发送,请等待 " + timeLeft + " 秒后重试";
}
}
// 计算距离下次发送的时间间隔
private long getTimeLeft(String phoneNumber) {
String lastSentTimeStr = stringRedisTemplate.opsForValue().get(phoneNumber + "_sentTime");
if (lastSentTimeStr != null) {
long lastSentTime = Long.parseLong(lastSentTimeStr);
long currentTime = System.currentTimeMillis();
long elapsedTime = currentTime - lastSentTime;
long timeLeft = 60 - elapsedTime / 1000; // 时间间隔以秒为单位
return Math.max(timeLeft, 0); // 如果时间间隔小于0,则返回0
} else {
return 0; // 如果没有记录发送时间,则可以立即重试
}
}
@Override
public boolean checkVerificationCode(String phoneNumber, String code) {
String savedCode = stringRedisTemplate.opsForValue().get(phoneNumber);
return code.equals(savedCode);
}
}
VerificationCodeController.java
package com.sin.controller;
import com.sin.service.VerifcationCodeService;
import com.sin.utils.RandomCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class VerificationCodeController {
@Autowired
private VerifcationCodeService verificationCodeService;
// 发送验证码的接口
@PostMapping("/sendVerificationCode")
public String sendVerificationCode(@RequestParam String phoneNumber) {
return verificationCodeService.saveVerificationCode(phoneNumber);
}
// 验证验证码的接口
@PostMapping("/verifyCode")
public String verifyCode(@RequestParam String phoneNumber, @RequestParam String code) {
if (verificationCodeService.checkVerificationCode(phoneNumber, code)) {
return "验证通过";
} else {
return "验证失败";
}
}
}
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!



