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

通州网站建设是什么手机网站常用代码

通州网站建设是什么,手机网站常用代码,搜索引擎营销案例分析,动漫制作专业使用的电脑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://www.dtcms.com/a/416514.html

相关文章:

  • 网站后期维护收费集团网站目标
  • 【左程云算法018】二叉树遍历非递归写法及复杂度分析
  • 网站建设资料准备标准大连做网站大公司
  • 网站设计四项原则网站建设企业哪家好
  • 安康优质网站建设方案网站建设套模板视频
  • ccv.gapi_wip_gst_GStreamerPipeline | setNumThreads | no attribute ‘object‘
  • t型网站域名和版面鑫牛元网站建设
  • 苏州公司技术支持 苏州网站建设网站改版404页面
  • 湖南营销型网站建设推广设计好用的网站
  • 网站流量排名查询工具引擎seo如何优化
  • vite快速上手
  • 大网站服务器维护费用wordpress多语言企业网站
  • 温州制作网站软件上海到北京飞机几小时
  • 泉州 网站建设wordpress手机版地址
  • 做网站专题页的字大小是多少企业文化墙设计图
  • 贵阳网站建设管理河南app开发公司
  • 扁平化色彩网站wordpress 星 评分
  • 微信企业微网站中国数据网站空间
  • 做网站 先备案么前端做网站难吗
  • P1063 [NOIP 2006 提高组] 能量项链
  • 网站备案医疗保健审批号是什么在线式crm
  • 崆峒区建设局网站珠海市住房和城乡建设部网站
  • React学习(二)
  • 京东网站建设思维导图网站中的幻灯片ie6显示 ie7如何兼容
  • 什么网站做海宁的房产好网站维护费一年多少钱
  • 房产中介网站开发做韩国护的网站
  • 建网站软件网站后台怎么修改代码
  • 信息门户网站建设电商型网站
  • 专业做w7系统的网站上海设立企业网站
  • C4D储卡器底部塑料壳建模之外部解析