[关闭]
@TedZhou 2020-11-10T03:35:37.000000Z 字数 1128 阅读 245

Springboot集成kaptcha图片验证码

java spring


kaptcha 是一个图像验证码生成和验证工具,有许多可配置项,可以简单快捷的生成各式各样的验证码,使用起来也很简便。

pom.xml添加依赖

  1. <dependency>
  2. <groupId>com.baomidou</groupId>
  3. <artifactId>kaptcha-spring-boot-starter</artifactId>
  4. <version>1.1.0</version>
  5. </dependency>

application.yaml 添加典型配置

不加用默认也可

  1. kaptcha:
  2. height: 50
  3. width: 200
  4. content:
  5. length: 4
  6. source: abcdefghjklmnopqrstuvwxyz23456789
  7. space: 2
  8. font:
  9. color: black
  10. name: Arial
  11. size: 40
  12. background-color:
  13. from: lightGray
  14. to: white
  15. border:
  16. enabled: true
  17. color: black
  18. thickness: 1

在controller里使用示例

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RequestParam;
  4. import org.springframework.web.bind.annotation.RestController;
  5. import com.baomidou.kaptcha.Kaptcha;
  6. @RestController
  7. @RequestMapping("/code")
  8. public class CodeController {
  9. @Autowired
  10. private Kaptcha kaptcha;
  11. @RequestMapping("/image")
  12. void renderImage() {
  13. String code = kaptcha.render();
  14. System.out.println(code);
  15. }
  16. @RequestMapping("/valid")
  17. boolean validImage(@RequestParam String code) {
  18. return kaptcha.validate(code);
  19. }
  20. }

测试

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注