Java集成验证码
1.java泛型2.Java面向对象三大特征3.Java设计模式——策略模式4.Java设计模式——代理模式5.Java设计模式——装饰者模式6.Java设计模式——单例模式+工厂模式7.Java 定时任务quartz8.java 基本操作9.JavaWeb获取信息10.Java操作Html11.java 定时任务12.Java 读取文件13.Java 发送邮件14.Java解析JSON数据15.java线程之CompletableFuture16.java8 新特性之日期时间 API17.java8 新特性之Optional 类18.java8 新特性之默认方法19.java8 新特性之方法引用20.java8 新特性之Lambda 表达式21.java8 新特性之函数式接口22.Java操作Zip
23.Java集成验证码
EasyCaptcha
maven地址
<dependency> <groupId>com.github.whvcse</groupId> <artifactId>easy-captcha</artifactId> <version>1.6.2</version> </dependency>
https://gitee.com/ele-admin/EasyCaptcha
效果图展示:

package com.epb.test; import com.wf.captcha.*; import java.io.FileOutputStream; public class Test001 { private static final String path = "C:\\Users\\Administrator\\Documents\\CaptchaTest"; public static void main(String[] args) { for (int i = 0; i < 5; i++) { //普通字母验证码 // specCaptcha(path+"/"+System.currentTimeMillis()+".png"); //普通字母闪烁验证码 // gifCaptcha(path+"/"+System.currentTimeMillis()+".gif"); //中文验证码 chineseCaptcha(path+"/"+System.currentTimeMillis()+".png"); //中文闪烁验证码 // chineseGifCaptcha(path+"/"+System.currentTimeMillis()+".gif"); //算数验证码 // arithmeticCaptcha(path + "/" + System.currentTimeMillis() + ".png"); } } public static void specCaptcha(String filePath) { // png类型 try (FileOutputStream os = new FileOutputStream(filePath)) { SpecCaptcha captcha = new SpecCaptcha(130, 48); captcha.text(); // 获取验证码的字符 captcha.textChar(); // 获取验证码的字符数组 captcha.out(os); } catch (Exception e) { e.printStackTrace(); } ; } public static void gifCaptcha(String filePath) { // png类型 try (FileOutputStream os = new FileOutputStream(filePath)) { GifCaptcha captcha = new GifCaptcha(130, 48); captcha.text(); // 获取验证码的字符 captcha.textChar(); // 获取验证码的字符数组 captcha.out(os); } catch (Exception e) { e.printStackTrace(); } ; } public static void chineseCaptcha(String filePath) { // png类型 try (FileOutputStream os = new FileOutputStream(filePath)) { ChineseCaptcha captcha = new ChineseCaptcha(130, 48); System.out.println(captcha.getFont().hashCode()); captcha.text(); // 获取验证码的字符 captcha.textChar(); // 获取验证码的字符数组 captcha.out(os); } catch (Exception e) { e.printStackTrace(); } ; } public static void chineseGifCaptcha(String filePath) { // png类型 try (FileOutputStream os = new FileOutputStream(filePath)) { ChineseGifCaptcha captcha = new ChineseGifCaptcha(130, 48); captcha.text(); // 获取验证码的字符 captcha.textChar(); // 获取验证码的字符数组 captcha.out(os); } catch (Exception e) { e.printStackTrace(); } ; } public static void arithmeticCaptcha(String filePath) { // png类型 try (FileOutputStream os = new FileOutputStream(filePath)) { ArithmeticCaptcha captcha = new ArithmeticCaptcha(130, 48); captcha.setLen(3); // 几位数运算,默认是两位 String arithmetic = captcha.getArithmeticString();// 获取运算的公式 System.out.println(arithmetic + " " + captcha.text()); // 获取运算的结果 captcha.out(os); // 输出验证码 } catch (Exception e) { e.printStackTrace(); } ; } }
kaptcha
可以随机文本,也可以自定义文本,参考链接:https://juejin.cn/post/6844903894661890055
maven地址:
<dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency>

package com.epb.test; import com.google.code.kaptcha.impl.DefaultKaptcha; import com.google.code.kaptcha.util.Config; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; import static com.google.code.kaptcha.Constants.*; import static com.google.code.kaptcha.Constants.KAPTCHA_OBSCURIFICATOR_IMPL; public class KaptchaTest { private static final String path = "C:\\Users\\Administrator\\Documents\\CaptchaTest"; public static void main(String[] args) { DefaultKaptcha producer = new DefaultKaptcha(); Config config = buildConfig(); producer.setConfig(config); // String codeString = producer.createText(); //可以自定义文本 String codeString = "8+3+9="; //生成图片验证码 BufferedImage image = producer.createImage(codeString); try(FileOutputStream os = new FileOutputStream(path+"/"+System.currentTimeMillis()+".png")) { ImageIO.write(image, "png", os);//写入流中 } catch (IOException e) { e.printStackTrace(); } } public static Config buildConfig(){ Properties properties = new Properties(); // 设置验证码图片宽度,默认为200 properties.setProperty(KAPTCHA_IMAGE_WIDTH, "160"); // 设置验证码图片高度,默认为50 properties.setProperty(KAPTCHA_IMAGE_HEIGHT, "60"); // 验证码文本字符大小 默认为40 properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_SIZE, "38"); // 设置边框,默认有 yes/no properties.setProperty(KAPTCHA_BORDER, "yes"); // 设置验证码文本字符颜色,默认为Color.BLACK properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_COLOR, "black"); // properties.put(KAPTCHA_TEXTPRODUCER_CHAR_STRING, "1234567890"); // KAPTCHA_SESSION_KEY properties.setProperty(KAPTCHA_SESSION_CONFIG_KEY, "kaptchaCode"); // 验证码文本字符长度,默认为5 properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "4"); // 验证码文本字体样式,默认为new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize) properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_NAMES, "Arial,Courier"); // 图片样式 水纹com.google.code.kaptcha.impl.WaterRipple 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy 阴影com.google.code.kaptcha.impl.ShadowGimpy properties.setProperty(KAPTCHA_OBSCURIFICATOR_IMPL, "com.google.code.kaptcha.impl.WaterRipple"); return new Config(properties); } }
HutoolCaptcha
maven引入
<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.8.15</version> </dependency>
文档地址:
https://www.hutool.cn/docs/#/captcha/概述

package com.epb.test; import cn.hutool.captcha.CaptchaUtil; import cn.hutool.captcha.LineCaptcha; import cn.hutool.captcha.ShearCaptcha; import cn.hutool.captcha.generator.MathGenerator; import cn.hutool.captcha.generator.RandomGenerator; public class HutoolCaptchaTest { private static final String path = "C:\\Users\\Administrator\\Documents\\CaptchaTest"; public static void main(String[] args) { for (int i = 0; i < 5; i++) { //随机英文 // lineCaptcha(path + "/" + System.currentTimeMillis() + ".png"); //随机数字 // randomNumber(path + "/" + System.currentTimeMillis() + ".png"); // 随机算术式 mathNumber(path + "/" + System.currentTimeMillis() + ".png"); } } public static void lineCaptcha(String filePath){ //定义图形验证码的长和宽 LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100, 4, 20); //图形验证码写出,可以写出到文件,也可以写出到流 lineCaptcha.write(filePath+"1.png"); //验证图形验证码的有效性,返回boolean值 lineCaptcha.verify("1234"); //重新生成验证码 lineCaptcha.createCode(); lineCaptcha.write(filePath+"2.png"); } public static void randomNumber(String filePath){ //定义图形验证码的长和宽 LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100, 4, 20); RandomGenerator randomGenerator = new RandomGenerator("0123456789", 4); lineCaptcha.setGenerator(randomGenerator); //图形验证码写出,可以写出到文件,也可以写出到流 lineCaptcha.write(filePath+"1.png"); //验证图形验证码的有效性,返回boolean值 lineCaptcha.verify("1234"); System.out.println(lineCaptcha.getCode()); } public static void mathNumber(String filePath){ //定义图形验证码的长和宽 ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(200, 45, 4, 4); captcha.setGenerator(new MathGenerator()); //图形验证码写出,可以写出到文件,也可以写出到流 captcha.write(filePath+"1.png"); //验证图形验证码的有效性,返回boolean值 captcha.verify("1234"); //90+69= 81-80= System.out.println(captcha.getCode()); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类