前言
相對來說呢,jpg格式的相對來說容易破解一點,當然也取決于你的干擾元素,元素越復雜,破解也就難度越高,有的加的多,人都識別不出來了,何況人呢。都是概率問題。
GIF格式 + 干擾元素,那么驗證碼破解難度又上了一個層次
上代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
/** * 獲取驗證碼(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格式動畫驗證碼 * 寬,高,位數。 */ 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格式驗證碼方式。
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
/** * 獲取驗證碼(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格式驗證碼 * 寬,高,位數。 */ 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()); } } |
有興趣的朋友可以下載源碼看看。
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。