16-Java-隨機驗證碼的基本實現


隨機驗證碼的基本實現

  一、實現步驟

    1.動態創建圖片

    2.在圖上隨機畫字符

    3.在圖上隨機生成字符字體格式

    4.將字符串保存在session中

    5.在圖上畫干擾線

    6.將圖片利用response輸出

 

 1         //1.動態創建圖片
 2                 int width = 70,height = 30;//設置驗證碼圖片寬和高
 3                 BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);//第一個參數表示圖片寬度,第二個參數表示圖片高度,第三個參數表示圖片類型
 4             //2.在圖片上畫字符
 5                 StringBuffer STR = new StringBuffer();//保存隨機產生的字符積累成的字符串
 6                 Graphics graphics = img.getGraphics();//建立一張圖片
 7                 graphics.fillRect(0, 0, width, height);//畫一個矩形
 8                 //隨機生成字符
 9                 Font font = new Font("楷體",Font.BOLD ,17); //設置字體格式為楷體,字號為17,並加粗
10                 String a = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";//隨機字符集
11                 Random rand = new Random();
12                 for (int i = 0; i < 4; i++) //隨機畫4個字符
13                 {
14                     char t = a.charAt(rand.nextInt(32));//在字符集中隨機選一個字符
15                     STR.append(t);//將隨機產生的字符積累到字符串尾部
16                     Color color = new Color(rand.nextInt(255),rand.nextInt(255),rand.nextInt(255));//產生一種由任意三原色組成的顏色
17                     graphics.setColor(color);//給字符設置顏色
18                     graphics.setFont(font);//給字符設置字體樣式
19                     graphics.drawString(Character.toString(t), 10+i*15, 20);//參數為(char,x,y),表示在之前畫的矩形截面中指定x,y坐標位置顯示字符
20                 }
21              //4.將圖片上的文字保存到session中
22                 HttpSession sess =  request.getSession();
23                 sess.setAttribute("codeSTR", STR);
24             //5.在圖上畫干擾線
25                 for (int i = 0; i < 5; i++) //隨機畫5條干擾直線
26                 { 
27                     Color color = new Color(rand.nextInt(255),rand.nextInt(255),rand.nextInt(255));//產生一種由任意三原色組成的顏色
28                     graphics.setColor(color);//給直線設置顏色
29                     graphics.drawLine(rand.nextInt(width), rand.nextInt(height), rand.nextInt(width), rand.nextInt(height));//參數為(x1,y1,x2,y2),表示直線的起止坐標
30                 }
31             //6.將圖片利用response輸出
32                 ServletOutputStream outputstream =  response.getOutputStream();
33                 ImageIO.write(img, "jpeg", outputstream);

 

  二、驗證碼看不清楚切換

    其實是通過請求讓驗證碼重新再畫一次

      function flushimg(){//刷新驗證碼
              $("#code").attr("src","image.do?="+Math.random());//更新新請求
          }

 

  三、驗證碼的校驗

    1.先判斷驗證碼是否為空

    2.再通過ajax與后台判斷驗證碼是否填寫正確

 1         var str = $("#cod").val().trim();//獲取驗證碼的字符串並去掉首尾空格
 2               if(str == ""||str == null){
 3                   $("#node").html("驗證碼不能為空,請重輸!");
 4                   $("#code").attr("src","image.do?="+Math.random());//更新新請求
 5                   return false;
 6               }
 7               var check = true;
 8               $.ajax({
 9                   url:"checkcode.do",
10                   type:"post",
11                   async:false,//讓返回false和ajax請求異步
12                   data:{"code":str},
13                   dataType:"text",
14                   success:function(data){
15                       if(data == "驗證碼錯誤"){
16                           $("#node").html("驗證碼錯誤,請重輸!");
17                           $("#code").attr("src","image.do?="+Math.random());//更新新請求
18                           check = false;
19                       }
20                   },
21                   error:function(){
22                       alert("服務器異常,請聯系管理員...");
23                       check = false;
24                   }
25               });
26               return check;

 

  四、Servlet禁止瀏覽器緩存

1 response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
2 response.setHeader("Pragma", "no-cache");
3 response.setDateHeader("Expires", 0);

 


免責聲明!

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



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