使用session實現一次性驗證碼


       在登錄頁面和各種頁面,會看到有驗證碼輸入,這樣做的目的是為了防止密碼猜測工具破解密碼,保護了用戶密碼安全,驗證碼只能使用一次,這樣就給密碼猜測工具帶來了很大的困難,基本上阻斷了密碼猜測工具的使用。

       可以使用session獲得一次性驗證碼。先看一下登錄頁面,即顯示驗證碼的頁面,代碼為:

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  2. <html>  
  3.   <head>  
  4.     <title>CheckCode.html</title>  
  5.     <meta http-equiv="content-type" content="text/html; charset=UTF-8">  
  6.   </head>  
  7.     
  8.   <body>  
  9.     <form action="/learnJS/servlet/LoginFormServlet" method="post">  
  10.     用戶名:<input type="text" name="name"/><br/>  
  11.     密     碼:<input type="password" name="pass"><br/>  
  12.     驗證碼:<input type="text" name="check_code"/>  
  13.     <img src="/learnJS/servlet/CheckCodeServlet"/><br/>  
  14.    <input type="submit" name="登錄"/>   
  15.     </form>  
  16.   </body>  
  17. </html>  


驗證碼存放在一張圖片上,那圖片是通過servlet產生的,在servlet中先產生驗證碼存放到session中,供以后驗證使用,然后在畫一張圖片,將驗證碼無規則的放在圖片上,在圖片上畫上干擾字符,然后就可以啦。代碼如下:

  1. package com.you.servlet;  
  2.   
  3. import java.awt.Color;  
  4. import java.awt.Font;  
  5. import java.awt.Graphics;  
  6. import java.awt.image.BufferedImage;  
  7. import java.io.ByteArrayOutputStream;  
  8. import java.io.IOException;  
  9.   
  10. import javax.imageio.ImageIO;  
  11. import javax.servlet.ServletException;  
  12. import javax.servlet.ServletOutputStream;  
  13. import javax.servlet.http.HttpServlet;  
  14. import javax.servlet.http.HttpServletRequest;  
  15. import javax.servlet.http.HttpServletResponse;  
  16. import javax.servlet.http.HttpSession;  
  17.   
  18. public class CheckCodeServlet extends HttpServlet {  
  19.     private static int WIDTH = 60;  
  20.     private static int HEIGHT = 20;  
  21.   
  22.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  23.             throws ServletException, IOException {  
  24.         HttpSession session = request.getSession();  
  25.         response.setContentType("image/jpeg");  
  26.         ServletOutputStream sos = response.getOutputStream();  
  27.           
  28.         //設置瀏覽器不要緩存此圖片  
  29.         response.setHeader("Pragma""No-cache");  
  30.         response.setHeader("Cache-Control""no-cache");  
  31.         response.setDateHeader("Expires"0);  
  32.           
  33.         //創建內存圖像並獲得其圖形上下文  
  34.         BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);  
  35.         Graphics g = image.getGraphics();  
  36.           
  37.         //產生隨機的驗證碼  
  38.         char[] rands = generateCheckCode();  
  39.           
  40.         //產生圖像  
  41.         drawBackground(g);  
  42.         drawRands(g, rands);  
  43.           
  44.         //結束圖像的繪制過程,完成圖像  
  45.         g.dispose();  
  46.           
  47.         //將圖像輸出到客戶端  
  48.         ByteArrayOutputStream bos = new ByteArrayOutputStream();  
  49.         ImageIO.write(image, "JPEG", bos);  
  50.         byte[] buf = bos.toByteArray();  
  51.         response.setContentLength(buf.length);  
  52.         sos.write(buf);  
  53.         bos.close();  
  54.         sos.close();  
  55.           
  56.         //將當前驗證碼存入到session中  
  57.         session.setAttribute("check_code"new String(rands));  
  58.   
  59.     }  
  60.   
  61.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  62.             throws ServletException, IOException {  
  63.   
  64.         doGet(request, response);  
  65.     }  
  66.       
  67.     private char[] generateCheckCode() {  
  68.         //定義驗證碼的字符集  
  69.         String chars = "0123456789abcdefghigklmnopqrstuvwxyz";  
  70.         char[] rands = new char[4];  
  71.         for(int i = 0; i < 4; i++) {  
  72.             int rand = (int)(Math.random() * 36);  
  73.             rands[i] = chars.charAt(rand);  
  74.         }  
  75.         return rands;  
  76.     }  
  77.       
  78.     private void drawRands(Graphics g, char[] rands) {  
  79.         g.setColor(Color.BLACK);  
  80.         g.setFont(new Font(null,Font.ITALIC|Font.BOLD,18));  
  81.         //在不同的高度上輸出驗證碼的每個字符  
  82.         g.drawString("" + rands[0], 117);  
  83.         g.drawString("" + rands[1], 1615);  
  84.         g.drawString("" + rands[2], 3118);  
  85.         g.drawString("" + rands[3], 4616);  
  86.         System.out.println(rands);  
  87.     }  
  88.       
  89.     private void drawBackground(Graphics g) {  
  90.         //畫背景  
  91.         g.setColor(new Color(0xDCDCDC));  
  92.         g.fillRect(00, WIDTH, HEIGHT);  
  93.         //隨機產生120個干擾點   
  94.         for(int i = 0; i < 120; i++) {  
  95.             int x = (int)(Math.random() * WIDTH);  
  96.             int y = (int)(Math.random() * HEIGHT);  
  97.             int red = (int)(Math.random() * 255);  
  98.             int green = (int)(Math.random() * 255);  
  99.             int blue = (int)(Math.random() * 255);  
  100.             g.setColor(new Color(red, green, blue));  
  101.             g.drawOval(x, y, 10);  
  102.         }  
  103.     }  
  104.   
  105. }  


然后是登錄之后處理用戶是否登錄成功的servlet,在這個servlet中通過比較session中存放的驗證碼和用戶輸入的驗證碼,如果匹配則進行下一步判斷,如果不匹配直接輸出驗證碼不匹配的問題。代碼為:

  1. package com.you.servlet;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.PrintWriter;  
  5.   
  6. import javax.servlet.ServletException;  
  7. import javax.servlet.http.HttpServlet;  
  8. import javax.servlet.http.HttpServletRequest;  
  9. import javax.servlet.http.HttpServletResponse;  
  10. import javax.servlet.http.HttpSession;  
  11.   
  12. public class LoginFormServlet extends HttpServlet {  
  13.   
  14.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  15.             throws ServletException, IOException {  
  16.         response.setContentType("text/html;charset=utf-8");  
  17.         PrintWriter out = response.getWriter();  
  18.           
  19.         HttpSession session = request.getSession(false);  
  20.         if(session == null) {  
  21.             out.print("驗證碼處理問題");  
  22.             return;  
  23.         }  
  24.           
  25.         String saveCode = (String)session.getAttribute("check_code");  
  26.         if(saveCode == null) {  
  27.             out.print("驗證碼處理問題");  
  28.             return;  
  29.         }  
  30.           
  31.         String checkCode = request.getParameter("check_code");  
  32.         if(!saveCode.equals(checkCode)) {  
  33.             out.print("驗證碼無效!");  
  34.             return;  
  35.         }  
  36.           
  37.         session.removeAttribute("check_code");  
  38.         out.print("驗證碼通過,服務器正在校驗用戶名和密碼!");  
  39.   
  40.     }  
  41.   
  42.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  43.             throws ServletException, IOException {  
  44.   
  45.         doGet(request, response);  
  46.     }  
  47.   
  48. }  




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM