# kaptcha-spring-boot-starter-webflux
**Repository Path**: ITjijicai/kaptcha-spring-boot-starter-webflux
## Basic Information
- **Project Name**: kaptcha-spring-boot-starter-webflux
- **Description**: 基于 SpringBoot Google Kaptcha 验证码 快速启动器
- **Primary Language**: Java
- **License**: Apache-2.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 1
- **Forks**: 146
- **Created**: 2021-01-21
- **Last Updated**: 2024-06-25
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
简单快速集成 Google Kaptcha验证码
QQ群:710314529
## 如何使用
1. 引入 kaptcha-datasource-spring-boot-starter。
```xml
com.baomidou
kaptcha-spring-boot-starter-webflux
2.0.0
```
2. 在Controller使用`Kaptcha`。
```java
@RestController
@RequestMapping("/kaptcha")
public class KaptchaController {
@Autowired
private Kaptcha kaptcha;
@GetMapping("/render")
public Mono render(ServerWebExchange webExchange) {
return kaptcha.render(webExchange);
}
@PostMapping("/valid")
public Mono validDefaultTime(@RequestParam final Mono monoCode, ServerWebExchange webExchange) {
//default timeout 900 seconds
return monoCode
.flatMap(code -> kaptcha.validate(code, webExchange))
.doOnNext(System.out::println)
.map(ResponseEntity::ok);
}
@PostMapping("/validTime")
public Mono validCustomTime(@RequestParam final Mono monoCode, ServerWebExchange webExchange) {
return monoCode
.flatMap(code -> kaptcha.validate(code,60, webExchange))
.doOnNext(System.out::println)
.map(ResponseEntity::ok);
}
}
```
3. 发生错误会抛出异常,建议使用全局异常来处理。
```
KaptchaException //super Exception
KaptchaIncorrectException
KaptchaNotFoundException
KaptchaTimeoutException
KaptchaRenderException //If something is wrong then Image.write when render.
```
```java
import com.baomidou.kaptcha.exception.KaptchaException;
import com.baomidou.kaptcha.exception.KaptchaIncorrectException;
import com.baomidou.kaptcha.exception.KaptchaNotFoundException;
import com.baomidou.kaptcha.exception.KaptchaTimeoutException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler
public String handle(KaptchaException ex) {
if (ex instanceof KaptchaIncorrectException) {
return "验证码不正确";
} else if (ex instanceof KaptchaNotFoundException) {
return "验证码未找到";
} else if (ex instanceof KaptchaTimeoutException) {
return "验证码过期";
} else {
return "验证码渲染失败";
}
}
}
```
4. 自定义验证码参数,以下为默认配置。
```yaml
kaptcha:
height: 50
width: 200
content:
length: 4
source: abcdefghjklmnopqrstuvwxyz23456789
space: 2
font:
color: black
name: Arial
size: 40
background-color:
from: lightGray
to: white
border:
enabled: true
color: black
thickness: 1
```