当前位置: 首页 > wzjs >正文

核工业西南建设集团有限公司网站网站建设与搜索

核工业西南建设集团有限公司网站,网站建设与搜索,句容网站建设开发,ui设计风格SpringBoot集成easy-captcha图片验证码框架 此项目已经很久未维护,如有更好的选择,建议使用更好的选择!!! 一、引言 验证码(CAPTCHA)是现代应用中防止机器人攻击、保护接口安全的核心手段之一。然而,从零开发验证码…

SpringBoot集成easy-captcha图片验证码框架

此项目已经很久未维护,如有更好的选择,建议使用更好的选择!!!

一、引言

验证码(CAPTCHA)是现代应用中防止机器人攻击、保护接口安全的核心手段之一。然而,从零开发验证码模块需要处理图形渲染、算法设计、安全防护等诸多复杂问题。Easy-Captcha 作为一款轻量级开源验证码框架,以简洁的API和高度可定制性成为开发者的优选方案。下面我们将介绍如何将Easy-Captcha框架整合到SpringBoot项目中。

二、依赖

<dependency><groupId>com.github.whvcse</groupId><artifactId>easy-captcha</artifactId><version>1.6.2</version>
</dependency>

三、代码

1. EasyCaptcha配置类

我们可以修改EasyCaptchaTypeEnum的枚举值来使用不同的验证码类型

@Configuration
@Data
public class EasyCaptchaConfig {/*** 验证码类型*/private EasyCaptchaTypeEnum type = EasyCaptchaTypeEnum.GIF;/*** 验证码缓存过期时间(单位:秒)*/private long ttl = 120L;/*** 验证码内容长度*/private int length = 4;/*** 验证码宽度*/private int width = 120;/*** 验证码高度*/private int height = 36;/*** 验证码字体*/private String fontName = "Verdana";/*** 字体风格*/private Integer fontStyle = Font.PLAIN;/*** 字体大小*/private int fontSize = 20;}

2. EasyCaptchaTypeEnum枚举类

/*** @desc: EasyCaptcha 验证码类型枚举* @author: shy* @date: 2025/02/27 16:55*/
public enum EasyCaptchaTypeEnum {/*** 算数*/ARITHMETIC,/*** 中文*/CHINESE,/*** 中文闪图*/CHINESE_GIF,/*** 闪图*/GIF,/*** png格式验证码*/SPEC
}

3. 验证码生成器

/*** @desc: 验证码生成器* @author: shy* @date: 2025/02/27 16:59*/
@Component
@RequiredArgsConstructor
public class EasyCaptchaProducer {private final EasyCaptchaConfig captchaConfig;public Captcha getCaptcha() {Captcha captcha;int width = captchaConfig.getWidth();int height = captchaConfig.getHeight();int length = captchaConfig.getLength();String fontName = captchaConfig.getFontName();switch (captchaConfig.getType()) {case ARITHMETIC:captcha = new ArithmeticCaptcha(width, height);//固定设置为两位,图片为算数运算表达式captcha.setLen(2);break;case CHINESE:captcha = new ChineseCaptcha(width, height);captcha.setLen(length);break;case CHINESE_GIF:captcha = new ChineseGifCaptcha(width, height);captcha.setLen(length);break;case GIF:captcha = new GifCaptcha(width, height);//最后一位是位数captcha.setLen(length);break;case SPEC:captcha = new SpecCaptcha(width, height);captcha.setLen(length);break;default:throw new RuntimeException("验证码配置信息错误!正确配置查看 CaptchaTypeEnum ");}// 使用默认字体即可解决中文乱码问题captcha.setFont(new Font(fontName, captchaConfig.getFontStyle(), captchaConfig.getFontSize()));return captcha;}
}

4. 验证码生成Service

/*** @desc: EasyCaptcha 业务类* @author: shy* @date: 2025/02/27 17:02*/
@Component
@RequiredArgsConstructor
public class EasyCaptchaService {private final EasyCaptchaProducer easyCaptchaProducer;private final EasyCaptchaConfig captchaConfig;/*** 获取EasyCaptcha图片验证码** @param* @return Captcha* @throws* @author shy* @date 2025/02/27 22:18*/public Captcha getCaptcha() {// 获取验证码Captcha captcha = easyCaptchaProducer.getCaptcha();// 验证码文本String captchaText = captcha.text();// todo 验证码文本存储Redis比对System.out.println("验证码文本:" + captchaText);return captcha;}
}

5. 对外接口

@GetMapping("/getEasyCaptcha")
@ApiOperation(value = "获取EasyCaptcha图片验证码", notes = "获取EasyCaptcha图片验证码", httpMethod = "GET")
public void getEasyCaptcha() {Captcha captcha = captchaService.getCaptcha();try {captcha.out(response.getOutputStream());// 以上两种方式都可以输出图片验证码//CaptchaUtil.out(captcha, request, response);} catch (IOException e) {throw new RuntimeException();}super.getEasyCaptcha();
}

四、验证码展示

SHY QR Code 动态验证码 SHY QR Code 中文验证码 SHY QR Code png格式验证码

五、总结

Easy-Captcha 通过模块化设计平衡了安全性与开发效率,其源码结构清晰(仅核心类约15个),适合二次开发。无论是快速实现基础验证功能,还是构建企业级人机验证系统,该框架都能提供可靠支持。建议结合具体业务需求,在验证码样式、验证流程上做深度定制。

GitHub 地址:https://github.com/whvcse/EasyCaptcha


文章转载自:

http://PFcuL7nI.cjxqx.cn
http://e2X2vc8c.cjxqx.cn
http://uVTKfeKW.cjxqx.cn
http://IM52uFpx.cjxqx.cn
http://M4SdpTXA.cjxqx.cn
http://o0YxHMDp.cjxqx.cn
http://t9HUKuey.cjxqx.cn
http://wfoGsVeA.cjxqx.cn
http://lkI2g2N3.cjxqx.cn
http://klsGOSWY.cjxqx.cn
http://gVIypD64.cjxqx.cn
http://W6ioJRlS.cjxqx.cn
http://9DpqKbMh.cjxqx.cn
http://I5KAx4wO.cjxqx.cn
http://xvJTmRW5.cjxqx.cn
http://rJPcf6LF.cjxqx.cn
http://QNqwDtk1.cjxqx.cn
http://FjNgREF0.cjxqx.cn
http://I2FPb778.cjxqx.cn
http://oUPQEVzM.cjxqx.cn
http://KzoBUhKH.cjxqx.cn
http://4Lh9vyXx.cjxqx.cn
http://1VG0sIfH.cjxqx.cn
http://pAEf1hvW.cjxqx.cn
http://7xBztTdw.cjxqx.cn
http://b1kkmmtL.cjxqx.cn
http://Jo1JMhh5.cjxqx.cn
http://Oev19DVc.cjxqx.cn
http://ySfnI88n.cjxqx.cn
http://1tps8ZUl.cjxqx.cn
http://www.dtcms.com/wzjs/731975.html

相关文章:

  • 网站封装成app可以做热更新济南市公共资源交易中心官网
  • 网站建设 英文学网站开发哪里好
  • 做贷款的网站有哪些网络营销名词解释是什么
  • 专业设计网站有哪些山西响应式网页建设报价
  • 部队内网网站建设方案谷歌手机网页版入口
  • 网站平台多少钱网站开发开源框架
  • 湘西吉首市建设局网站烟台建站模板源码
  • 怎么做动漫网站电影网站建设教程
  • 网站建设的企业目标做网站的公司叫什么名字好
  • 班级网站源代码乐清新闻
  • wordpress退出登录四川成都网站优化
  • 专业的上海网站建设公司哪家好上海市教育网官网
  • 网站制作寻找客户知名企业招聘信息
  • 哈尔滨 做网站iapp源码
  • 连锁加盟网站制作罗湖网站建设报价
  • 东胜区教育网站入口html网页设计大赛
  • 泉州网站建设哪家好微信小程序低代码开发平台
  • 汽贸做网站有用处吗网站建设如何财务处理
  • 无锡自助建网站微商分销如何搭建分销模式
  • 查看邮箱注册过的网站系统总裁
  • 公众号开发合同泉州网络seo
  • 优秀排版设计图片seowhy什么意思
  • 有没有给宝宝做辅食的网站最热门的网络游戏排行
  • 泉州市住房和城乡建设部网站网站搜索排名优化价格
  • 如何做响应式的网站如何构建一个成交型网站
  • 南京市公共建设中心网站建设一个公司网站需要多少钱
  • 微信上做网站怎么做成都建设规划局网站
  • 公司网站建设费用科目网店培训班
  • 手机模板网站制作网络黄页进入有限公司
  • 建网站外包网站用什么开发