您好,登錄后才能下訂單哦!
這篇文章主要講解了“怎么使用java搞定網(wǎng)站登錄驗證碼”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“怎么使用java搞定網(wǎng)站登錄驗證碼”吧!
本效果是利用easy-captcha工具包實現(xiàn),首先需要添加相關(guān)依賴到pom.xml中,代碼如下:
<dependency> <groupId>com.github.whvcse</groupId> <artifactId>easy-captcha</artifactId> <version>1.6.2</version> </dependency>
easy-captcha驗證碼工具支持GIF、中文、算術(shù)等類型,分別通過下面幾個實例對象實現(xiàn):
SpecCaptcha(PNG類型的靜態(tài)圖片驗證碼)
GifCaptcha(Gif類型的圖片驗證碼)
ChineseCaptcha(GIF類型中文圖片驗證碼)
ArithmeticCaptcha(算術(shù)類型的圖片驗證碼)
字符類型分為以下幾種:
TYPE_DEFAULT:數(shù)字和字母混合
TYPEONLYNUMBER:純數(shù)字
TYPEONLYCHAR:純字母
TYPEONLYUPPER:純大寫字母
TYPEONLYLOWER:純小寫字母
TYPENUMAND_UPPER:數(shù)字和大寫字母混合
package com.yanx.controller; import com.wf.captcha.SpecCaptcha; import com.wf.captcha.base.Captcha; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.thymeleaf.util.StringUtils; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @Controller public class KapchaController { @GetMapping("/kaptcha") public void defaultKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException { httpServletResponse.setHeader("Cache-Control","no-store"); httpServletResponse.setHeader("Pragma","no-cache"); httpServletResponse.setDateHeader("Expires",0); httpServletResponse.setContentType("image/gif"); //三個參數(shù)分別為寬、高、位數(shù) SpecCaptcha captcha=new SpecCaptcha(75,30,4); //設(shè)置類型為數(shù)字和字母混合 captcha.setCharType(Captcha.TYPE_DEFAULT); //設(shè)置字體 captcha.setCharType(Captcha.FONT_9); //驗證碼存入session httpServletRequest.getSession().setAttribute("verifyCode",captcha.text().toLowerCase()); //輸出圖片流 captcha.out(httpServletResponse.getOutputStream()); } }
這里控制器新增了defaultKaptcha()方法,該方法所攔截處理的路徑為/kaptcha
在static目錄中新建kaptcha.html頁面,代碼如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>驗證碼</title> </head> <body> <img src="/kaptcha" onclick="this.src='/kaptcha?t=new Date()'"> </body> </html>
訪問后端驗證碼路徑/kaptcha,驗證碼為圖片形式。onclick方法為點擊該標(biāo)簽時可以動態(tài)切換顯示驗證碼。
啟動Spring Boot項目,打開瀏覽器輸入地址:
http://localhost:8080/kaptcha.html
效果如下:
package com.yanx.controller; import com.wf.captcha.SpecCaptcha; import com.wf.captcha.base.Captcha; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.thymeleaf.util.StringUtils; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; @Controller public class KapchaController { @GetMapping("/kaptcha") public void defaultKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException { httpServletResponse.setHeader("Cache-Control","no-store"); httpServletResponse.setHeader("Pragma","no-cache"); httpServletResponse.setDateHeader("Expires",0); httpServletResponse.setContentType("image/gif"); //三個參數(shù)分別為寬、高、位數(shù) SpecCaptcha captcha=new SpecCaptcha(75,30,4); //設(shè)置類型為數(shù)字和字母混合 captcha.setCharType(Captcha.TYPE_DEFAULT); //設(shè)置字體 captcha.setCharType(Captcha.FONT_9); //驗證碼存入session httpServletRequest.getSession().setAttribute("verifyCode",captcha.text().toLowerCase()); //輸出圖片流 captcha.out(httpServletResponse.getOutputStream()); } @GetMapping("/verify") @ResponseBody public String verify(@RequestParam("code") String code, HttpSession session){ if(StringUtils.isEmpty(code)){ return "驗證碼不能為空"; } String kapchaCode = session.getAttribute("verifyCode")+""; if(StringUtils.isEmpty(kapchaCode)||!code.toLowerCase().equals(kapchaCode)){ return "驗證碼輸入錯誤"; } return "驗證成功"; } }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>驗證碼驗證</title> </head> <body> <img src="/kaptcha" onclick="this.src='/kaptcha?d=new Date()'"> <input type="text" maxlength="5" id="code" placeholder="請輸入驗證碼"/> <button id="verify">驗證</button> <p id="verifyResult"></p> </body> <script src="https://s3.pstatp.com/cdn/expire-1-M/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript" > $(function(){ //驗證按鈕點擊事件 $('#verify').click(function(){ var code=$('#code').val(); $.ajax({ type:'GET',//方法類型 url:'/verify?code='+code, success:function(result){ $('#verifyResult').html(result); }, error:function(){ alert('請求失敗'); }, }); }); }); </script> </html>
感謝各位的閱讀,以上就是“怎么使用java搞定網(wǎng)站登錄驗證碼”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對怎么使用java搞定網(wǎng)站登錄驗證碼這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。