refactor(auth): 重构登录认证和验证码功能

- 创建 SystemAuthVo 类用于统一登录返回数据结构
- 创建 SystemCaptchaVo 类用于验证码信息封装
- 将验证码功能从 SystemAuthService 拆分为独立的 SystemCaptchaService
- 登录接口返回类型从 Map 改为 SystemAuthVo
- 验证码接口返回类型从 Map 改为 SystemCaptchaVo
- 登录时验证码校验改为调用 SystemCaptchaService
- 移除 SystemAuthService 中的验证码相关代码
- 统一数据返回格式,提高代码可读性
This commit is contained in:
2026-01-08 09:25:31 +08:00
parent da6fe40960
commit 547421ad44
9 changed files with 214 additions and 55 deletions
@@ -0,0 +1,16 @@
package cn.odboy.system.dal.model;
import cn.odboy.base.KitObject;
import lombok.Getter;
import lombok.Setter;
/**
* 登录成功后:token 与 用户信息
*/
@Getter
@Setter
public class SystemAuthVo extends KitObject {
private String token;
private SystemUserInfoVo user;
}
@@ -0,0 +1,22 @@
package cn.odboy.system.dal.model;
import cn.odboy.base.KitObject;
import lombok.Getter;
import lombok.Setter;
/**
* 验证码信息
*/
@Getter
@Setter
public class SystemCaptchaVo extends KitObject {
/**
* redis key
*/
private String uuid;
/**
* image base64
*/
private String img;
}
@@ -0,0 +1,59 @@
package cn.odboy.system.service;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.odboy.constant.CaptchaCodeEnum;
import cn.odboy.constant.SystemConst;
import cn.odboy.framework.exception.BadRequestException;
import cn.odboy.framework.properties.AppProperties;
import cn.odboy.framework.redis.KitRedisHelper;
import cn.odboy.system.dal.model.SystemCaptchaVo;
import cn.odboy.system.dal.redis.SystemCacheKey;
import com.wf.captcha.base.Captcha;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* 验证码
*/
@Service
public class SystemCaptchaService {
@Autowired
private KitRedisHelper redisHelper;
@Autowired
private AppProperties properties;
public SystemCaptchaVo getLoginCaptcha() {
// 获取运算的结果
Captcha captcha = properties.getLogin().getCaptchaSetting().getCaptcha();
String uuid = SystemCacheKey.CAPTCHA_LOGIN + IdUtil.simpleUUID();
//当验证码类型为 arithmetic时且长度 >= 2 时, captcha.text()的结果有几率为浮点型
String captchaValue = captcha.text();
if (captcha.getCharType() - 1 == CaptchaCodeEnum.ARITHMETIC.ordinal() &&
captchaValue.contains(SystemConst.SYMBOL_DOT)) {
captchaValue = captchaValue.split("\\.")[0];
}
// 保存
redisHelper.set(uuid, captchaValue, properties.getLogin().getCaptchaSetting().getExpiration(), TimeUnit.MINUTES);
// 验证码信息
SystemCaptchaVo captchaVo = new SystemCaptchaVo();
captchaVo.setUuid(uuid);
captchaVo.setImg(captcha.toBase64());
return captchaVo;
}
public void validate(String uuid, String inputCode) {
// 查询验证码
String code = redisHelper.get(uuid, String.class);
if (StrUtil.isBlank(code)) {
throw new BadRequestException("验证码不存在或已过期");
}
// 清除验证码
redisHelper.del(uuid);
if (StrUtil.isBlank(inputCode) || !code.equalsIgnoreCase(inputCode)) {
throw new BadRequestException("验证码错误");
}
}
}