kaptcha-sprig-boot-starter基于sprigBoot2.0和GoogleKaptcha的验证码组件,kaptcha-sprig-boot-starter可以很方便的集成验证码到你的系统中。
如何使用
引入kaptcha-datasource-sprig-boot-starter。
<depedecy> <groupId>com.baomidou</groupId> <artifactId>kaptcha-sprig-boot-starter</artifactId> <versio>1.0.0</versio></depedecy>在Cotroller使用Kaptcha
@RestCotroller@RequestMappig("/kaptcha")public class KaptchaCotroller { @Autowired private Kaptcha kaptcha; @GetMappig("/reder") public void reder() { kaptcha.reder(); } @PostMappig("/valid") public void validDefaultTime(@RequestParam Strig code) { //default timeout 900 secods kaptcha.validate(code); } @PostMappig("/validTime") public void validWithTime(@RequestParam Strig code) { kaptcha.validate(code, 60); }}发生错误会抛出异常,建议使用全局异常来处理。
KaptchaExceptio //super ExceptioKaptchaIcorrectExceptioKaptchaNotFoudExceptioKaptchaTimeoutExceptioKaptchaRederExceptio //If somethig is wrog the Image.write whe reder.import com.baomidou.kaptcha.exceptio.KaptchaExceptio;import com.baomidou.kaptcha.exceptio.KaptchaIcorrectExceptio;import com.baomidou.kaptcha.exceptio.KaptchaNotFoudExceptio;import com.baomidou.kaptcha.exceptio.KaptchaTimeoutExceptio;import org.sprigframework.web.bid.aotatio.ExceptioHadler;import org.sprigframework.web.bid.aotatio.RestCotrollerAdvice;@RestCotrollerAdvicepublic class GlobalExceptioHadler { @ExceptioHadler(value = KaptchaExceptio.class) public Strig kaptchaExceptioHadler(KaptchaExceptio kaptchaExceptio) { if (kaptchaExceptio istaceof KaptchaIcorrectExceptio) { retur "验证码不正确"; } else if (kaptchaExceptio istaceof KaptchaNotFoudExceptio) { retur "验证码未找到"; } else if (kaptchaExceptio istaceof KaptchaTimeoutExceptio) { retur "验证码过期"; } else { retur "验证码渲染失败"; } }}自定义验证码参数,以下为默认配置。
kaptcha: height: 50 width: 200 cotet: legth: 4 source: abcdefghjklmopqrstuvwxyz23456789 space: 2 fot: color: black ame: Arial size: 40 backgroud-color: from: lightGray to: white border: eabled: true color: black thickess: 1
评论