SpringBoot+Redis发送短信
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
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 "验证失败";
}
}
}