隨筆 - 3  文章 - 1  trackbacks - 0
          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(2)

          隨筆檔案

          文章分類

          文章檔案

          搜索

          •  

          最新評論

          • 1.?re: 不好的項目演示
          • 雖然項目演示失務,但我相信你的項目是最成功的,因為你的態度,你的聰明.

            You are NO.1! my lover
          • --roy117

          閱讀排行榜

          評論排行榜

          當前web應用越來越注重安全性的問題,所以再web應用項目實施中,用到了驗證碼的機制,其目的就是建立防刷機制,限制用戶惡意提交頁面,保護系統.

          現在網絡上各個網站上都有驗證碼,但是實現的形形色色各不相同.通過查資料和摸索,使用servlet來實現驗證碼.

          一 建一個驗證碼生成類RandomImageGenerator ,其功能就是實現驗證碼

          該類實現如下

          import java.awt.*;
          import java.awt.image.*;
          import java.io.FileOutputStream;
          import java.io.IOException;
          import java.io.OutputStream;
          import javax.imageio.ImageIO;
          import org.apache.commons.lang.RandomStringUtils;

          /**
           * 隨即圖片生成器
           * 該類用于用戶注冊時候需要用戶根據圖片內容進行填寫正確后方可注冊
           * @author Liudong
           */
          public class RandomImageGenerator {

          //隨即生成包含驗證碼的字符串   

          public static String random() {
                  //20060320 add by wyx
                  //因為o和0,l和1很難區分,所以,去掉大小寫的o和l

                  String str = "";
                  str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz";//初始化種子
                  return RandomStringUtils.random(6, str);//返回6為的字符串
                      }

              /**
               * 根據要求的數字生成圖片,背景為白色,字體大小16,字體顏色黑色粗體
               * @param num 要生成的數字
               * @param out 輸出流
               * @throws IOException
               */
              public static void render(String num, OutputStream out) throws IOException {
                  if (num.getBytes().length > 6) {
                      throw new IllegalArgumentException(
                              "The length of param num cannot exceed 6.");
                  }
                  //設定寬度和高度
                  int width = 130; 

                  int height = 30; 
                  // 在內存中創建圖象
                  BufferedImage bi = new BufferedImage(width, height,
                                                       BufferedImage.TYPE_INT_RGB);
                  // 獲取圖形上下文
                  Graphics2D g = (Graphics2D) bi.getGraphics();
                  //畫邊框
                  java.util.Random random = new java.util.Random();
                  g.setColor(Color.white);
                  g.fillRect(0, 0, width, height);
                  //設置字體
                  Font mFont = new Font("Tahoma", Font.BOLD, 16);
                  g.setFont(mFont);
                  g.setColor(Color.BLACK);//設置字體顏色
                  //畫認證碼,每個認證碼在不同的水平位置
                  String str1[] = new String[6];
                  for (int i = 0; i < str1.length; i++) {
                      str1[i] = num.substring(i, i + 1);
                      int w = 0;
                      int x = (i + 1) % 3;

          //隨即生成驗證碼字符的水平偏移量
                      if (x == random.nextInt(3)) {
                          w = 19 - random.nextInt(7);
                      } else {
                          w = 19 + random.nextInt(7);
                      }

          //隨即生成顏色
                      Color color1 = new Color(random.nextInt(180), random.nextInt(180),
                                               random.nextInt(180));
                      g.setColor(color1);
                      g.drawString(str1[i], 20 * i + 10, w);
                  }

                  // 隨機產生干擾點,并用不同的顏色表示,使圖象中的認證碼不易被其它程序探測到
                  for (int i = 0; i < 100; i++) {
                      int x = random.nextInt(width);
                      int y = random.nextInt(height);
                      Color color1 = new Color(random.nextInt(255), random.nextInt(255),
                                               random.nextInt(255));
                      g.setColor(color1); //隨即畫各種顏色的點
                      g.drawOval(x, y, 0, 0);
                  }
                  //畫干擾線
                  for (int i = 0; i < 5; i++) {
                      int x = random.nextInt(width);
                      int y = random.nextInt(height);
                      int x1 = random.nextInt(width);
                      int y1 = random.nextInt(height);
                      Color color1 = new Color(random.nextInt(255), random.nextInt(255),
                                               random.nextInt(255));

                      g.setColor(color1); //隨即畫各種顏色的線
                      g.drawLine(x, y, x1, y1);
                  }
                  //圖像生效
                  g.dispose();
                  //輸出頁面
                  ImageIO.write(bi, "jpg", out);
              }

              public static void main(String[] args) throws IOException {
                  String num = random();
                  System.out.println(num);
                  render(num, new FileOutputStream("D:\\test.jpg"));
                  System.out.println("Image generated.");
              }
          }

          二 驗證碼的實現,使用servlet來實現驗證碼

          import java.io.IOException;

          import javax.servlet.ServletException;
          import javax.servlet.http.HttpServlet;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;
          import javax.servlet.http.HttpSession;

          import peopleBlog.util.RandomImageGenerator;


          /**
           * 用于產生隨即圖片以防止非法攻擊
           * @author Liudong
           */
          public class RandomImageServlet extends HttpServlet {
           public final static String RANDOM_LOGIN_KEY = "RANDOM_LOGIN_KEY";

           public void init() throws ServletException {
            System.setProperty("java.awt.headless","true");
           }

              public static String getRandomLoginKey(HttpServletRequest req) {
                  return (String)req.getSession().getAttribute(RANDOM_LOGIN_KEY);
              }

              protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
              {
                  HttpSession ssn = req.getSession();
                  if(ssn!=null) {
                      String randomString = RandomImageGenerator.random();//生成種子
                      ssn.setAttribute(RANDOM_LOGIN_KEY,randomString);//將種子放到session里面
                      res.setContentType("image/jpeg");//設置圖像生成格式

                      RandomImageGenerator.render(randomString,res.getOutputStream());//輸出到頁面
                  }
              }
          }

          其中 ssn.setAttribute(RANDOM_LOGIN_KEY,randomString);這一行代碼的作用是:

          當從頁面上輸入驗證碼,提交后,在后臺sevlet或者action里面驗證輸入的驗證碼和session里面的是否一致,如果不一致返回錯誤信息.

          三,頁面實現

          在web.xml文件中設置

          <servlet>
              <servlet-name>image</servlet-name>
              <servlet-class>peopleBlog.RandomImageServlet</servlet-class>
              <load-on-startup>5</load-on-startup>
            </servlet>

            <servlet-mapping>
              <servlet-name>image</servlet-name>
              <url-pattern>/verifyCode.jsp</url-pattern>

            </servlet-mapping>

          然后在jsp頁面中

                  <td><input name="verifyCode" type="text" id="verifyCode" class="input2" style="width:55px;" onkeydown="JavaScript:ifEnter()"/>&nbsp; <img src="/blog/verifyCode.jsp" align="absMiddle" border="0"/> </td>

          再在目的servlet中進行驗證碼判斷就ok了

          posted on 2007-04-14 23:05 terryliu 閱讀(300) 評論(0)  編輯  收藏 所屬分類: java

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 武隆县| 龙江县| 双牌县| 呼图壁县| 报价| 四平市| 白朗县| 浏阳市| 玛沁县| 沧州市| 高台县| 鄂伦春自治旗| 中超| 彰武县| 常山县| 佳木斯市| 玉环县| 宿州市| 嵊泗县| 新绛县| 富蕴县| 定州市| 钟山县| 丹阳市| 徐水县| 南丹县| 漳浦县| 白水县| 芦溪县| 洛川县| 上高县| 庄浪县| 青冈县| 咸宁市| 巴马| 九台市| 内江市| 民乐县| 萨嘎县| 城市| 隆尧县|