优化:优化部分常量命名及使用

This commit is contained in:
Charles7c 2023-03-23 21:56:37 +08:00
parent 8591a24730
commit 069104c598
7 changed files with 21 additions and 25 deletions

View File

@ -29,17 +29,17 @@ import lombok.NoArgsConstructor;
public class CacheConsts { public class CacheConsts {
/** /**
* 登录用户缓存 * 登录用户
*/ */
public static final String LOGIN_USER_CACHE_KEY = "LOGIN_USER"; public static final String LOGIN_USER_KEY = "LOGIN_USER";
/** /**
* 验证码缓存 * 验证码前缀
*/ */
public static final String CAPTCHA_CACHE_KEY = "CAPTCHA"; public static final String CAPTCHA_KEY_PREFIX = "CAPTCHA";
/** /**
* 限流缓存 * 限流前缀
*/ */
public static final String LIMIT_CACHE_KEY = "LIMIT"; public static final String LIMIT_KEY_PREFIX = "LIMIT";
} }

View File

@ -41,7 +41,7 @@ public class SysConsts {
/** /**
* 全部权限标识 * 全部权限标识
*/ */
public static final String ALL_PERMISSION = "*"; public static final String ALL_PERMISSION = StringConsts.ASTERISK;
/** /**
* 默认密码 * 默认密码

View File

@ -66,8 +66,7 @@ public class LoginHelper {
// 登录保存用户信息 // 登录保存用户信息
StpUtil.login(loginUser.getId()); StpUtil.login(loginUser.getId());
loginUser.setToken(StpUtil.getTokenValue()); loginUser.setToken(StpUtil.getTokenValue());
SaHolder.getStorage().set(CacheConsts.LOGIN_USER_CACHE_KEY, loginUser); StpUtil.getTokenSession().set(CacheConsts.LOGIN_USER_KEY, loginUser);
StpUtil.getTokenSession().set(CacheConsts.LOGIN_USER_CACHE_KEY, loginUser);
} }
/** /**
@ -76,15 +75,12 @@ public class LoginHelper {
* @return / * @return /
*/ */
public static LoginUser getLoginUser() { public static LoginUser getLoginUser() {
LoginUser loginUser = (LoginUser)SaHolder.getStorage().get(CacheConsts.LOGIN_USER_CACHE_KEY); LoginUser loginUser = (LoginUser)SaHolder.getStorage().get(CacheConsts.LOGIN_USER_KEY);
if (loginUser != null) { if (loginUser != null) {
return loginUser; return loginUser;
} }
try { loginUser = (LoginUser)StpUtil.getTokenSession().get(CacheConsts.LOGIN_USER_KEY);
loginUser = (LoginUser)StpUtil.getTokenSession().get(CacheConsts.LOGIN_USER_CACHE_KEY); SaHolder.getStorage().set(CacheConsts.LOGIN_USER_KEY, loginUser);
SaHolder.getStorage().set(CacheConsts.LOGIN_USER_CACHE_KEY, loginUser);
} catch (Exception ignored) {
}
return loginUser; return loginUser;
} }
@ -95,8 +91,8 @@ public class LoginHelper {
* 登录用户信息 * 登录用户信息
*/ */
public static void updateLoginUser(LoginUser loginUser) { public static void updateLoginUser(LoginUser loginUser) {
SaHolder.getStorage().set(CacheConsts.LOGIN_USER_CACHE_KEY, loginUser); SaHolder.getStorage().set(CacheConsts.LOGIN_USER_KEY, loginUser);
StpUtil.getTokenSession().set(CacheConsts.LOGIN_USER_CACHE_KEY, loginUser); StpUtil.getTokenSession().set(CacheConsts.LOGIN_USER_KEY, loginUser);
} }
/** /**

View File

@ -65,7 +65,7 @@ public class LoginController {
@PostMapping("/login") @PostMapping("/login")
public R<LoginVO> login(@Validated @RequestBody LoginRequest loginRequest) { public R<LoginVO> login(@Validated @RequestBody LoginRequest loginRequest) {
// 校验验证码 // 校验验证码
String captchaKey = RedisUtils.formatKey(CacheConsts.CAPTCHA_CACHE_KEY, loginRequest.getUuid()); String captchaKey = RedisUtils.formatKey(CacheConsts.CAPTCHA_KEY_PREFIX, loginRequest.getUuid());
String captcha = RedisUtils.getCacheObject(captchaKey); String captcha = RedisUtils.getCacheObject(captchaKey);
ValidationUtils.throwIfBlank(captcha, "验证码已失效"); ValidationUtils.throwIfBlank(captcha, "验证码已失效");
RedisUtils.deleteCacheObject(captchaKey); RedisUtils.deleteCacheObject(captchaKey);

View File

@ -74,7 +74,7 @@ public class CaptchaController {
// 保存验证码 // 保存验证码
String uuid = IdUtil.fastSimpleUUID(); String uuid = IdUtil.fastSimpleUUID();
String captchaKey = RedisUtils.formatKey(CacheConsts.CAPTCHA_CACHE_KEY, uuid); String captchaKey = RedisUtils.formatKey(CacheConsts.CAPTCHA_KEY_PREFIX, uuid);
RedisUtils.setCacheObject(captchaKey, captcha.text(), RedisUtils.setCacheObject(captchaKey, captcha.text(),
Duration.ofMinutes(captchaImage.getExpirationInMinutes())); Duration.ofMinutes(captchaImage.getExpirationInMinutes()));
@ -88,9 +88,9 @@ public class CaptchaController {
public R getMailCaptcha( public R getMailCaptcha(
@NotBlank(message = "邮箱不能为空") @Pattern(regexp = RegexPool.EMAIL, message = "邮箱格式错误") String email) @NotBlank(message = "邮箱不能为空") @Pattern(regexp = RegexPool.EMAIL, message = "邮箱格式错误") String email)
throws MessagingException { throws MessagingException {
String limitCacheKey = CacheConsts.LIMIT_CACHE_KEY; String limitKeyPrefix = CacheConsts.LIMIT_KEY_PREFIX;
String captchaCacheKey = CacheConsts.CAPTCHA_CACHE_KEY; String captchaKeyPrefix = CacheConsts.CAPTCHA_KEY_PREFIX;
String limitCaptchaKey = RedisUtils.formatKey(limitCacheKey, captchaCacheKey, email); String limitCaptchaKey = RedisUtils.formatKey(limitKeyPrefix, captchaKeyPrefix, email);
long limitTimeInMillisecond = RedisUtils.getTimeToLive(limitCaptchaKey); long limitTimeInMillisecond = RedisUtils.getTimeToLive(limitCaptchaKey);
CheckUtils.throwIf(limitTimeInMillisecond > 0, "发送邮箱验证码过于频繁,请您 {}s 后再试", limitTimeInMillisecond / 1000); CheckUtils.throwIf(limitTimeInMillisecond > 0, "发送邮箱验证码过于频繁,请您 {}s 后再试", limitTimeInMillisecond / 1000);
@ -105,7 +105,7 @@ public class CaptchaController {
MailUtils.sendHtml(email, String.format("【%s】邮箱验证码", properties.getName()), content); MailUtils.sendHtml(email, String.format("【%s】邮箱验证码", properties.getName()), content);
// 保存验证码 // 保存验证码
String captchaKey = RedisUtils.formatKey(CacheConsts.CAPTCHA_CACHE_KEY, email); String captchaKey = RedisUtils.formatKey(captchaKeyPrefix, email);
RedisUtils.setCacheObject(captchaKey, captcha, Duration.ofMinutes(expirationInMinutes)); RedisUtils.setCacheObject(captchaKey, captcha, Duration.ofMinutes(expirationInMinutes));
RedisUtils.setCacheObject(limitCaptchaKey, captcha, Duration.ofSeconds(captchaMail.getLimitInSeconds())); RedisUtils.setCacheObject(limitCaptchaKey, captcha, Duration.ofSeconds(captchaMail.getLimitInSeconds()));
return R.ok(String.format("发送成功,验证码有效期 %s 分钟", expirationInMinutes)); return R.ok(String.format("发送成功,验证码有效期 %s 分钟", expirationInMinutes));

View File

@ -74,7 +74,7 @@ public class OnlineUserController {
// 获取 Token Session // 获取 Token Session
SaSession saSession = StpUtil.getTokenSessionByToken(token); SaSession saSession = StpUtil.getTokenSessionByToken(token);
LoginUser loginUser = saSession.get(CacheConsts.LOGIN_USER_CACHE_KEY, new LoginUser()); LoginUser loginUser = saSession.get(CacheConsts.LOGIN_USER_KEY, new LoginUser());
// 检查是否符合查询条件 // 检查是否符合查询条件
if (Boolean.TRUE.equals(checkQuery(query, loginUser))) { if (Boolean.TRUE.equals(checkQuery(query, loginUser))) {

View File

@ -103,7 +103,7 @@ public class UserCenterController {
ValidationUtils.throwIfBlank(rawCurrentPassword, "当前密码解密失败"); ValidationUtils.throwIfBlank(rawCurrentPassword, "当前密码解密失败");
// 校验验证码 // 校验验证码
String captchaKey = RedisUtils.formatKey(CacheConsts.CAPTCHA_CACHE_KEY, updateEmailRequest.getNewEmail()); String captchaKey = RedisUtils.formatKey(CacheConsts.CAPTCHA_KEY_PREFIX, updateEmailRequest.getNewEmail());
String captcha = RedisUtils.getCacheObject(captchaKey); String captcha = RedisUtils.getCacheObject(captchaKey);
ValidationUtils.throwIfBlank(captcha, "验证码已失效"); ValidationUtils.throwIfBlank(captcha, "验证码已失效");
ValidationUtils.throwIfNotEqualIgnoreCase(updateEmailRequest.getCaptcha(), captcha, "验证码错误"); ValidationUtils.throwIfNotEqualIgnoreCase(updateEmailRequest.getCaptcha(), captcha, "验证码错误");