很多網(wǎng)站都有該實(shí)現(xiàn)
作用:
為了提高系統(tǒng)的安全性
有了驗(yàn)證碼,我們就可以要求用戶在輸入用戶名,密碼等信息后,同時(shí)輸入圖片上的文字,用戶提交后,系統(tǒng)會(huì)首先從session中提取剛剛生成的驗(yàn)證碼,并和用戶輸入的驗(yàn)證碼進(jìn)行比較,如果比較相等,表示用戶是從登錄界面登錄過(guò)來(lái)的,否則,表示用戶是非法的,我們使用驗(yàn)證碼,是確保系統(tǒng)的使用必須要進(jìn)行登錄成功之后,才能使用,避免用戶直接在地址欄中輸入要訪問(wèn)的頁(yè)面。
也就是說(shuō),使用驗(yàn)證碼,就強(qiáng)制用戶用戶必須先從登錄界面登錄
二. 驗(yàn)證實(shí)現(xiàn)方式
用到兩個(gè)關(guān)鍵類(lèi),這兩個(gè)類(lèi)跟圖片的輸出是有關(guān)系的
1
2
3
4
5
|
BufferedImage im = new BufferedImage( 60 , 20 ,BufferedImage.TYPE_INT_RGB); //第一個(gè)參數(shù)im表示一個(gè)圖片對(duì)象 //JPG表示圖片輸出類(lèi)型 //response.getOutputStream()代表一個(gè)響應(yīng)的輸出流,也就是說(shuō),你訪問(wèn)這個(gè)servlet.該servlet就會(huì)圖片顯示給你 ImageIO.write(im, "JPG" ,response.getOutputStream()); |
三. 實(shí)現(xiàn)步驟
1.使用BufferedImage產(chǎn)生一個(gè)圖片,然后使用ImageIO輸出,并指定為JPG格式
1
2
3
4
5
|
BufferedImage im = new BufferedImage( 60 , 20 ,BufferedImage.TYPE_INT_RGB); //第一個(gè)參數(shù)im表示一個(gè)圖片對(duì)象 //JPG表示圖片輸出類(lèi)型 //response.getOutputStream()代表一個(gè)響應(yīng)的輸出流,也就是說(shuō),你訪問(wèn)這個(gè)servlet.該servlet就會(huì)圖片顯示給你 ImageIO.write(im, "JPG" ,response.getOutputStream()); |
2.獲取圖片繪圖對(duì)象
Graphics g = im.getGraphics();
3.填充繪圖區(qū)域
1
2
3
4
5
|
Random rm = new Random(); Color c = new Color(rm.nextInt(255),rm.nextInt(255),rm.nextInt(255)); g.setColor(c); //填充整個(gè)圖片的顏色 g.fillRect(0, 0, 60, 20); |
4.向圖片中輸出數(shù)字
1
2
3
|
g.setColor( new Color(rm.nextInt( 255 ),rm.nextInt( 255 ),rm.nextInt( 255 ))); g.setFont( new Font( "華文隸書(shū)" ,Font.BOLD|Font.ITALIC, 28 )); g.drawString( "8" , 1 , 18 ); |
5.隨機(jī)4位數(shù)字
1
2
3
4
5
6
|
//隨機(jī)產(chǎn)生4位數(shù)字 for ( int i= 0 ;i< 4 ;i++){ g.setColor( new Color(rm.nextInt( 255 ),rm.nextInt( 255 ),rm.nextInt( 255 ))); g.setFont( new Font( "Gungsuh" ,Font.BOLD|Font.ITALIC, 22 )); g.drawString( "" +rm.nextInt( 10 ), (i* 15 )+ 2 , 18 ); } |
6.隨機(jī)產(chǎn)生中文
1
2
3
4
5
6
|
String str = "胸有激雷而面如平湖者可拜上將軍" ; for ( int i= 0 ;i< 4 ;i++){ g.setColor( new Color(rm.nextInt( 255 ),rm.nextInt( 255 ),rm.nextInt( 255 ))); g.setFont( new Font( "Gungsuh" ,Font.BOLD|Font.ITALIC, 15 )); g.drawString( "" +str.charAt(rm.nextInt(str.length())), (i* 15 )+ 2 , 18 ); } |
7.在頁(yè)面中如何來(lái)引入該驗(yàn)證碼:
- <img alt="驗(yàn)證碼" src="/ImageServlet">
8.保存數(shù)字,以便進(jìn)行登錄比較
1
2
|
//將得到的四個(gè)數(shù)字保存到session中,以便當(dāng)用戶登錄的時(shí)候,用來(lái)比較 request.getSession().setAttribute( "piccode" , sbf.toString()); |
9.登錄驗(yàn)證
首先,需要驗(yàn)證該用戶在數(shù)據(jù)庫(kù)中是否存在,如果存在,還需要驗(yàn)證輸入的驗(yàn)證碼是否一致.
驗(yàn)證成功后,需要轉(zhuǎn)發(fā)到相關(guān)的操作頁(yè)面.
代碼實(shí)例:
1
2
3
4
5
6
7
8
9
10
11
12
|
boolean b_exist = login.validate(username,passwd); //如果該用戶存在 if (b_exist){ String pic = "" +request.getSession().getAttribute( "piccode" ); //比較驗(yàn)證碼 if (!pic.equals( "" ) && pic.equals(code)){ //向session中存入用戶信息,以供其他中來(lái)使用 request.getSession().setAttribute( "username" , username); response.sendRedirect( "index.jsp" ); } } |
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持服務(wù)器之家!
原文鏈接:http://www.cnblogs.com/liu321kai/p/6257131.html