<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-208  評(píng)論-469  文章-30  trackbacks-0
    在開(kāi)發(fā)中驗(yàn)證碼是比較常用到有效防止這種問(wèn)題對(duì)某一個(gè)特定注冊(cè)用戶(hù)用特定程序暴力破解方式進(jìn)行不斷的登陸嘗試的方式。
    此演示程序包括三個(gè)文件:
    1.index.jsp:登錄頁(yè)面
    2.image.jsp:生成驗(yàn)證碼圖片頁(yè)面
    3.result.jsp:結(jié)果頁(yè)面
    【頁(yè)面顯示】
    哦哦D74ED2}0.jpg

    【頁(yè)面代碼】
    1.index.jsp
    html代碼
    1. <html><body>??????
    2. <formmethod=postaction="result.jsp">??????
    3. <inputtype=textname=inputmaxlength=4>??????
    4. <imgborder=0src="image.jsp">??????
    5. <inputtype="submit"value="submit">??????
    6. </form></body></html>??
    [注意]:
    (1)使用maxlength屬性來(lái)限制輸入字符;
    (2)使用<img>標(biāo)簽來(lái)顯示生成的驗(yàn)證碼圖片.
    2.image.jsp
      1. <%@ page contentType="image/JPEG"??????
      2. ????import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"??????
      3. ????? pageEncoding="GBK"%><%!Color getRandColor(int fc, int bc) {//給定范圍獲得隨機(jī)顏色???????
      4. ????????? Random random = new Random();???????
      5. ????????if (fc > 255)???????
      6. ????????????? fc = 255;???????
      7. ????????if (bc > 255)???????
      8. ????????????? bc = 255;???????
      9. ????????int r = fc + random.nextInt(bc - fc);???????
      10. ????????int g = fc + random.nextInt(bc - fc);???????
      11. ????????int b = fc + random.nextInt(bc - fc);???????
      12. ????????returnnew Color(r, g, b);???????
      13. ????? }%><%???????
      14. ????//設(shè)置頁(yè)面不緩存???????
      15. ????? response.setHeader("Pragma", "No-cache");???????
      16. ????? response.setHeader("Cache-Control", "no-cache");???????
      17. ????? response.setDateHeader("Expires", 0);???????
      18. ??????
      19. ????// 在內(nèi)存中創(chuàng)建圖象???????
      20. ????int width = 60, height = 20;???????
      21. ????? BufferedImage image = new BufferedImage(width, height,???????
      22. ????????????? BufferedImage.TYPE_INT_RGB);???????
      23. ??????
      24. ????// 獲取圖形上下文???????
      25. ????? Graphics g = image.getGraphics();???????
      26. ??????
      27. ????//生成隨機(jī)類(lèi)???????
      28. ????? Random random = new Random();???????
      29. ??????
      30. ????// 設(shè)定背景色???????
      31. ????? g.setColor(getRandColor(200, 250));???????
      32. ????? g.fillRect(0, 0, width, height);???????
      33. ??????
      34. ????//設(shè)定字體???????
      35. ????? g.setFont(new Font("Times New Roman", Font.PLAIN, 18));???????
      36. ??????
      37. ????//畫(huà)邊框???????
      38. ????//g.setColor(new Color());???????
      39. ????//g.drawRect(0,0,width-1,height-1);???????
      40. ??????
      41. ????// 隨機(jī)產(chǎn)生155條干擾線(xiàn),使圖象中的認(rèn)證碼不易被其它程序探測(cè)到???????
      42. ????? g.setColor(getRandColor(160, 200));???????
      43. ????for (int i = 0; i < 100; i++) {???????
      44. ????????int x = random.nextInt(width);???????
      45. ????????int y = random.nextInt(height);???????
      46. ????????int xl = random.nextInt(12);???????
      47. ????????int yl = random.nextInt(12);???????
      48. ????????? g.drawLine(x, y, x + xl, y + yl);???????
      49. ????? }???????
      50. ??????
      51. ????// 取隨機(jī)產(chǎn)生的認(rèn)證碼(4位數(shù)字)???????
      52. ????? String sRand = "";???????
      53. ????for (int i = 0; i < 4; i++) {???????
      54. ????????? String rand = String.valueOf(random.nextInt(10));???????
      55. ????????? sRand += rand;???????
      56. ????????// 將認(rèn)證碼顯示到圖象中???????
      57. ????????? g.setColor(new Color(20 + random.nextInt(110), 20 + random???????
      58. ????????? .nextInt(110), 20 + random.nextInt(110)));//調(diào)用函數(shù)出來(lái)的顏色相同,可能是因?yàn)榉N子太接近,所以只能直接生成???????
      59. ????????? g.drawString(rand, 13 * i + 6, 16);???????
      60. ????? }???????
      61. ??????
      62. ????// 將認(rèn)證碼存入SESSION???????
      63. ????? session.setAttribute("code", sRand);???????
      64. ??????
      65. ????// 圖象生效???????
      66. ????? g.dispose();???????
      67. ??????
      68. ????// 輸出圖象到頁(yè)面???????
      69. ????? ImageIO.write(image, "JPEG", response.getOutputStream());???????
      70. %>??
      ??
    [注意]:
    (1)contentType值設(shè)置為"image/JPEG"
    3.result.jsp
    1. <%@ page language="java"import="java.util.*"pageEncoding="GBK"%>??
    2. <html><body>??
    3. <%???
    4. ????? String input=request.getParameter("input");???
    5. ????? String code=(String)session.getAttribute("code");???????
    6. ????? if(input.equals(code)){???
    7. ????????? out.println("驗(yàn)證成功!");???
    8. ????? }else{???
    9. ????????? out.println("驗(yàn)證失敗!");???
    10. ????? }???
    11. %>??
    12. </body></html>??
    posted on 2007-04-18 23:38 EricWong 閱讀(353) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): Java
    主站蜘蛛池模板: 亚洲日韩在线观看| 一区二区免费在线观看| 浮力影院亚洲国产第一页| 亚洲狠狠ady亚洲精品大秀| 日韩精品亚洲专区在线观看| fc2成年免费共享视频网站| 亚洲Av高清一区二区三区| 亚洲成AV人片一区二区密柚| 免费一级做a爰片性色毛片| 国产v精品成人免费视频400条| 精品国产污污免费网站入口在线 | 亚洲一区二区三区四区在线观看| 国产免费看插插插视频| 1024免费福利永久观看网站| 99精品免费视品| 日本高清免费中文在线看| 亚洲永久网址在线观看| 亚洲手机中文字幕| 久久精品国产亚洲AV麻豆~| 亚洲欧洲一区二区三区| 国产最新凸凹视频免费| 欧洲黑大粗无码免费| 久久精品国产免费观看| 无码日韩精品一区二区免费暖暖| 久草免费福利在线| 一级免费黄色大片| 免费人成在线观看播放a| 在线视频亚洲一区| 免费A级毛片在线播放不收费| 无码人妻精品一二三区免费| 久久国产免费福利永久| 亚洲一区二区免费视频| 精品免费久久久久久久| 精品国产无限资源免费观看| 美女内射毛片在线看免费人动物| 久久精品成人免费观看| 99久久久国产精品免费牛牛| 最近中文字幕无免费| 人成午夜免费视频在线观看| 国产卡一卡二卡三免费入口| 最近的中文字幕大全免费版|