前言
本文介紹的imagecode方法是一個生成圖形驗證碼的請求,checkcode方法實現了對這個圖形驗證碼的驗證。從驗證碼的生成到驗證的過程中,驗證碼是通過Session來保存的,并且設定一個驗證碼的最長有效時間為5分鐘。驗證碼的生成規則是從0~9的數字中,隨機產生一個4位數,并增加一些干擾元素,最終組合成為一個圖形輸出
1、驗證碼生成類
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
import java.awt.*; import java.awt.image.BufferedImage; import java.io.OutputStream; import java.util.HashMap; import java.util.Map; import java.util.Random; public class ImageCode { private static char mapTable[] = { '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' }; public static Map<String, Object> getImageCode( int width, int height, OutputStream os) { Map<String,Object> returnMap = new HashMap<String, Object>(); if (width <= 0 ) width = 60 ; if (height <= 0 ) height = 20 ; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 獲取圖形上下文 Graphics g = image.getGraphics(); //生成隨機類 Random random = new Random(); // 設定背景色 g.setColor(getRandColor( 200 , 250 )); g.fillRect( 0 , 0 , width, height); //設定字體 g.setFont( new Font( "Times New Roman" , Font.PLAIN, 18 )); // 隨機產生168條干擾線,使圖象中的認證碼不易被其它程序探測到 g.setColor(getRandColor( 160 , 200 )); for ( int i = 0 ; i < 168 ; i++) { int x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt( 12 ); int yl = random.nextInt( 12 ); g.drawLine(x, y, x + xl, y + yl); } //取隨機產生的碼 String strEnsure = "" ; //4代表4位驗證碼,如果要生成更多位的認證碼,則加大數值 for ( int i = 0 ; i < 4 ; ++i) { strEnsure += mapTable[( int ) (mapTable.length * Math.random())]; // 將認證碼顯示到圖象中 g.setColor( new Color( 20 + random.nextInt( 110 ), 20 + random.nextInt( 110 ), 20 + random.nextInt( 110 ))); //直接生成 String str = strEnsure.substring(i, i + 1 ); g.drawString(str, 13 * i + 6 , 16 ); } // 釋放圖形上下文 g.dispose(); returnMap.put( "image" ,image); returnMap.put( "strEnsure" ,strEnsure); return returnMap; } //給定范圍獲得隨機顏色 static Color getRandColor( int fc, int bc) { Random random = new Random(); if (fc > 255 ) fc = 255 ; if (bc > 255 ) bc = 255 ; int r = fc + random.nextInt(bc - fc); int g = fc + random.nextInt(bc - fc); int b = fc + random.nextInt(bc - fc); return new Color(r, g, b); } } |
2、獲取驗證碼API
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@RequestMapping (value = "/images/imagecode" ) public String imagecode(HttpServletRequest request, HttpServletResponse response) throws Exception { OutputStream os = response.getOutputStream(); Map<String,Object> map = ImageCode.getImageCode( 60 , 20 , os); String simpleCaptcha = "simpleCaptcha" ; request.getSession().setAttribute(simpleCaptcha, map.get( "strEnsure" ).toString().toLowerCase()); request.getSession().setAttribute( "codeTime" , new Date().getTime()); try { ImageIO.write((BufferedImage) map.get( "image" ), "JPEG" , os); } catch (IOException e) { return "" ; } return null ; } |
3、驗證驗證碼API
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
@RequestMapping (value = "/checkcode" ) @ResponseBody public String checkcode(HttpServletRequest request, HttpSession session) throws Exception { String checkCode = request.getParameter( "checkCode" ); Object cko = session.getAttribute( "simpleCaptcha" ) ; //驗證碼對象 if (cko == null ){ request.setAttribute( "errorMsg" , "驗證碼已失效,請重新輸入!" ); return "驗證碼已失效,請重新輸入!" ; } String captcha = cko.toString(); Date now = new Date(); Long codeTime = Long.valueOf(session.getAttribute( "codeTime" )+ "" ); if (StringUtils.isEmpty(checkCode) || captcha == null || !(checkCode.equalsIgnoreCase(captcha))) { request.setAttribute( "errorMsg" , "驗證碼錯誤!" ); return "驗證碼錯誤!" ; } else if ((now.getTime()-codeTime)/ 1000 / 60 > 5 ) { //驗證碼有效時長為5分鐘 request.setAttribute( "errorMsg" , "驗證碼已失效,請重新輸入!" ); return "驗證碼已失效,請重新輸入!" ; } else { session.removeAttribute( "simpleCaptcha" ); return "1" ; } } |
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:http://www.jianshu.com/p/29f005992527