简单的EasyCaptcha图片验证码学习
1. 需求
图片验证码是一种常见的验证形式,它通过生成一串随机数字或符号,并加入一些干扰像素,最终生成用于验证的图片。这种验证码的设计旨在增加破解难度,主要通过加大干扰强度来提高安全性。通过这种方式,图片验证码能够有效地防止密码被暴力猜解,从而提高登录客户账户的安全性。我想在项目登录的时候生成一个简单的图片验证码功能,所以进行了简单的调研,方便之后使用学习。
2. 工具介绍
2.1 简介
验证码部分有很多的工具类已经实现了,我们使用的时候不需要自己进行编写,直接使用工具类就好,此处我使用的是EasyCaptcha
这个工具包进行生成验证码,他是一个Java
图形验证码,支持gif、中文、算术等类型,可用于Java Web
、JavaSE
等项目。我们项目使用的是前后端分离项目,所以此处我们便只讨论前后端分离部分的使用方法
2.2 实现的效果
-
大写验证码
-
gif验证码
-
算数类型验证码
-
中文类型验证码
-
切换展示字体(内置)
2.3 引入方式
我们项目使用pom进行引入,具体引入坐标如下
<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
<version>1.6.2</version>
</dependency>
2.4 验证码字符展示类型
验证码展示类型可以在代码中自定义,具体可以使用charType
进行设置
// 构造器,参数长,高,验证字符个数
SpecCaptcha captcha = new SpecCaptcha(130, 48, 5);
captcha.setCharType(Captcha.TYPE_ONLY_NUMBER);
类型 | 描述 |
---|---|
TYPE_DEFAULT | 数字和字母混合 |
TYPE_ONLY_NUMBER | 纯数字 |
TYPE_ONLY_CHAR | 纯字母 |
TYPE_ONLY_UPPER | 纯大写字母 |
TYPE_ONLY_LOWER | 纯小写字母 |
TYPE_NUM_AND_UPPER | 数字和大写字母 |
2.5 字体设置
EasyCaptcha
当前可以设置多种字体,包含他定义10种字体以及系统默认支持的字体,可以使用font
属性进行设置,具体使用方式以及具体字体如下:
// 构造器,参数长,高,验证字符个数
SpecCaptcha captcha = new SpecCaptcha(130, 48, 5);
// 设置内置字体
captcha.setFont(Captcha.FONT_1);
// 设置系统字体
captcha.setFont(new Font("楷体", Font.PLAIN, 28));
2.6 输出形式
我们可以将验证码图片转义成base64
字符串输出,也可以转换为本地图片进行输出
-
base64
字符串输出// 构造器,参数长,高,验证字符个数 SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5); specCaptcha.toBase64(); // 如果不想要base64的头部data:image/png;base64, specCaptcha.toBase64(""); // 加一个空的参数即可
-
文件输出
FileOutputStream outputStream = new FileOutputStream(new File("d:/pic/validate.png")) // 构造器,参数长,高,验证字符个数 SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5); specCaptcha.out(outputStream);
3.测试
我们编写本地测试类进行测试,我们使用的是前后端分离,暂时是一个简单的测试案例,如果想要验证码有失效时间,则需要我们使用redis
的expire
功能,设定一个过期时间添加一个简单的过期验证即可,此处不多赘述。具体的测试过程如下:
-
在spring boot项目中引入工具类坐标
<dependency> <groupId>com.github.whvcse</groupId> <artifactId>easy-captcha</artifactId> <version>1.6.2</version> </dependency>
-
编写测试类
ValidatePicController
package cn.git.controller; import com.wf.captcha.ArithmeticCaptcha; import com.wf.captcha.ChineseCaptcha; import com.wf.captcha.ChineseGifCaptcha; import com.wf.captcha.SpecCaptcha; import com.wf.captcha.base.Captcha; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.awt.*; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; /** * @description: 验证码工具controller * @program: bank-credit-sy * @author: lixuchun * @create: 2023-05-03 */ @RestController @RequestMapping("/validate") public class ValidatePicController { /** * 生成验证码图片 * @return */ @GetMapping("/pic") public String pic() throws IOException, FontFormatException { // 图片的宽度,高度以及字符个数 SpecCaptcha specCaptcha = new SpecCaptcha(130,48,4); specCaptcha.setFont(Captcha.FONT_10); System.out.println("随机验证码为 : " + specCaptcha.text()); // 将验证码图片下载到本地 File file = new File("D:\\pic\\" + System.currentTimeMillis() + ".png"); specCaptcha.out(new FileOutputStream(file)); // 获取中文验证码,参数为图片的宽度,高度以及字符个数 ChineseCaptcha chineseCaptcha = new ChineseCaptcha(130, 48); File file2 = new File("D:\\pic\\" + System.currentTimeMillis() + ".png"); chineseCaptcha.out(new FileOutputStream(file2)); System.out.println("中文验证码为 : " + chineseCaptcha.text()); // 获取中文gif验证码 ChineseGifCaptcha chineseGifCaptcha = new ChineseGifCaptcha(130, 48); // 将验证码图片下载到本地 File file3 = new File("D:\\pic\\" + System.currentTimeMillis() + ".gif"); chineseGifCaptcha.out(new FileOutputStream(file3)); System.out.println("中文gif验证码为 : " + chineseGifCaptcha.text()); // 算数图验证码 ArithmeticCaptcha captcha = new ArithmeticCaptcha(130, 48); // 几位数运算,默认是两位 captcha.setLen(3); // 获取运算的公式, eg: 5+6=? System.out.println("算数验证表达式为 : " + captcha.getArithmeticString()); // 获取运算的结果 captcha.text(); System.out.println("算数验证码为 : " + captcha.text()); // 将验证码图片下载到本地 File file4 = new File("D:\\pic\\" + System.currentTimeMillis() + ".png"); captcha.out(new FileOutputStream(file4)); return "success"; } }
-
浏览器访问测试
我们使用浏览器进行接口访问
http://localhost:8088/validate/pic
,查看结果已经返回,那么我们看下后台以及本地生成图片是否正确
后台以及本地生成文件展示如下:
- 随机验证码 cC8J
- 中文验证码 起掉座那
- 中文gif验证码 门也老穿
- 算数验证码 8 * 5 - 0 = ?,结果40
- 随机验证码 cC8J