利用Java工具類Hutool實現(xiàn)驗證碼校驗功能
第一篇是純利用現(xiàn)有JDK提供的繪圖類(ImageIO)類制作,這個過程比較復(fù)雜且需要了解ImageIO類。
這一篇文章是利用Hutool工具類來實現(xiàn)的,該工具類已經(jīng)封裝驗證碼所需的相關(guān)類等,使用起來較為簡單和方便。
Hutool工具類介紹

Hutool是一個小而全的Java工具類庫,通過靜態(tài)方法封裝,降低相關(guān)API的學(xué)習(xí)成本,提高工作效率,使Java擁有函數(shù)式語言般的優(yōu)雅,讓Java語言也可以“甜甜的”。
Hutool中的工具方法來自每個用戶的精雕細琢,它涵蓋了Java開發(fā)底層代碼中的方方面面,它既是大型項目開發(fā)中解決小問題的利器,也是小型項目中的效率擔(dān)當(dāng);
• 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相關(guān)可參閱:idea創(chuàng)建Maven項目
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.8</version>
</dependency>
生成驗證碼:
設(shè)置驗證碼長、寬、驗證碼字符數(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還提供了自定義驗證碼
有時候標(biāo)準(zhǔn)的驗證碼不滿足要求,比如我們希望使用純字母的驗證碼、純數(shù)字的驗證碼、加減乘除的驗證碼,此時我們就要自定義CodeGenerator
例:
//?自定義純數(shù)字的驗證碼(隨機4位數(shù)字,可重復(fù))
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();
到此這篇關(guān)于利用Java工具類Hutool實現(xiàn)驗證碼校驗功能的文章就介紹到這了,更多相關(guān)Hutool生成驗證碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
GC算法實現(xiàn)篇之并發(fā)標(biāo)記清除
這篇文章主要為大家介紹了GC算法實現(xiàn)篇之并發(fā)-標(biāo)記-清除,?CMS垃圾收集器在減少停頓時間上做了很多給力的工作,?大量的并發(fā)線程執(zhí)行的工作并不需要暫停應(yīng)用線程2022-01-01
MyBatis復(fù)雜Sql查詢實現(xiàn)示例介紹
在利用mybatis做查詢的時候,一般返回結(jié)果用resulttype,這種情況必須是查詢的結(jié)果在對應(yīng) 的pojo類中有對應(yīng)的,一般都是單表查詢,但是對于一些復(fù)雜的情況,比如需要用到多表查詢的時候,resultType不再適用,此時一般用resultMap來表示返回的結(jié)果2022-12-12
SpringCloud中使用webclient(get和post)請求微服務(wù)接口數(shù)據(jù)
在SpringCloud項目中使用WebClient調(diào)用微服務(wù)時,涉及配置WebClient、發(fā)起get和post請求等操作,如請求頭設(shè)置、服務(wù)地址配置、數(shù)據(jù)轉(zhuǎn)換處理、異常處理等,避免在循環(huán)中使用WebClient請求、路徑設(shè)置細節(jié)以及數(shù)據(jù)返回處理技巧,本文旨在幫助理解和應(yīng)用WebClient進行微服務(wù)調(diào)用2024-10-10

