随笔-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 阅读(335) 评论(0)  编辑  收藏 所属分类: Java

只有注册用户登录后才能发表评论。


网站导航: