Java生成動態(tài)版驗證碼的方法實例
前言
相對來說呢,jpg格式的相對來說容易破解一點,當(dāng)然也取決于你的干擾元素,元素越復(fù)雜,破解也就難度越高,有的加的多,人都識別不出來了,何況人呢。都是概率問題。
GIF格式 + 干擾元素,那么驗證碼破解難度又上了一個層次
上代碼:
/** * 獲取驗證碼(Gif版本) * @param response */ @RequestMapping(value="getGifCode",method=RequestMethod.GET) public void getGifCode(HttpServletResponse response,HttpServletRequest request){ try { response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); response.setContentType("image/gif"); /** * gif格式動畫驗證碼 * 寬,高,位數(shù)。 */ Captcha captcha = new GifCaptcha(146,33,4); //輸出 captcha.out(response.getOutputStream()); HttpSession session = request.getSession(true); //存入Session session.setAttribute("_code",captcha.text().toLowerCase()); } catch (Exception e) { LoggerUtils.fmtError(getClass(),e, "獲取驗證碼異常:%s",e.getMessage()); } }
使用挺簡單的,但是用了其他人封裝的工具類。下面會提供下載鏈接的。
這些個工具類,還提供了這個氣泡版本的jpg格式驗證碼方式。
代碼如下:
/** * 獲取驗證碼(jpg版本) * @param response */ @RequestMapping(value="getJPGCode",method=RequestMethod.GET) public void getJPGCode(HttpServletResponse response,HttpServletRequest request){ try { response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); response.setContentType("image/jpg"); /** * jgp格式驗證碼 * 寬,高,位數(shù)。 */ Captcha captcha = new SpecCaptcha(146,33,4); //輸出 captcha.out(response.getOutputStream()); HttpSession session = request.getSession(true); //存入Session session.setAttribute("_code",captcha.text().toLowerCase()); } catch (Exception e) { LoggerUtils.fmtError(getClass(),e, "獲取驗證碼異常:%s",e.getMessage()); } }
有興趣的朋友可以下載源碼看看。
vcode-sojson.com(jb51.net).rar
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
IDEA 中 30 秒創(chuàng)建一個 Spring Cloud Alibaba 工程
這篇文章主要介紹了IDEA 中 30 秒生成 Spring Cloud Alibaba 工程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì)對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04SpringBoot+Redis實現(xiàn)后端接口防重復(fù)提交校驗的示例
本文將結(jié)合實例代碼,介紹SpringBoot+Redis實現(xiàn)后端接口防重復(fù)提交校驗的示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-06-06