Kaptcha-验证码使用及扩展
一、第三方验证码API
1、引入 kaptcha-datasource-spring-boot-starter。
引入kaptcha-spring-boot-starter: 基于 SpringBoot Google Kaptcha 验证码 快速启动器的验证码生成包
<dependency><groupId>com.baomidou</groupId><artifactId>kaptcha-spring-boot-starter</artifactId><version>${version}</version>
</dependency>
2、在Controller使用Kaptcha
@Autowiredprivate Kaptcha kaptcha;
@GetMapping("/generator")public void generatorCode(HttpServletRequest request, HttpServletResponse response) {System.out.println("-- 进入生成校验码 ---");kaptcha.render();}
3、提供的异常处理
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 "验证码渲染失败";}}
}
4、引入自定义配置
在application.yml中配置如下:
kaptcha:height: 50width: 200content:length: 4source: abcdefghjkmnopqrstuvwxyz23456789space: 2font:color: blackname: Arialsize: 40background-color:from: lightGrayto: whiteborder:enabled: truecolor: blackthickness: 1
5、验证结果
二、自定义验证码
查看源码,得知这个将验证码缓存在session中,存在一定的网络安全问题。
public String render() {this.response.setDateHeader("Expires", 0L);this.response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");this.response.addHeader("Cache-Control", "post-check=0, pre-check=0");this.response.setHeader("Pragma", "no-cache");this.response.setContentType("image/jpeg");String sessionCode = this.kaptcha.createText();
try {ServletOutputStream out = this.response.getOutputStream();Throwable var3 = null;
String var4;try {this.request.getSession().setAttribute("KAPTCHA_SESSION_KEY", sessionCode);this.request.getSession().setAttribute("KAPTCHA_SESSION_DATE", System.currentTimeMillis());ImageIO.write(this.kaptcha.createImage(sessionCode), "jpg", out);var4 = sessionCode;} catch (Throwable var14) {var3 = var14;throw var14;} finally {if (out != null) {if (var3 != null) {try {out.close();} catch (Throwable var13) {var3.addSuppressed(var13);}} else {out.close();}}
}
return var4;} catch (IOException var16) {throw new KaptchaRenderException(var16);}}
我们可以将此处存储方式改为redis存储。
@Overridepublic String render() {response.setDateHeader(HttpHeaders.EXPIRES, 0L);response.setHeader(HttpHeaders.CACHE_CONTROL, "no-store, no-cache, must-revalidate");response.addHeader(HttpHeaders.CACHE_CONTROL, "post-check=0, pre-check=0");response.setHeader(HttpHeaders.PRAGMA, "no-cache");response.setContentType("image/jpeg");String sessionCode = kaptcha.createText();try (ServletOutputStream out = response.getOutputStream()) {// 放到redisstringRedisTemplate.opsForValue().set("eric",sessionCode);ImageIO.write(kaptcha.createImage(sessionCode), "jpg", out);return sessionCode;} catch (IOException e) {throw new KaptchaRenderException(e);}}
Kaptcha-验证码使用及扩展相关推荐
- kaptcha验证码实现,配合spring boot使用
一.kaptcha介绍 Kaptcha是谷歌放在github上的一个验证码jar包,我们可以简单配置属性实现验证码的验证功能. kaptcha参数设置如下所示: Constant 描述 默认值 kap ...
- kaptcha验证码组件使用简介
kaptcha验证码组件使用简介 Kaptcha是一个基于SimpleCaptcha的验证码开源项目. 官网地址:http://code.google.com/p/kaptcha/ kaptcha的使 ...
- kaptcha 验证码在spring mvc 中的使用
转自:http://ttaale.iteye.com/blog/808719 kaptcha 是一个非常实用的验证码生成工具.有了它,你可以生成各种样式的验证码,因为它是可配置的.kaptcha工作的 ...
- java kaptcha_java相关:kaptcha验证码使用方法详解
java相关:kaptcha验证码使用方法详解 发布于 2020-6-7| 复制链接 摘记: 本文为大家分享了kaptcha验证码的使用方法,供大家参考,具体内容如下1.首先在pom.xml文件中导入 ...
- Springboot整合kaptcha验证码
Springboot整合kaptcha验证码 01.通过配置类来配置kaptcha 01-01.添加kaptcha的依赖: <!-- kaptcha验证码 --> <dependen ...
- springboot集成kaptcha验证码
导入依赖 <!-- kaptcha 验证码开源组件 --> <dependency><groupId>com.github.penggle</groupId& ...
- Gateway + Redis整合Kaptcha验证码
引言 验证码的作用: 防止自动化代码(爬虫,恶意脚本)来直接发送请求 确认用户信息的真实性 1. pom依赖 <dependency><groupId>org.springfr ...
- kaptcha html页面验证码,kaptcha验证码使用(示例代码)
效果图: kaptcha 是一个很有用的验证码生成工具.有了它,你能够生成各种样式的验证码,由于它是可配置的. kaptcha工作的原理是调用 com.google.code.kaptcha.serv ...
- Spring MVC 中使用 Google kaptcha 验证码
验证码是抵抗批量操作和恶意登录最有效的方式之一. 验证码从产生到现在已经衍生出了很多分支.方式.google kaptcha 是一个非常实用的验证码生成类库. 通过灵活的配置生成各种样式的验证码,并将 ...
- Java笔记-编码方式创建kaptcha验证码
这里使用编码的方式调用kaptcha实现验证码功能 程序运行截图如下: 输入错误后,还是这个界面,但输入正确后: maven如下: <?xml version="1.0" e ...
最新文章
- Windows Server应用之32位系统如何安装64位打印机驱动
- 【C/C++】代码优化技巧
- JS两个字符串日期相减得出天数
- Android Sqite数据库 6
- JS Addition
- 深入浅出 - 公钥、私钥和数字签名最通俗的理解
- leetcode python3 简单题111. Minimum Depth of Binary Tree
- 开发者该如何利用 C# 加解密?
- AFNetWorking 之 网络请求的基本知识
- 生成式对抗网络GAN生成手写数字
- 电磁兼容EMC标准 CISPR 22:EN 55022
- Python常用小技巧总结
- 如何把pdf文件转换为excel表格
- 中芯国际换帅,事情并不简单
- php 图片后加参数缩图,智能的PHP缩图类
- 解决:idea打开项目后卡住,界面一直白色
- Interval 类型总结
- 基于JAVA社区养老服务管理系统计算机毕业设计源码+系统+mysql数据库+lw文档+部署
- 网易云音乐打卡2.0(一天300首)10级指日可待
- About the ball Technical support