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;
@Service
public class SmsVerificationCodeService {
@Autowired
private RedisTemplate<String, String> redisTemplate;
public boolean sendVerificationCode(String mobile) {
String lastSendTimeStr = redisTemplate.opsForValue().get("sms:sendtime:" + mobile);
if (lastSendTimeStr != null) {
long lastSendTime = Long.parseLong(lastSendTimeStr);
if (System.currentTimeMillis() - lastSendTime < 60 * 1000) {
return false;
}
}
String countStr = redisTemplate.opsForValue().get("sms:count:" + mobile);
if (countStr != null && Integer.parseInt(countStr) >= 3) {
return false;
}
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);
}
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() {
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;
public class SmsController {
@Autowired
private RedisTemplate<String, String> redisTemplate;
public ResponseEntity<String> 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();
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
@EnableScheduling
public class ScheduleConfig {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Scheduled(cron = "0 0 0 * * ?")
public void clearSmsCount() {
Set<String> keys = redisTemplate.keys("sms:count:*");
redisTemplate.delete(keys);
}
}