refactor: 适配 ContiNew Starter Redisson 自动配置

This commit is contained in:
2023-11-22 21:58:30 +08:00
parent f4c0919eda
commit a40e609ea1
11 changed files with 46 additions and 281 deletions

View File

@@ -41,13 +41,13 @@ import top.charles7c.cnadmin.auth.model.resp.UserInfoResp;
import top.charles7c.cnadmin.auth.service.LoginService;
import top.charles7c.cnadmin.common.constant.CacheConsts;
import top.charles7c.cnadmin.common.model.dto.LoginUser;
import top.charles7c.cnadmin.common.util.RedisUtils;
import top.charles7c.cnadmin.common.util.SecureUtils;
import top.charles7c.cnadmin.common.util.helper.LoginHelper;
import top.charles7c.cnadmin.common.util.validate.ValidationUtils;
import top.charles7c.cnadmin.monitor.annotation.Log;
import top.charles7c.cnadmin.system.model.resp.UserDetailResp;
import top.charles7c.cnadmin.system.service.UserService;
import top.charles7c.continew.starter.cache.redisson.util.RedisUtils;
import top.charles7c.continew.starter.core.util.ExceptionUtils;
/**
@@ -71,9 +71,9 @@ public class AuthController {
@PostMapping("/account")
public LoginResp accountLogin(@Validated @RequestBody AccountLoginReq loginReq) {
String captchaKey = RedisUtils.formatKey(CacheConsts.CAPTCHA_KEY_PREFIX, loginReq.getUuid());
String captcha = RedisUtils.getCacheObject(captchaKey);
String captcha = RedisUtils.get(captchaKey);
ValidationUtils.throwIfBlank(captcha, "验证码已失效");
RedisUtils.deleteCacheObject(captchaKey);
RedisUtils.delete(captchaKey);
ValidationUtils.throwIfNotEqualIgnoreCase(loginReq.getCaptcha(), captcha, "验证码错误");
// 用户登录
String rawPassword = ExceptionUtils.exToNull(() -> SecureUtils.decryptByRsaPrivateKey(loginReq.getPassword()));
@@ -88,10 +88,10 @@ public class AuthController {
public LoginResp emailLogin(@Validated @RequestBody EmailLoginReq loginReq) {
String email = loginReq.getEmail();
String captchaKey = RedisUtils.formatKey(CacheConsts.CAPTCHA_KEY_PREFIX, email);
String captcha = RedisUtils.getCacheObject(captchaKey);
String captcha = RedisUtils.get(captchaKey);
ValidationUtils.throwIfBlank(captcha, "验证码已失效");
ValidationUtils.throwIfNotEqualIgnoreCase(loginReq.getCaptcha(), captcha, "验证码错误");
RedisUtils.deleteCacheObject(captchaKey);
RedisUtils.delete(captchaKey);
String token = loginService.emailLogin(email);
return LoginResp.builder().token(token).build();
}
@@ -102,10 +102,10 @@ public class AuthController {
public LoginResp phoneLogin(@Validated @RequestBody PhoneLoginReq loginReq) {
String phone = loginReq.getPhone();
String captchaKey = RedisUtils.formatKey(CacheConsts.CAPTCHA_KEY_PREFIX, phone);
String captcha = RedisUtils.getCacheObject(captchaKey);
String captcha = RedisUtils.get(captchaKey);
ValidationUtils.throwIfBlank(captcha, "验证码已失效");
ValidationUtils.throwIfNotEqualIgnoreCase(loginReq.getCaptcha(), captcha, "验证码错误");
RedisUtils.deleteCacheObject(captchaKey);
RedisUtils.delete(captchaKey);
String token = loginService.phoneLogin(phone);
return LoginResp.builder().token(token).build();
}

View File

@@ -53,9 +53,9 @@ import top.charles7c.cnadmin.common.constant.RegexConsts;
import top.charles7c.cnadmin.common.model.resp.CaptchaResp;
import top.charles7c.cnadmin.common.model.resp.R;
import top.charles7c.cnadmin.common.util.MailUtils;
import top.charles7c.cnadmin.common.util.RedisUtils;
import top.charles7c.cnadmin.common.util.TemplateUtils;
import top.charles7c.cnadmin.common.util.validate.CheckUtils;
import top.charles7c.continew.starter.cache.redisson.util.RedisUtils;
/**
* 验证码 API
@@ -83,8 +83,7 @@ public class CaptchaController {
// 保存验证码
String uuid = IdUtil.fastUUID();
String captchaKey = RedisUtils.formatKey(CacheConsts.CAPTCHA_KEY_PREFIX, uuid);
RedisUtils.setCacheObject(captchaKey, captcha.text(),
Duration.ofMinutes(captchaImage.getExpirationInMinutes()));
RedisUtils.set(captchaKey, captcha.text(), Duration.ofMinutes(captchaImage.getExpirationInMinutes()));
return CaptchaResp.builder().uuid(uuid).img(captcha.toBase64()).build();
}
@@ -108,8 +107,8 @@ public class CaptchaController {
MailUtils.sendHtml(email, String.format("【%s】邮箱验证码", projectProperties.getName()), content);
// 保存验证码
String captchaKey = RedisUtils.formatKey(captchaKeyPrefix, email);
RedisUtils.setCacheObject(captchaKey, captcha, Duration.ofMinutes(expirationInMinutes));
RedisUtils.setCacheObject(limitCaptchaKey, captcha, Duration.ofSeconds(captchaMail.getLimitInSeconds()));
RedisUtils.set(captchaKey, captcha, Duration.ofMinutes(expirationInMinutes));
RedisUtils.set(limitCaptchaKey, captcha, Duration.ofSeconds(captchaMail.getLimitInSeconds()));
return R.ok(String.format("发送成功,验证码有效期 %s 分钟", expirationInMinutes));
}
@@ -136,8 +135,8 @@ public class CaptchaController {
CheckUtils.throwIf(!smsResponse.isSuccess(), "验证码发送失败");
// 保存验证码
String captchaKey = RedisUtils.formatKey(captchaKeyPrefix, phone);
RedisUtils.setCacheObject(captchaKey, captcha, Duration.ofMinutes(expirationInMinutes));
RedisUtils.setCacheObject(limitCaptchaKey, captcha, Duration.ofSeconds(captchaSms.getLimitInSeconds()));
RedisUtils.set(captchaKey, captcha, Duration.ofMinutes(expirationInMinutes));
RedisUtils.set(limitCaptchaKey, captcha, Duration.ofSeconds(captchaSms.getLimitInSeconds()));
return R.ok(String.format("发送成功,验证码有效期 %s 分钟", expirationInMinutes));
}
}

View File

@@ -40,7 +40,6 @@ import top.charles7c.cnadmin.common.constant.CacheConsts;
import top.charles7c.cnadmin.common.constant.RegexConsts;
import top.charles7c.cnadmin.common.enums.SocialSourceEnum;
import top.charles7c.cnadmin.common.model.resp.R;
import top.charles7c.cnadmin.common.util.RedisUtils;
import top.charles7c.cnadmin.common.util.SecureUtils;
import top.charles7c.cnadmin.common.util.helper.LoginHelper;
import top.charles7c.cnadmin.common.util.validate.ValidationUtils;
@@ -53,6 +52,7 @@ import top.charles7c.cnadmin.system.model.resp.AvatarResp;
import top.charles7c.cnadmin.system.model.resp.UserSocialBindResp;
import top.charles7c.cnadmin.system.service.UserService;
import top.charles7c.cnadmin.system.service.UserSocialService;
import top.charles7c.continew.starter.cache.redisson.util.RedisUtils;
import top.charles7c.continew.starter.core.util.ExceptionUtils;
import me.zhyd.oauth.model.AuthCallback;
@@ -114,10 +114,10 @@ public class UserCenterController {
ExceptionUtils.exToNull(() -> SecureUtils.decryptByRsaPrivateKey(updateReq.getCurrentPassword()));
ValidationUtils.throwIfBlank(rawCurrentPassword, "当前密码解密失败");
String captchaKey = RedisUtils.formatKey(CacheConsts.CAPTCHA_KEY_PREFIX, updateReq.getNewPhone());
String captcha = RedisUtils.getCacheObject(captchaKey);
String captcha = RedisUtils.get(captchaKey);
ValidationUtils.throwIfBlank(captcha, "验证码已失效");
ValidationUtils.throwIfNotEqualIgnoreCase(updateReq.getCaptcha(), captcha, "验证码错误");
RedisUtils.deleteCacheObject(captchaKey);
RedisUtils.delete(captchaKey);
userService.updatePhone(updateReq.getNewPhone(), rawCurrentPassword, LoginHelper.getUserId());
return R.ok("修改成功");
}
@@ -129,10 +129,10 @@ public class UserCenterController {
ExceptionUtils.exToNull(() -> SecureUtils.decryptByRsaPrivateKey(updateReq.getCurrentPassword()));
ValidationUtils.throwIfBlank(rawCurrentPassword, "当前密码解密失败");
String captchaKey = RedisUtils.formatKey(CacheConsts.CAPTCHA_KEY_PREFIX, updateReq.getNewEmail());
String captcha = RedisUtils.getCacheObject(captchaKey);
String captcha = RedisUtils.get(captchaKey);
ValidationUtils.throwIfBlank(captcha, "验证码已失效");
ValidationUtils.throwIfNotEqualIgnoreCase(updateReq.getCaptcha(), captcha, "验证码错误");
RedisUtils.deleteCacheObject(captchaKey);
RedisUtils.delete(captchaKey);
userService.updateEmail(updateReq.getNewEmail(), rawCurrentPassword, LoginHelper.getUserId());
return R.ok("修改成功");
}

View File

@@ -71,6 +71,9 @@ spring.data:
# 是否开启 SSL
ssl:
enabled: false
redisson:
enabled: true
mode: single
--- ### Spring Cache 配置
spring.cache:

View File

@@ -73,6 +73,9 @@ spring.data:
# 是否开启 SSL
ssl:
enabled: false
redisson:
enabled: true
mode: single
--- ### Spring Cache 配置
spring.cache: