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