---
date: 2025-03-28 03:46:03
title: Captcha验证码使用
permalink: /pages/e40993
tags:
  - Spring Boot
author:
  name: 华总
  link: https://xiaoying.org.cn
categories:
  - 后端
  - 功能扩展
  - 通用模块

---






## 引用

首先在项目中引用EasyCaptchaService

```java
public class CaptchaController {
   ...
    @Resource
    private EasyCaptchaService captchaService;
   ...
}
```

## 生成验证码

```java
@PostMapping("/get")
@Operation(summary = "生成验证码")
public BaseResponse<CaptchaResult> captcha() {
    return captchaService.generateCaptcha();
}
```

## 校验验证码

```java
@PostMapping("/check")
@Operation(summary = "校验验证码")
public BaseResponse<String> verifyCaptcha(@RequestBody CaptchaCheck CaptchaCheck){
  String verifyCode = CaptchaCheck.getVerifyCode();
  String verifyCodeKey = CaptchaCheck.getVerifyCodeKey();
  return captchaService.verifyCaptcha(verifyCode, verifyCodeKey);
}
```



## 完整代码

```java
@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);

    }
}
```



