本页导航
article
Captcha验证码使用
AI摘要
本文介绍了在项目中集成和使用Captcha验证码的完整流程。主要内容包括:首先引用EasyCaptchaService服务,然后详细说明了生成验证码以及后续校验验证码的具体步骤,最后提供了完整的实现代码示例。文章核心是指导开发者如何快速实现验证码的生成与验证功能。
引用
首先在项目中引用EasyCaptchaService
public class CaptchaController {
...
@Resource
private EasyCaptchaService captchaService;
...
}
生成验证码
@PostMapping("/get")
@Operation(summary = "生成验证码")
public BaseResponse<CaptchaResult> captcha() {
return captchaService.generateCaptcha();
}
校验验证码
@PostMapping("/check")
@Operation(summary = "校验验证码")
public BaseResponse<String> verifyCaptcha(@RequestBody CaptchaCheck CaptchaCheck){
String verifyCode = CaptchaCheck.getVerifyCode();
String verifyCodeKey = CaptchaCheck.getVerifyCodeKey();
return captchaService.verifyCaptcha(verifyCode, verifyCodeKey);
}
完整代码
@RestController
@Slf4j
@RequestMapping("/captcha")
public class CaptchaController {
@Resource
private EasyCaptchaService captchaService;
@PostMapping("/get")
@Operation(summary = "生成验证码")
public BaseResponse<CaptchaResult> captcha() {
return captchaService.generateCaptcha();
}
@PostMapping("/check")
@Operation(summary = "校验验证码")
public BaseResponse<String> verifyCaptcha(@RequestBody CaptchaCheck CaptchaCheck) {
String verifyCode = CaptchaCheck.getVerifyCode();
String verifyCodeKey = CaptchaCheck.getVerifyCodeKey();
return captchaService.verifyCaptcha(verifyCode, verifyCodeKey);
}
}
最后更新于 2026-02-17 19:40