refactor: 优化行为验证码 CaptchaService 服务获取方式,关闭行为验证码不会导致启动报错

Closes #91
This commit is contained in:
2025-07-08 21:40:08 +08:00
parent 1076b4a19b
commit dcc28bcf34

View File

@@ -24,6 +24,7 @@ import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.servlet.JakartaServletUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.anji.captcha.model.common.RepCodeEnum;
import com.anji.captcha.model.common.ResponseModel;
import com.anji.captcha.model.vo.CaptchaVO;
@@ -86,7 +87,6 @@ public class CaptchaController {
private final ApplicationProperties applicationProperties;
private final CaptchaProperties captchaProperties;
private final CaptchaService behaviorCaptchaService;
private final GraphicCaptchaService graphicCaptchaService;
private final OptionService optionService;
private final SmsConfigService smsConfigService;
@@ -95,6 +95,7 @@ public class CaptchaController {
@Operation(summary = "获取行为验证码", description = "获取行为验证码Base64编码")
@GetMapping("/behavior")
public Object getBehaviorCaptcha(CaptchaVO captchaReq, HttpServletRequest request) {
CaptchaService behaviorCaptchaService = SpringUtil.getBean(CaptchaService.class);
captchaReq.setBrowserInfo(JakartaServletUtil.getClientIP(request) + request.getHeader(HttpHeaders.USER_AGENT));
ResponseModel responseModel = behaviorCaptchaService.get(captchaReq);
CheckUtils.throwIf(() -> !StrUtil.equals(RepCodeEnum.SUCCESS.getCode(), responseModel
@@ -106,6 +107,7 @@ public class CaptchaController {
@Operation(summary = "校验行为验证码", description = "校验行为验证码")
@PostMapping("/behavior")
public Object checkBehaviorCaptcha(@RequestBody CaptchaVO captchaReq, HttpServletRequest request) {
CaptchaService behaviorCaptchaService = SpringUtil.getBean(CaptchaService.class);
captchaReq.setBrowserInfo(JakartaServletUtil.getClientIP(request) + request.getHeader(HttpHeaders.USER_AGENT));
return behaviorCaptchaService.check(captchaReq);
}
@@ -152,6 +154,7 @@ public class CaptchaController {
public R getMailCaptcha(@NotBlank(message = "邮箱不能为空") @Email(message = "邮箱格式不正确") String email,
CaptchaVO captchaReq) throws MessagingException {
// 行为验证码校验
CaptchaService behaviorCaptchaService = SpringUtil.getBean(CaptchaService.class);
ResponseModel verificationRes = behaviorCaptchaService.verification(captchaReq);
ValidationUtils.throwIfNotEqual(verificationRes.getRepCode(), RepCodeEnum.SUCCESS.getCode(), verificationRes
.getRepMsg());
@@ -198,6 +201,7 @@ public class CaptchaController {
@RateLimiter(name = CacheConstants.CAPTCHA_KEY_PREFIX, key = "#phone", rate = 30, interval = 1, unit = TimeUnit.MINUTES, type = LimitType.IP, message = "获取验证码操作太频繁,请稍后再试")})
public R getSmsCaptcha(@NotBlank(message = "手机号不能为空") @Mobile String phone, CaptchaVO captchaReq) {
// 行为验证码校验
CaptchaService behaviorCaptchaService = SpringUtil.getBean(CaptchaService.class);
ResponseModel verificationRes = behaviorCaptchaService.verification(captchaReq);
ValidationUtils.throwIfNotEqual(verificationRes.getRepCode(), RepCodeEnum.SUCCESS.getCode(), verificationRes
.getRepMsg());