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

          【頁面代碼】
          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屬性來限制輸入字符;
          (2)使用<img>標(biāo)簽來顯示生成的驗證碼圖片.
          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) {//給定范圍獲得隨機顏色???????
            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è)置頁面不緩存???????
            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. ????//生成隨機類???????
            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. ????//畫邊框???????
            38. ????//g.setColor(new Color());???????
            39. ????//g.drawRect(0,0,width-1,height-1);???????
            40. ??????
            41. ????// 隨機產(chǎn)生155條干擾線,使圖象中的認(rèn)證碼不易被其它程序探測到???????
            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. ????// 取隨機產(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ù)出來的顏色相同,可能是因為種子太接近,所以只能直接生成???????
            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. ????// 輸出圖象到頁面???????
            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("驗證成功!");???
          8. ????? }else{???
          9. ????????? out.println("驗證失敗!");???
          10. ????? }???
          11. %>??
          12. </body></html>??
          posted on 2007-04-18 23:38 EricWong 閱讀(357) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 分宜县| 定日县| 鄂托克旗| 昌图县| 绥芬河市| 厦门市| 河曲县| 板桥市| 沐川县| 额尔古纳市| 贺兰县| 景宁| 金门县| 东乡| 旅游| 吕梁市| 樟树市| 乌兰浩特市| 宜宾县| 鄢陵县| 宜丰县| 乐清市| 南靖县| 陇南市| 新乡县| 万年县| 丰宁| 比如县| 泗水县| 右玉县| 高台县| 塔河县| 长沙县| 舟山市| 高唐县| 财经| 莒南县| 个旧市| 湘西| 九龙城区| 长沙市|