利用Java工具類Hutool實現(xiàn)驗證碼校驗功能
第一篇是純利用現(xiàn)有JDK提供的繪圖類(ImageIO)類制作,這個過程比較復雜且需要了解ImageIO
類。
這一篇文章是利用Hutool工具類來實現(xiàn)的,該工具類已經(jīng)封裝驗證碼所需的相關類等,使用起來較為簡單和方便。
Hutool工具類介紹
Hutool是一個小而全的Java工具類庫,通過靜態(tài)方法封裝,降低相關API的學習成本,提高工作效率,使Java擁有函數(shù)式語言般的優(yōu)雅,讓Java語言也可以“甜甜的”。
Hutool中的工具方法來自每個用戶的精雕細琢,它涵蓋了Java開發(fā)底層代碼中的方方面面,它既是大型項目開發(fā)中解決小問題的利器,也是小型項目中的效率擔當;
• Web開發(fā)
• 與其它框架無耦合
• 高度可替換
Hutool官方網(wǎng)站:https://hutool.cn/
Hutool實現(xiàn)驗證碼生成
利用Hutool實現(xiàn)驗證碼校驗,校驗的Servlet與今天的第一篇是一樣的,唯一就是驗證碼的生成是不一樣的。利用Hutool生成驗證碼更快捷。
獲取Hutool:
• jar包下載:https://repo1.maven.org/maven2/cn/hutool/hutool-all/5.8.8/
• Maven:在項目的pom.xml的dependencies中加入以下內(nèi)容:
<dependency>
Maven相關可參閱:idea創(chuàng)建Maven項目
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.8</version>
</dependency>
生成驗證碼:
設置驗證碼長、寬、驗證碼字符數(shù)、干擾元素個數(shù):
LineCaptcha?lineCaptcha?=?CaptchaUtil.createLineCaptcha(100,30,4,25);
在頁面顯示驗證碼及保存驗證碼內(nèi)容到Session:
????try{ ????????lineCaptcha.write(response.getOutputStream()); ????????String?code?=?lineCaptcha.getCode();//獲取驗證碼內(nèi)容 ????????request.getSession().setAttribute("piccode",code); ????????response.getOutputStream().close(); ????????}catch?(IOException?e){ ????????????e.printStackTrace(); ????????}
這個就實現(xiàn)了驗證碼的生成,相比于第一篇自己制作簡潔了許多。
完整代碼:
ImageGenerate.java
public?class?ImageGenerate?extends?HttpServlet?{ ????public?void?doGet?(HttpServletRequest?request,HttpServletResponse?response)?{ ????????LineCaptcha?lineCaptcha?=?CaptchaUtil.createLineCaptcha(100,30,4,25); ????????response.setContentType("image/jpeg"); ????????response.setHeader("Pragma",?"No-cache"); ????????try{ ????????lineCaptcha.write(response.getOutputStream()); ????????String?code?=?lineCaptcha.getCode(); ????????request.getSession().setAttribute("piccode",code); ????????response.getOutputStream().close(); ????????}catch?(IOException?e){ ????????????e.printStackTrace(); ????????} ????} }
測試驗證碼生成
還是需要先配置web.xml文件:
?????<servlet> ????????<servlet-name>ImageGenerate</servlet-name> ????????<servlet-class>com.kailong.servlet.ImageGenerate</servlet-class> ????</servlet> ????<servlet-mapping> ????????<servlet-name>ImageGenerate</servlet-name> ????????<url-pattern>/imageGenerate</url-pattern> ????</servlet-mapping>
其他樣式的驗證碼
上面展示的驗證碼是線段干擾樣式的驗證碼,Hutool工具類還提供了其他樣式的驗證碼:
1. CircleCaptcha -圓圈干擾驗證碼
例:
//定義圖形驗證碼的長、寬、驗證碼字符數(shù)、干擾元素個數(shù) CircleCaptcha?captcha?=?CaptchaUtil.createCircleCaptcha(200,?100,?4,?20); try{ ????lineCaptcha.write(response.getOutputStream()); ????String?code?=?lineCaptcha.getCode();//獲取驗證碼內(nèi)容 ????request.getSession().setAttribute("piccode",code); ????response.getOutputStream().close(); }catch?(IOException?e){ ????e.printStackTrace(); }
2. ShearCaptcha 扭曲干擾驗證碼
例:
//定義圖形驗證碼的長、寬、驗證碼字符數(shù)、干擾線寬度 ShearCaptcha?captcha?=?CaptchaUtil.createShearCaptcha(200,?100,?4,?4); try{ ????lineCaptcha.write(response.getOutputStream()); ????String?code?=?lineCaptcha.getCode();//獲取驗證碼內(nèi)容 ????request.getSession().setAttribute("piccode",code); ????response.getOutputStream().close(); }catch?(IOException?e){ ????e.printStackTrace(); }
3. Hutool還提供了自定義驗證碼
有時候標準的驗證碼不滿足要求,比如我們希望使用純字母的驗證碼、純數(shù)字的驗證碼、加減乘除的驗證碼,此時我們就要自定義CodeGenerator
例:
//?自定義純數(shù)字的驗證碼(隨機4位數(shù)字,可重復) RandomGenerator?randomGenerator?=?new?RandomGenerator("0123456789",?4); LineCaptcha?lineCaptcha?=?CaptchaUtil.createLineCaptcha(200,?100); lineCaptcha.setGenerator(randomGenerator); //?重新生成code lineCaptcha.createCode();
ShearCaptcha?captcha?=?CaptchaUtil.createShearCaptcha(200,?45,?4,?4); //?自定義驗證碼內(nèi)容為四則運算方式 captcha.setGenerator(new?MathGenerator()); //?重新生成code captcha.createCode();
到此這篇關于利用Java工具類Hutool實現(xiàn)驗證碼校驗功能的文章就介紹到這了,更多相關Hutool生成驗證碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringCloud中使用webclient(get和post)請求微服務接口數(shù)據(jù)
在SpringCloud項目中使用WebClient調(diào)用微服務時,涉及配置WebClient、發(fā)起get和post請求等操作,如請求頭設置、服務地址配置、數(shù)據(jù)轉(zhuǎn)換處理、異常處理等,避免在循環(huán)中使用WebClient請求、路徑設置細節(jié)以及數(shù)據(jù)返回處理技巧,本文旨在幫助理解和應用WebClient進行微服務調(diào)用2024-10-10