SpringBoot集成圖片驗證碼框架easy-captcha的詳細過程
SpringBoot集成圖片驗證碼框架easy-captcha
此項目已經(jīng)很久未維護,如有更好的選擇,建議使用更好的選擇!!!
一、引言
驗證碼(CAPTCHA)是現(xiàn)代應(yīng)用中防止機器人攻擊、保護接口安全的核心手段之一。然而,從零開發(fā)驗證碼模塊需要處理圖形渲染、算法設(shè)計、安全防護等諸多復(fù)雜問題。Easy-Captcha 作為一款輕量級開源驗證碼框架,以簡潔的API和高度可定制性成為開發(fā)者的優(yōu)選方案。下面我們將介紹如何將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; /** * 驗證碼內(nèi)容長度 */ private int length = 4; /** * 驗證碼寬度 */ private int width = 120; /** * 驗證碼高度 */ private int height = 36; /** * 驗證碼字體 */ private String fontName = "Verdana"; /** * 字體風(fēng)格 */ private Integer fontStyle = Font.PLAIN; /** * 字體大小 */ private int fontSize = 20; }
2. EasyCaptchaTypeEnum枚舉類
/** * @desc: EasyCaptcha 驗證碼類型枚舉 * @author: shy * @date: 2025/02/27 16:55 */ public enum EasyCaptchaTypeEnum { /** * 算數(shù) */ 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); //固定設(shè)置為兩位,圖片為算數(shù)運算表達式 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("驗證碼配置信息錯誤!正確配置查看 CaptchaTypeEnum "); } // 使用默認(rèn)字體即可解決中文亂碼問題 captcha.setFont(new Font(fontName, captchaConfig.getFontStyle(), captchaConfig.getFontSize())); return captcha; } }
4. 驗證碼生成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圖片驗證碼 * * @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(); }
四、驗證碼展示
動態(tài)驗證碼
中文驗證碼
png格式驗證碼
五、總結(jié)
Easy-Captcha 通過模塊化設(shè)計平衡了安全性與開發(fā)效率,其源碼結(jié)構(gòu)清晰(僅核心類約15個),適合二次開發(fā)。無論是快速實現(xiàn)基礎(chǔ)驗證功能,還是構(gòu)建企業(yè)級人機驗證系統(tǒng),該框架都能提供可靠支持。建議結(jié)合具體業(yè)務(wù)需求,在驗證碼樣式、驗證流程上做深度定制。
GitHub 地址:https://github.com/whvcse/EasyCaptcha
到此這篇關(guān)于SpringBoot集成圖片驗證碼框架easy-captcha的文章就介紹到這了,更多相關(guān)SpringBoot驗證碼easy-captcha內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java根據(jù)模板實現(xiàn)excel導(dǎo)出標(biāo)準(zhǔn)化
這篇文章主要為大家詳細介紹了Java如何根據(jù)模板實現(xiàn)excel導(dǎo)出標(biāo)準(zhǔn)化,文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的小伙伴可以參考下2024-03-03詳解Java的Struts框架中上傳文件和客戶端驗證的實現(xiàn)
這篇文章主要介紹了Java的Struts框架中上傳文件和客戶端驗證的實現(xiàn),Struts是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下2015-12-12springmvc+shiro自定義過濾器的實現(xiàn)代碼
這篇文章主要介紹了springmvc+shiro自定義過濾器的實現(xiàn)方法,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2018-10-10