短信过期时效,图形验证码缓存清理

This commit is contained in:
mashuai 2024-06-06 15:52:11 +08:00
parent 37e8761508
commit 147c10476f
2 changed files with 23 additions and 17 deletions

View File

@ -78,36 +78,33 @@ public class TokenController {
//管理员用户需要额外校验手机短信验证码
String redisCode = redisService.getCacheObject("code_" + phone);
if (StringUtils.isEmpty(redisCode)) {
throw new ServiceException("验证码失效", 500);
throw new ServiceException("短信验证码失效", 500);
}
if (!StringUtils.equals(redisCode.split(GlobalConstants.STRING_UNDERLINE)[0], form.getTextCode())) {
throw new ServiceException("验证码错误", 500);
throw new ServiceException("短信验证码错误", 500);
} else {
redisService.deleteObject("code_" + phone);
}
}
String uuid = form.getUuid();
String decryptedData = RsaUtil.decryptByPrivateKey(form.getPassword(), privateKey);
Object cacheObject = redisService.getCacheObject(CacheConstants.CAPTCHA_CODE_KEY + uuid);
String captcha = cacheObject == null ? null : cacheObject.toString();
// 获取后立即删除图形验证码缓存
redisService.deleteObject(CacheConstants.CAPTCHA_CODE_KEY + uuid);
// 用户登录
LoginUser userInfo = sysLoginService.login(form.getUsername(), decryptedData);
if (decryptedData.equals(USER_PASSWORD)) {
userInfo.setCode(1);
}
String uuid = form.getUuid();
String captcha = redisService.getCacheObject(CacheConstants.CAPTCHA_CODE_KEY + uuid).toString();
if (StringUtils.isBlank(captcha)) {
// 删除验证码缓存
redisService.deleteObject(CacheConstants.CAPTCHA_CODE_KEY + uuid);
return R.fail("验证码超时,请重新刷新");
return R.fail("图形验证码超时,请重新刷新");
}
if (form.getCode() != null && form.getCode().equals(captcha)) {
// 删除验证码缓存
redisService.deleteObject(CacheConstants.CAPTCHA_CODE_KEY + uuid);
// 获取登录token
return R.ok(tokenService.createToken(userInfo));
} else {
// 删除验证码缓存
redisService.deleteObject(CacheConstants.CAPTCHA_CODE_KEY + uuid);
return R.fail("验证码错误");
return R.fail("图形验证码错误");
}
}
@ -146,9 +143,13 @@ public class TokenController {
String uuid = form.getUuid();
String captcha = redisService.getCacheObject(CacheConstants.CAPTCHA_CODE_KEY + uuid).toString();
if (StringUtils.isBlank(captcha)) {
// 删除验证码缓存
redisService.deleteObject(CacheConstants.CAPTCHA_CODE_KEY + uuid);
return R.fail("验证码超时,请重新刷新");
}
if (form.getCode() != null && !form.getCode().equals(captcha)) {
// 删除验证码缓存
redisService.deleteObject(CacheConstants.CAPTCHA_CODE_KEY + uuid);
return R.fail("验证码错误");
}
R<Boolean> sendState = remoteUserService.sendCode(form.getPhone());

View File

@ -23,6 +23,9 @@ import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
@ -162,8 +165,14 @@ public class SysSmsServiceImpl implements ISysSmsService {
} else {
sendCount++;
}
// 获取当前日期和时间
LocalDateTime now = LocalDateTime.now();
// 获取当天午夜时间
LocalDateTime midnight = LocalDateTime.of(now.toLocalDate(), LocalTime.MIDNIGHT).plusDays(1);
// 计算当前时间到当天午夜的时间差
long secondsUntilMidnight = now.until(midnight, ChronoUnit.SECONDS);
//存储一天手机号发送验证码数据
redisService.setCacheObject(key, sendCount, 1L, TimeUnit.DAYS);
redisService.setCacheObject(key, sendCount, secondsUntilMidnight, TimeUnit.SECONDS);
return success("手机号:" + phone + ",短信验证码发送成功 ");
} catch (Exception e) {
return AjaxResult.error("发送失败:" + e.getMessage());
@ -204,10 +213,6 @@ public class SysSmsServiceImpl implements ISysSmsService {
}
}
public static void main(String[] args) {
System.out.println(getSixBitCode());
}
/**
* 随机生成6位验证码
*/