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);
原文地址:https://www.cnblogs.com/qinqin-me/p/12432290.html