短信过期时效,图形验证码缓存清理
This commit is contained in:
parent
37e8761508
commit
147c10476f
|
|
@ -78,36 +78,33 @@ public class TokenController {
|
||||||
//管理员用户需要额外校验手机短信验证码
|
//管理员用户需要额外校验手机短信验证码
|
||||||
String redisCode = redisService.getCacheObject("code_" + phone);
|
String redisCode = redisService.getCacheObject("code_" + phone);
|
||||||
if (StringUtils.isEmpty(redisCode)) {
|
if (StringUtils.isEmpty(redisCode)) {
|
||||||
throw new ServiceException("验证码失效", 500);
|
throw new ServiceException("短信验证码失效", 500);
|
||||||
}
|
}
|
||||||
if (!StringUtils.equals(redisCode.split(GlobalConstants.STRING_UNDERLINE)[0], form.getTextCode())) {
|
if (!StringUtils.equals(redisCode.split(GlobalConstants.STRING_UNDERLINE)[0], form.getTextCode())) {
|
||||||
throw new ServiceException("验证码错误", 500);
|
throw new ServiceException("短信验证码错误", 500);
|
||||||
} else {
|
} else {
|
||||||
redisService.deleteObject("code_" + phone);
|
redisService.deleteObject("code_" + phone);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
String uuid = form.getUuid();
|
||||||
String decryptedData = RsaUtil.decryptByPrivateKey(form.getPassword(), privateKey);
|
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);
|
LoginUser userInfo = sysLoginService.login(form.getUsername(), decryptedData);
|
||||||
if (decryptedData.equals(USER_PASSWORD)) {
|
if (decryptedData.equals(USER_PASSWORD)) {
|
||||||
userInfo.setCode(1);
|
userInfo.setCode(1);
|
||||||
}
|
}
|
||||||
String uuid = form.getUuid();
|
|
||||||
String captcha = redisService.getCacheObject(CacheConstants.CAPTCHA_CODE_KEY + uuid).toString();
|
|
||||||
if (StringUtils.isBlank(captcha)) {
|
if (StringUtils.isBlank(captcha)) {
|
||||||
// 删除验证码缓存
|
return R.fail("图形验证码超时,请重新刷新");
|
||||||
redisService.deleteObject(CacheConstants.CAPTCHA_CODE_KEY + uuid);
|
|
||||||
return R.fail("验证码超时,请重新刷新");
|
|
||||||
}
|
}
|
||||||
if (form.getCode() != null && form.getCode().equals(captcha)) {
|
if (form.getCode() != null && form.getCode().equals(captcha)) {
|
||||||
// 删除验证码缓存
|
|
||||||
redisService.deleteObject(CacheConstants.CAPTCHA_CODE_KEY + uuid);
|
|
||||||
// 获取登录token
|
// 获取登录token
|
||||||
return R.ok(tokenService.createToken(userInfo));
|
return R.ok(tokenService.createToken(userInfo));
|
||||||
} else {
|
} else {
|
||||||
// 删除验证码缓存
|
return R.fail("图形验证码错误");
|
||||||
redisService.deleteObject(CacheConstants.CAPTCHA_CODE_KEY + uuid);
|
|
||||||
return R.fail("验证码错误");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -146,9 +143,13 @@ public class TokenController {
|
||||||
String uuid = form.getUuid();
|
String uuid = form.getUuid();
|
||||||
String captcha = redisService.getCacheObject(CacheConstants.CAPTCHA_CODE_KEY + uuid).toString();
|
String captcha = redisService.getCacheObject(CacheConstants.CAPTCHA_CODE_KEY + uuid).toString();
|
||||||
if (StringUtils.isBlank(captcha)) {
|
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)) {
|
if (form.getCode() != null && !form.getCode().equals(captcha)) {
|
||||||
|
// 删除验证码缓存
|
||||||
|
redisService.deleteObject(CacheConstants.CAPTCHA_CODE_KEY + uuid);
|
||||||
return R.fail("验证码错误");
|
return R.fail("验证码错误");
|
||||||
}
|
}
|
||||||
R<Boolean> sendState = remoteUserService.sendCode(form.getPhone());
|
R<Boolean> sendState = remoteUserService.sendCode(form.getPhone());
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,9 @@ import org.apache.commons.collections4.CollectionUtils;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.LocalTime;
|
||||||
|
import java.time.temporal.ChronoUnit;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
@ -162,8 +165,14 @@ public class SysSmsServiceImpl implements ISysSmsService {
|
||||||
} else {
|
} else {
|
||||||
sendCount++;
|
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 + ",短信验证码发送成功 !");
|
return success("手机号:" + phone + ",短信验证码发送成功 !");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return AjaxResult.error("发送失败:" + e.getMessage());
|
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位验证码
|
* 随机生成6位验证码
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue