苞米豆 — 谷歌验证码快速启动器 1.1.0 发布

栏目: 软件资讯 · 发布时间: 6年前

内容简介:https://gitee.com/baomidou/kaptcha-spring-boot-starter 修复了一个重复校验引起的Np bug 支持了jdk 1.7 简介 kaptcha-spring-boot-starter 基于 springBoot 和 Google Kaptcha 验证码组件,支持jdk1.7+,kaptc...

https://gitee.com/baomidou/kaptcha-spring-boot-starter

修复了一个重复校验引起的Np bug

支持了jdk 1.7

简介

kaptcha-spring-boot-starter 基于 springBoot 和 Google Kaptcha 验证码组件,支持jdk1.7+,kaptcha-spring-boot-starter可以很方便的集成验证码到你的系统中。

如何使用

  1. 引入 kaptcha-datasource-spring-boot-starter。

<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>kaptcha-spring-boot-starter</artifactId>
  <version>1.1.0</version>
</dependency>
  1. 在Controller使用Kaptcha

@RestController
@RequestMapping("/kaptcha")
public class KaptchaController {

  @Autowired
  private Kaptcha kaptcha;

  @GetMapping("/render")
  public void render() {
    kaptcha.render();
  }

  @PostMapping("/valid")
  public void validDefaultTime(@RequestParam String code) {
    //default timeout 900 seconds
    kaptcha.validate(code);
  }

  @PostMapping("/validTime")
  public void validWithTime(@RequestParam String code) {
    kaptcha.validate(code, 60);
  }

}
  1. 发生错误会抛出异常,建议使用全局异常来处理。

KaptchaException  //super Exception

KaptchaIncorrectException

KaptchaNotFoundException

KaptchaTimeoutException

KaptchaRenderException //If something is wrong then Image.write when render.
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(value = KaptchaException.class)
  public String kaptchaExceptionHandler(KaptchaException kaptchaException) {
    if (kaptchaException instanceof KaptchaIncorrectException) {
      return "验证码不正确";
    } else if (kaptchaException instanceof KaptchaNotFoundException) {
      return "验证码未找到";
    } else if (kaptchaException instanceof KaptchaTimeoutException) {
      return "验证码过期";
    } else {
      return "验证码渲染失败";
    }

  }

}
  1. 自定义验证码参数,以下为默认配置。

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

【声明】文章转载自:开源中国社区 [http://www.oschina.net]


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

女人的起源

女人的起源

[英]伊莲·摩根 / 刘筠 / 上海译文出版社 / 2007-8 / 19.00元

《圣经》上说,上帝先创造了男人。女人,不过是他身上的一条肋骨。 以男性为中心的生物学家,则用人类起源于丛林并进化为以狩猎为生的肉食动物的学说,来证明女性无论在体力和智力上,都处于从属的地位。 对此,本书首次为女性在人类进化史中的平等地位据理力争。它开一代风气之先,力图解开人类,特 别是女性的演化和起源之谜;而它提供的答案,则从女性的角度对人类的史前史做出了推测性的重构,极富革命性和破坏......一起来看看 《女人的起源》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具