總體思路,簡單講,就是后臺生成圖片同時將圖片信息保存在session,前端顯示圖片,輸入驗證碼信息后提交表單到后臺,取出存放在session里的驗證碼信息,與表單提交的驗證碼信息核對。
點(diǎn)擊驗證碼圖片時,通過jquery重新請求后臺生成驗證碼圖片方法,更換圖片。
首先在后端controller里,有這樣一個方法:
路徑為http://localhost:8888/RiXiang_blog/login/captcha.form,訪問這個路徑便可以通過response寫入圖片。
1
2
3
4
5
6
7
|
@RequestMapping (value = "/captcha" , method = RequestMethod.GET) @ResponseBody public void captcha(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { CaptchaUtil.outputCaptcha(request, response); } |
CaptchaUtil是一個工具類,封裝了驗證碼圖片生成,和存儲session功能。
代碼如下:
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
package com.util; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.Random; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; /** * @ClassName: CaptchaUtil * @Description: 關(guān)于驗證碼的工具類 * @author 無名 * @date 2016-5-7 上午8:33:08 * @version 1.0 */ public final class CaptchaUtil { private CaptchaUtil(){} /* * 隨機(jī)字符字典 */ private static final char[] CHARS = { '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; /* * 隨機(jī)數(shù) */ private static Random random = new Random(); /* * 獲取6位隨機(jī)數(shù) */ private static String getRandomString() { StringBuffer buffer = new StringBuffer(); for(int i = 0; i < 6; i++) { buffer.append(CHARS[random.nextInt(CHARS.length)]); } return buffer.toString(); } /* * 獲取隨機(jī)數(shù)顏色 */ private static Color getRandomColor() { return new Color(random.nextInt(255),random.nextInt(255), random.nextInt(255)); } /* * 返回某顏色的反色 */ private static Color getReverseColor(Color c) { return new Color( 255 - c.getRed(), 255 - c.getGreen(), 255 - c.getBlue()); } public static void outputCaptcha(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType( "image/jpeg" ); String randomString = getRandomString(); request.getSession( true ).setAttribute( "randomString" , randomString); int width = 100 ; int height = 30 ; Color color = getRandomColor(); Color reverse = getReverseColor(color); BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = bi.createGraphics(); g.setFont( new Font(Font.SANS_SERIF, Font.BOLD, 16 )); g.setColor(color); g.fillRect( 0 , 0 , width, height); g.setColor(reverse); g.drawString(randomString, 18 , 20 ); for ( int i = 0 , n = random.nextInt( 100 ); i < n; i++) { g.drawRect(random.nextInt(width), random.nextInt(height), 1 , 1 ); } // 轉(zhuǎn)成JPEG格式 ServletOutputStream out = response.getOutputStream(); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(bi); out.flush(); } } |
前端獲取驗證碼圖片,要這樣寫:
1
2
3
4
5
6
7
8
9
|
…… < tr > < th >captcha</ th > < td > < input type = "text" id = "captcha" name = "captcha" class = "text" maxlength = "10" /> < img id = "captchaImage" src = "captcha.form" /> </ td > </ tr > |
img的src里寫入路徑,頁面加載時就會訪問http://localhost:8888/RiXiang_blog/login/captcha.form獲取圖片。
表單的提交和登錄信息驗證就不具體講了。
點(diǎn)擊更換驗證碼的js代碼如下:
1
2
3
4
5
|
// 更換驗證碼 $( '#captchaImage' ).click( function () { $( '#captchaImage' ).attr( "src" , "captcha.form?timestamp=" + ( new Date()).valueOf()); }); |
可以看到后面加入時間戳作為參數(shù),timestamp=" + (new Date()).valueOf()。加入這個參數(shù)就可以實(shí)現(xiàn)重新訪問后臺方法。否則是無法刷新圖像的。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。