Skip to content

Commit dcc28bc

Browse files
committed
refactor: 优化行为验证码 CaptchaService 服务获取方式,关闭行为验证码不会导致启动报错
Closes #91
1 parent 1076b4a commit dcc28bc

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

continew-server/src/main/java/top/continew/admin/controller/common/CaptchaController.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import cn.hutool.core.util.RandomUtil;
2525
import cn.hutool.core.util.StrUtil;
2626
import cn.hutool.extra.servlet.JakartaServletUtil;
27+
import cn.hutool.extra.spring.SpringUtil;
2728
import com.anji.captcha.model.common.RepCodeEnum;
2829
import com.anji.captcha.model.common.ResponseModel;
2930
import com.anji.captcha.model.vo.CaptchaVO;
@@ -86,7 +87,6 @@ public class CaptchaController {
8687

8788
private final ApplicationProperties applicationProperties;
8889
private final CaptchaProperties captchaProperties;
89-
private final CaptchaService behaviorCaptchaService;
9090
private final GraphicCaptchaService graphicCaptchaService;
9191
private final OptionService optionService;
9292
private final SmsConfigService smsConfigService;
@@ -95,6 +95,7 @@ public class CaptchaController {
9595
@Operation(summary = "获取行为验证码", description = "获取行为验证码(Base64编码)")
9696
@GetMapping("/behavior")
9797
public Object getBehaviorCaptcha(CaptchaVO captchaReq, HttpServletRequest request) {
98+
CaptchaService behaviorCaptchaService = SpringUtil.getBean(CaptchaService.class);
9899
captchaReq.setBrowserInfo(JakartaServletUtil.getClientIP(request) + request.getHeader(HttpHeaders.USER_AGENT));
99100
ResponseModel responseModel = behaviorCaptchaService.get(captchaReq);
100101
CheckUtils.throwIf(() -> !StrUtil.equals(RepCodeEnum.SUCCESS.getCode(), responseModel
@@ -106,6 +107,7 @@ public Object getBehaviorCaptcha(CaptchaVO captchaReq, HttpServletRequest reques
106107
@Operation(summary = "校验行为验证码", description = "校验行为验证码")
107108
@PostMapping("/behavior")
108109
public Object checkBehaviorCaptcha(@RequestBody CaptchaVO captchaReq, HttpServletRequest request) {
110+
CaptchaService behaviorCaptchaService = SpringUtil.getBean(CaptchaService.class);
109111
captchaReq.setBrowserInfo(JakartaServletUtil.getClientIP(request) + request.getHeader(HttpHeaders.USER_AGENT));
110112
return behaviorCaptchaService.check(captchaReq);
111113
}
@@ -152,6 +154,7 @@ public CaptchaResp getImageCaptcha() {
152154
public R getMailCaptcha(@NotBlank(message = "邮箱不能为空") @Email(message = "邮箱格式不正确") String email,
153155
CaptchaVO captchaReq) throws MessagingException {
154156
// 行为验证码校验
157+
CaptchaService behaviorCaptchaService = SpringUtil.getBean(CaptchaService.class);
155158
ResponseModel verificationRes = behaviorCaptchaService.verification(captchaReq);
156159
ValidationUtils.throwIfNotEqual(verificationRes.getRepCode(), RepCodeEnum.SUCCESS.getCode(), verificationRes
157160
.getRepMsg());
@@ -198,6 +201,7 @@ public R getMailCaptcha(@NotBlank(message = "邮箱不能为空") @Email(message
198201
@RateLimiter(name = CacheConstants.CAPTCHA_KEY_PREFIX, key = "#phone", rate = 30, interval = 1, unit = TimeUnit.MINUTES, type = LimitType.IP, message = "获取验证码操作太频繁,请稍后再试")})
199202
public R getSmsCaptcha(@NotBlank(message = "手机号不能为空") @Mobile String phone, CaptchaVO captchaReq) {
200203
// 行为验证码校验
204+
CaptchaService behaviorCaptchaService = SpringUtil.getBean(CaptchaService.class);
201205
ResponseModel verificationRes = behaviorCaptchaService.verification(captchaReq);
202206
ValidationUtils.throwIfNotEqual(verificationRes.getRepCode(), RepCodeEnum.SUCCESS.getCode(), verificationRes
203207
.getRepMsg());

0 commit comments

Comments
 (0)