struts2中使用的验证码

来源:互联网 发布:vip域名后缀可以备案 编辑:程序博客网 时间:2024/05/20 19:47
对于验证码,必须采用一张临时图片来显示随机验证码,万不可采用直接在HTML页面中输入验证码,也不可使用固定的图片来显示随机验证码!!!

因为Cracker很容易根据页面源代码来取得随机验证码的字符串,从而失去验证码的用途。不仅如此,甚至图形验证码的数字也不能太清楚,一旦图形验证码的图片太清楚,Crack程序也可分析出该图片中的随机字符串!!



生成验证码的Servlet:
Java代码 复制代码 收藏代码
  1. package com.nongzi.util;   
  2.   
  3. import javax.servlet.*;   
  4. import javax.servlet.http.*;   
  5. import java.io.*;   
  6. import java.awt.*;   
  7. import java.awt.image.*;   
  8. import java.util.*;   
  9. import javax.imageio.*;    
  10.   
  11. public class AuthImg extends HttpServlet   
  12. {   
  13.     //定义图形验证码中绘制字符的字体  
  14.     private final Font mFont =   
  15.         new Font("Arial Black", Font.PLAIN, 16);   
  16.     //定义图形验证码的大小  
  17.     private final int IMG_WIDTH = 100;   
  18.     private final int IMG_HEIGTH = 18;   
  19.     //定义一个获取随机颜色的方法  
  20.     private Color getRandColor(int fc,int bc)   
  21.     {   
  22.         Random random = new Random();   
  23.         if(fc > 255) fc = 255;   
  24.         if(bc > 255) bc = 255;   
  25.         int r = fc + random.nextInt(bc - fc);   
  26.         int g = fc + random.nextInt(bc - fc);   
  27.         int b = fc + random.nextInt(bc - fc);   
  28.         //得到随机颜色  
  29.         return new Color(r , g , b);   
  30.     }   
  31.     //重写service方法,生成对客户端的响应  
  32.     public void service(HttpServletRequest request,   
  33.         HttpServletResponse response)    
  34.         throws ServletException, IOException   
  35.     {   
  36.         //设置禁止缓存  
  37.         response.setHeader("Pragma","No-cache");   
  38.         response.setHeader("Cache-Control","no-cache");   
  39.         response.setDateHeader("Expires"0);   
  40.         response.setContentType("image/jpeg");   
  41.         BufferedImage image = new BufferedImage   
  42.             (IMG_WIDTH , IMG_HEIGTH , BufferedImage.TYPE_INT_RGB);   
  43.         Graphics g = image.getGraphics();   
  44.         Random random = new Random();   
  45.         g.setColor(getRandColor(200 , 250));   
  46.         //填充背景色  
  47.         g.fillRect(11, IMG_WIDTH - 1, IMG_HEIGTH - 1);   
  48.         //为图形验证码绘制边框  
  49.         g.setColor(new Color(102 , 102 , 102));   
  50.         g.drawRect(00, IMG_WIDTH - 1, IMG_HEIGTH - 1);   
  51.         g.setColor(getRandColor(160,200));   
  52.         //生成随机干扰线  
  53.         for (int i = 0 ; i < 80 ; i++)   
  54.         {   
  55.             int x = random.nextInt(IMG_WIDTH - 1);   
  56.             int y = random.nextInt(IMG_HEIGTH - 1);   
  57.             int xl = random.nextInt(6) + 1;   
  58.             int yl = random.nextInt(12) + 1;   
  59.             g.drawLine(x , y , x + xl , y + yl);   
  60.         }   
  61.         g.setColor(getRandColor(160,200));   
  62.         //生成随机干扰线  
  63.         for (int i = 0 ; i < 80 ; i++)   
  64.         {   
  65.             int x = random.nextInt(IMG_WIDTH - 1);   
  66.             int y = random.nextInt(IMG_HEIGTH - 1);   
  67.             int xl = random.nextInt(12) + 1;   
  68.             int yl = random.nextInt(6) + 1;   
  69.             g.drawLine(x , y , x - xl , y - yl);   
  70.         }   
  71.         //设置绘制字符的字体  
  72.         g.setFont(mFont);   
  73.         //用于保存系统生成的随机字符串  
  74.         String sRand = "";   
  75.         for (int i = 0 ; i < 6 ; i++)   
  76.         {   
  77.             String tmp = getRandomChar();   
  78.             sRand += tmp;   
  79.             //获取随机颜色  
  80.             g.setColor(new Color(20 + random.nextInt(110)   
  81.                 ,20 + random.nextInt(110)   
  82.                 ,20 + random.nextInt(110)));   
  83.             //在图片上绘制系统生成的随机字符  
  84.             g.drawString(tmp , 15 * i + 10,15);   
  85.         }   
  86.         //获取HttpSesssion对象  
  87.         HttpSession session = request.getSession(true);   
  88.         //将随机字符串放入HttpSesssion对象中 -----------------------------------  
  89.         session.setAttribute("rand" , sRand);   
  90.         g.dispose();   
  91.         //向输出流中输出图片  
  92.         ImageIO.write(image, "JPEG", response.getOutputStream());   
  93.     }   
  94.     //定义获取随机字符串方法  
  95.     private String getRandomChar()   
  96.     {   
  97.         //生成一个0、1、2的随机数字  
  98.         int rand = (int)Math.round(Math.random() * 2);   
  99.         long itmp = 0;   
  100.         char ctmp = '\u0000';   
  101.         switch (rand)   
  102.         {   
  103.             //生成大写字母  
  104.             case 1:   
  105.                 itmp = Math.round(Math.random() * 25 + 65);   
  106.                 ctmp = (char)itmp;   
  107.                 return String.valueOf(ctmp);   
  108.             //生成小写字母  
  109.             case 2:   
  110.                 itmp = Math.round(Math.random() * 25 + 97);   
  111.                 ctmp = (char)itmp;   
  112.                 return String.valueOf(ctmp);   
  113.             //生成数字  
  114.             default :   
  115.                 itmp = Math.round(Math.random() * 9);   
  116.                 return  itmp + "";   
  117.         }   
  118.     }   
  119. }  




web.xml中的配置:
Java代码 复制代码 收藏代码
  1. <!-- 配置图形验证码Servlet -->   
  2.     <servlet>   
  3.         <servlet-name>img</servlet-name>   
  4.         <servlet-class>com.nongzi.util.AuthImg</servlet-class>   
  5.     </servlet>   
  6.     <!-- 为图形验证码Servlet指定URL -->   
  7.     <servlet-mapping>   
  8.         <servlet-name>img</servlet-name>   
  9.         <url-pattern>/admin/authImg.jpg</url-pattern>   
  10.     </servlet-mapping>  






登陆页面代码:
Java代码 复制代码 收藏代码
  1. <center>   
  2.   
  3.     后台登陆   
  4.     <br>   
  5.     <font color="red"> <s:fielderror /> <s:actionmessage /> <s:property   
  6.             value="tip" /> </font>   
  7.   
  8.     <s:form action="login" method="post" namespace="/admin">   
  9.        
  10.         用户名:<s:textfield name="adminuser.name" />   
  11.         <br>   
  12.         密  码:<s:password name="adminuser.password" />   
  13.         <br>   
  14.         验证码:<s:textfield name="vercode" /><br>   
  15.         <input type="submit" value="登陆" />   
  16.         <input type="reset" value="重置" />   
  17.     </s:form>   
  18.     验证码:   
  19.     <img name="d" src="authImg.jpg"> <font color="red">区分大小写!</font>    
  20. </center>  



Action的代码:

Java代码 复制代码 收藏代码
  1. private Adminuser adminuser;   
  2.     private String vercode;   
  3.     @Resource AdminuserService adminuserService;   
  4.        
  5.            
  6. //省略setter和getter方法  
  7.   
  8.     @Override  
  9.     public String execute() throws Exception {   
  10.            
  11.         Map session = ActionContext.getContext().getSession();   
  12.         String ver2 = (String )session.get("rand");   
  13.         //清空用户Session的随机验证码字符串。  
  14.         session.put("rand" , null);   
  15.         if (!vercode.equals(ver2)){   
  16.             this.addFieldError("vercode""验证码不对,请重新输入!");   
  17.             return INPUT;   
  18.         }   
  19.   
  20.         boolean isAdmin = adminuserService.isAdmin(adminuser);   
  21.         if (isAdmin) {   
  22.                
  23.             HttpSession session1 = org.apache.struts2.ServletActionContext.getRequest().getSession();   
  24.             session1.setAttribute("admin", adminuser);   
  25.             session1.setMaxInactiveInterval(6000);   
  26.                
  27.             return SUCCESS;    
  28.         }else {   
  29.             this.addActionMessage("用户名和密码不匹配,请重新输入!");   
  30.             return INPUT;   
  31.         }   
  32.            
  33.            
  34.     }  
原创粉丝点击