收到請求->生成驗證碼所用的隨機(jī)數(shù)->使用隨機(jī)數(shù)寫出圖片->將隨機(jī)數(shù)記錄到Session中->輸出驗證碼
Java 驗證驗證碼的流程是:
收到請求->獲取用戶傳過來的驗證碼數(shù)字->驗證是否正確->輸出驗證結(jié)果
下面通過一個例子來展示驗證碼的生成流程,該例子使用基本Java Spring框架的Rest接口,可以使用任何平臺來獲取驗證碼:
服務(wù)器處理驗證碼的例子:
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
|
/** * 接收驗證碼請求 */ @RequestMapping (value= "captchacode" ) public void CaptchaCode(){ try { CaptchaCodeModel captchaCodeModel= new CaptchaCode().getCode(); //將驗證碼放到Session中 HttpServletRequest httpServletRequest= super .getRequest(); httpServletRequest.getSession().setAttribute( "captchacodekey" , captchaCodeModel.getCaptchaCode()); //將圖片寫到客戶端 HttpServletResponse httpServletResponse= super .getResponse(); //禁止緩存 httpServletResponse.setHeader( "Pragma" , "no-cache" ); httpServletResponse.setHeader( "Cache-Control" , "no-cache" ); httpServletResponse.setDateHeader( "Expires" , 0 ); ServletOutputStream servletOutputStream=httpServletResponse.getOutputStream(); //輸出圖片 ImageIO.write(captchaCodeModel.getCaptchaImage(), "jpeg" , servletOutputStream); servletOutputStream.close(); } catch (Exception e) { logger.info( "驗證碼生成失敗:" +e.getMessage()); } } |
2.生成驗證碼并生成圖片:
1
2
3
4
5
6
7
8
9
10
|
public class CaptchaCode { private int width = 90 ; // 定義圖片的width private int height = 20 ; // 定義圖片的height private int codeCount = 4 ; // 定義圖片上顯示驗證碼的個數(shù) private int xx = 15 ; private int fontHeight = 18 ; private int codeY = 16 ; char [] codeSequence = { 'A' , 'B' , 'C' , 'D' , 'E' , 'F' , 'G' , 'H' , 'I' , 'J' , 'K' , 'L' , 'M' , 'N' , 'O' , 'P' , 'Q' , 'R' , 'S' , 'T' , 'U' , 'V' , 'W' , 'X' , 'Y' , 'Z' , '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' }; |
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
|
public CaptchaCodeModel getCode() throws IOException { // 定義圖像buffer BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics gd = buffImg.getGraphics(); // 創(chuàng)建一個隨機(jī)數(shù)生成器類 Random random = new Random(); // 將圖像填充為白色 gd.setColor(Color.WHITE); gd.fillRect( 0 , 0 , width, height); // 創(chuàng)建字體,字體的大小應(yīng)該根據(jù)圖片的高度來定。 Font font = new Font( "Fixedsys" , Font.BOLD, fontHeight); // 設(shè)置字體。 gd.setFont(font); // 畫邊框。 gd.setColor(Color.BLACK); gd.drawRect( 0 , 0 , width - 1 , height - 1 ); // 隨機(jī)產(chǎn)生40條干擾線,使圖象中的認(rèn)證碼不易被其它程序探測到。 gd.setColor(Color.BLACK); for ( int i = 0 ; i < 40 ; i++) { int x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt( 12 ); int yl = random.nextInt( 12 ); gd.drawLine(x, y, x + xl, y + yl); } // randomCode用于保存隨機(jī)產(chǎn)生的驗證碼,以便用戶登錄后進(jìn)行驗證。 StringBuffer randomCode = new StringBuffer(); int red = 0 , green = 0 , blue = 0 ; // 隨機(jī)產(chǎn)生codeCount數(shù)字的驗證碼。 for ( int i = 0 ; i < codeCount; i++) { // 得到隨機(jī)產(chǎn)生的驗證碼數(shù)字。 String code = String.valueOf(codeSequence[random.nextInt( 36 )]); // 產(chǎn)生隨機(jī)的顏色分量來構(gòu)造顏色值,這樣輸出的每位數(shù)字的顏色值都將不同。 red = random.nextInt( 255 ); green = random.nextInt( 255 ); blue = random.nextInt( 255 ); // 用隨機(jī)產(chǎn)生的顏色將驗證碼繪制到圖像中。 gd.setColor( new Color(red, green, blue)); gd.drawString(code, (i + 1 ) * xx, codeY); // 將產(chǎn)生的四個隨機(jī)數(shù)組合在一起。 randomCode.append(code); } CaptchaCodeModel captchaCodeModel= new CaptchaCodeModel(); captchaCodeModel.setCaptchaCode(randomCode.toString()); captchaCodeModel.setCaptchaImage(buffImg); return captchaCodeModel; } public class CaptchaCodeModel{ //驗證碼的String形式 private String captchaCode; //驗證碼的圖片形式 private BufferedImage captchaImage; public String getCaptchaCode() { return captchaCode; } public void setCaptchaCode(String captchaCode) { this .captchaCode = captchaCode; } public BufferedImage getCaptchaImage() { return captchaImage; } public void setCaptchaImage(BufferedImage captchaImage) { this .captchaImage = captchaImage; } } |
3.接收用戶傳過來的驗證碼并驗證:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
/** * 驗證驗證碼 */ @RequestMapping (value = "valicatpcha" ) public void register_R() { PageData pageData = super .getPageData(); // 獲取驗證碼 String captchaCode = pageData.getString( "captchacode" ); HttpServletRequest httpServletRequest = super .getRequest(); Object codeObject = httpServletRequest.getSession().getAttribute(“captchacodekey”); // 驗證碼錯誤 if (codeObject == null || Tools.isEmptyString(captchaCode) || !String.valueOf(codeObject).toUpperCase() .equals(captchaCode.toUpperCase())) { setResult( MessageManager.getInstance().getMessage( "invalidcaptcha" ), ResultType.Error); return ; } } |
頁面請求驗證碼并驗證的例子:
-請求驗證碼:<img src='captchacode' style='height:32px;width:148px;'
-驗證驗證碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
function validcaptchacode(captchaCode) { $.ajax({ type : "POST" , url : "valicatpcha" , data : { captchacode : captchaCode, tm : new Date().getTime() }, dataType : "json" , cache : false , success : function(data) { alert(data); }, error : function(data) { alert(data); } }); } |
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持服務(wù)器之家!
原文鏈接:http://www.cnblogs.com/yujie365/p/6391371.html