隨筆-208  評論-469  文章-30  trackbacks-0
          在開發中驗證碼是比較常用到有效防止這種問題對某一個特定注冊用戶用特定程序暴力破解方式進行不斷的登陸嘗試的方式。
          此演示程序包括三個文件:
          1.index.jsp:登錄頁面
          2.image.jsp:生成驗證碼圖片頁面
          3.result.jsp:結果頁面
          【頁面顯示】
          哦哦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>標簽來顯示生成的驗證碼圖片.
          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. ????//設置頁面不緩存???????
            15. ????? response.setHeader("Pragma", "No-cache");???????
            16. ????? response.setHeader("Cache-Control", "no-cache");???????
            17. ????? response.setDateHeader("Expires", 0);???????
            18. ??????
            19. ????// 在內存中創建圖象???????
            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. ????// 設定背景色???????
            31. ????? g.setColor(getRandColor(200, 250));???????
            32. ????? g.fillRect(0, 0, width, height);???????
            33. ??????
            34. ????//設定字體???????
            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. ????// 隨機產生155條干擾線,使圖象中的認證碼不易被其它程序探測到???????
            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. ????// 取隨機產生的認證碼(4位數字)???????
            52. ????? String sRand = "";???????
            53. ????for (int i = 0; i < 4; i++) {???????
            54. ????????? String rand = String.valueOf(random.nextInt(10));???????
            55. ????????? sRand += rand;???????
            56. ????????// 將認證碼顯示到圖象中???????
            57. ????????? g.setColor(new Color(20 + random.nextInt(110), 20 + random???????
            58. ????????? .nextInt(110), 20 + random.nextInt(110)));//調用函數出來的顏色相同,可能是因為種子太接近,所以只能直接生成???????
            59. ????????? g.drawString(rand, 13 * i + 6, 16);???????
            60. ????? }???????
            61. ??????
            62. ????// 將認證碼存入SESSION???????
            63. ????? session.setAttribute("code", sRand);???????
            64. ??????
            65. ????// 圖象生效???????
            66. ????? g.dispose();???????
            67. ??????
            68. ????// 輸出圖象到頁面???????
            69. ????? ImageIO.write(image, "JPEG", response.getOutputStream());???????
            70. %>??
            ??
          [注意]:
          (1)contentType值設置為"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 閱讀(353) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 额尔古纳市| 忻州市| 贡嘎县| 延安市| 滕州市| 吕梁市| 宜昌市| 桃源县| 达州市| 信丰县| 九龙坡区| 正安县| 张家界市| 百色市| 咸丰县| 北辰区| 马关县| 龙游县| 边坝县| 南丰县| 黔南| 乃东县| 都兰县| 巫溪县| 枣强县| 乳山市| 井陉县| 广昌县| 合阳县| 古交市| 微山县| 景德镇市| 当雄县| 安多县| 工布江达县| 阿拉善右旗| 丰城市| 中方县| 台山市| 霞浦县| 永清县|