SpringBoot集成圖片驗(yàn)證碼框架easy-captcha的詳細(xì)過(guò)程
SpringBoot集成圖片驗(yàn)證碼框架easy-captcha
此項(xiàng)目已經(jīng)很久未維護(hù),如有更好的選擇,建議使用更好的選擇!!!
一、引言
驗(yàn)證碼(CAPTCHA)是現(xiàn)代應(yīng)用中防止機(jī)器人攻擊、保護(hù)接口安全的核心手段之一。然而,從零開(kāi)發(fā)驗(yàn)證碼模塊需要處理圖形渲染、算法設(shè)計(jì)、安全防護(hù)等諸多復(fù)雜問(wèn)題。Easy-Captcha 作為一款輕量級(jí)開(kāi)源驗(yàn)證碼框架,以簡(jiǎn)潔的API和高度可定制性成為開(kāi)發(fā)者的優(yōu)選方案。下面我們將介紹如何將Easy-Captcha框架整合到SpringBoot項(xiàng)目中。
二、依賴
<dependency> <groupId>com.github.whvcse</groupId> <artifactId>easy-captcha</artifactId> <version>1.6.2</version> </dependency>
三、代碼
1. EasyCaptcha配置類
我們可以修改EasyCaptchaTypeEnum的枚舉值來(lái)使用不同的驗(yàn)證碼類型
@Configuration @Data public class EasyCaptchaConfig { /** * 驗(yàn)證碼類型 */ private EasyCaptchaTypeEnum type = EasyCaptchaTypeEnum.GIF; /** * 驗(yàn)證碼緩存過(guò)期時(shí)間(單位:秒) */ private long ttl = 120L; /** * 驗(yàn)證碼內(nèi)容長(zhǎng)度 */ private int length = 4; /** * 驗(yàn)證碼寬度 */ private int width = 120; /** * 驗(yàn)證碼高度 */ private int height = 36; /** * 驗(yàn)證碼字體 */ private String fontName = "Verdana"; /** * 字體風(fēng)格 */ private Integer fontStyle = Font.PLAIN; /** * 字體大小 */ private int fontSize = 20; }
2. EasyCaptchaTypeEnum枚舉類
/** * @desc: EasyCaptcha 驗(yàn)證碼類型枚舉 * @author: shy * @date: 2025/02/27 16:55 */ public enum EasyCaptchaTypeEnum { /** * 算數(shù) */ ARITHMETIC, /** * 中文 */ CHINESE, /** * 中文閃圖 */ CHINESE_GIF, /** * 閃圖 */ GIF, /** * png格式驗(yàn)證碼 */ SPEC }
3. 驗(yàn)證碼生成器
/** * @desc: 驗(yàn)證碼生成器 * @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); //固定設(shè)置為兩位,圖片為算數(shù)運(yùn)算表達(dá)式 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);//最后一位是位數(shù) captcha.setLen(length); break; case SPEC: captcha = new SpecCaptcha(width, height); captcha.setLen(length); break; default: throw new RuntimeException("驗(yàn)證碼配置信息錯(cuò)誤!正確配置查看 CaptchaTypeEnum "); } // 使用默認(rèn)字體即可解決中文亂碼問(wèn)題 captcha.setFont(new Font(fontName, captchaConfig.getFontStyle(), captchaConfig.getFontSize())); return captcha; } }
4. 驗(yàn)證碼生成Service
/** * @desc: EasyCaptcha 業(yè)務(wù)類 * @author: shy * @date: 2025/02/27 17:02 */ @Component @RequiredArgsConstructor public class EasyCaptchaService { private final EasyCaptchaProducer easyCaptchaProducer; private final EasyCaptchaConfig captchaConfig; /** * 獲取EasyCaptcha圖片驗(yàn)證碼 * * @param * @return Captcha * @throws * @author shy * @date 2025/02/27 22:18 */ public Captcha getCaptcha() { // 獲取驗(yàn)證碼 Captcha captcha = easyCaptchaProducer.getCaptcha(); // 驗(yàn)證碼文本 String captchaText = captcha.text(); // todo 驗(yàn)證碼文本存儲(chǔ)Redis比對(duì) System.out.println("驗(yàn)證碼文本:" + captchaText); return captcha; } }
5. 對(duì)外接口
@GetMapping("/getEasyCaptcha") @ApiOperation(value = "獲取EasyCaptcha圖片驗(yàn)證碼", notes = "獲取EasyCaptcha圖片驗(yàn)證碼", httpMethod = "GET") public void getEasyCaptcha() { Captcha captcha = captchaService.getCaptcha(); try { captcha.out(response.getOutputStream()); // 以上兩種方式都可以輸出圖片驗(yàn)證碼 //CaptchaUtil.out(captcha, request, response); } catch (IOException e) { throw new RuntimeException(); } super.getEasyCaptcha(); }
四、驗(yàn)證碼展示
動(dòng)態(tài)驗(yàn)證碼
中文驗(yàn)證碼
png格式驗(yàn)證碼
五、總結(jié)
Easy-Captcha 通過(guò)模塊化設(shè)計(jì)平衡了安全性與開(kāi)發(fā)效率,其源碼結(jié)構(gòu)清晰(僅核心類約15個(gè)),適合二次開(kāi)發(fā)。無(wú)論是快速實(shí)現(xiàn)基礎(chǔ)驗(yàn)證功能,還是構(gòu)建企業(yè)級(jí)人機(jī)驗(yàn)證系統(tǒng),該框架都能提供可靠支持。建議結(jié)合具體業(yè)務(wù)需求,在驗(yàn)證碼樣式、驗(yàn)證流程上做深度定制。
GitHub 地址:https://github.com/whvcse/EasyCaptcha
到此這篇關(guān)于SpringBoot集成圖片驗(yàn)證碼框架easy-captcha的文章就介紹到這了,更多相關(guān)SpringBoot驗(yàn)證碼easy-captcha內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java根據(jù)模板實(shí)現(xiàn)excel導(dǎo)出標(biāo)準(zhǔn)化
這篇文章主要為大家詳細(xì)介紹了Java如何根據(jù)模板實(shí)現(xiàn)excel導(dǎo)出標(biāo)準(zhǔn)化,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以參考下2024-03-03詳解Java如何通過(guò)Socket實(shí)現(xiàn)查詢IP
在本文中,我們來(lái)學(xué)習(xí)下如何找到連接到服務(wù)器的客戶端計(jì)算機(jī)的IP地址。我們將創(chuàng)建一個(gè)簡(jiǎn)單的客戶端-服務(wù)器場(chǎng)景,讓我們探索用于TCP/IP通信的java.net?API,感興趣的可以了解一下2022-10-10mybatis-plus中更新null值的問(wèn)題解決
本文主要介紹 mybatis-plus 中常使用的 update 相關(guān)方法的區(qū)別,以及更新 null 的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-04-04詳解Java的Struts框架中上傳文件和客戶端驗(yàn)證的實(shí)現(xiàn)
這篇文章主要介紹了Java的Struts框架中上傳文件和客戶端驗(yàn)證的實(shí)現(xiàn),Struts是Java的SSH三大web開(kāi)發(fā)框架之一,需要的朋友可以參考下2015-12-12springmvc+shiro自定義過(guò)濾器的實(shí)現(xiàn)代碼
這篇文章主要介紹了springmvc+shiro自定義過(guò)濾器的實(shí)現(xiàn)方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-10-10