暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Java生成带计算的验证码

开发者的花花世界 2017-09-02
382
  1. import java.awt.Color;  

  2. import java.awt.Font;  

  3. import java.awt.Graphics;  

  4. import java.awt.image.BufferedImage;  

  5. import java.io.IOException;  

  6. import java.io.OutputStream;  

  7. import java.util.Random;  

  8.   

  9. import javax.imageio.ImageIO;  

  10. import javax.servlet.ServletException;  

  11. import javax.servlet.http.HttpServlet;  

  12. import javax.servlet.http.HttpServletRequest;  

  13. import javax.servlet.http.HttpServletResponse;  

  14. import javax.servlet.http.HttpSession;  

  15.   

  16.   

  17.   

  18.   

  19. /** 

  20.  * 生成带简单运算(加、减)的验证码 

  21.  */  

  22. public class CaptchaServlet extends HttpServlet {     

  23.     

  24.     private static final long serialVersionUID = 5581444867349884055L;  

  25.       

  26.     private static final Random random = new Random();   

  27.   

  28.     public void doGet(HttpServletRequest request, HttpServletResponse response)     

  29.             throws ServletException, IOException {     

  30.         this.doPost(request, response);     

  31.     }  

  32.       

  33.     public void doPost(HttpServletRequest request, HttpServletResponse response)     

  34.             throws ServletException, IOException {  

  35.         String type = request.getParameter("type");  

  36.         if (null == type || "".equals(type)) {  

  37.             return;  

  38.         }  

  39.           

  40.         if ("genarate".equals(type)) {  

  41.             int width = 100, height = 30;  

  42.             //创建BufferedImage对象,设置图片的长度宽度和色彩。     

  43.             BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);     

  44.               

  45.             OutputStream os = response.getOutputStream();     

  46.               

  47.             //取得Graphics对象,用来绘制图片     

  48.             Graphics g = image.getGraphics();     

  49.               

  50.             //绘制图片背景和文字,释放Graphics对象所占用的资源。     

  51.             g.setColor(getRandColor(50100));     

  52.               

  53.             //设置内容生成的位置     

  54.             g.fillRect(00, width, height);     

  55.               

  56.             //设置内容的字体和大小     

  57.             g.setFont(new Font("Times New Roman", Font.BOLD, 20));     

  58.               

  59.             //图片背景上随机生成100干扰线,避免通过图片识别破解验证码     

  60.             for (int i = 0; i < 100; i++) {     

  61.                 //设置内容的颜色:主要为生成图片背景的线条     

  62.                 g.setColor(getRandColor(100200));  

  63.                 int x = random.nextInt(width);     

  64.                 int y = random.nextInt(height);     

  65.                 int xl = random.nextInt(width);     

  66.                 int yl = random.nextInt(height);     

  67.                 g.drawLine(x, y, x+1, y+1);     

  68.             }     

  69.             g.setColor(getRandColor(100250));  

  70.             // 将运算式绘到图片中  

  71.             g.drawString(createEquation(request), 222);  

  72.               

  73.             //释放此图形的上下文以及它使用的所有系统资源,类似于关闭流     

  74.             g.dispose();     

  75.               

  76.              //通过ImageIO对象的write静态方法将图片输出。     

  77.             ImageIO.write(image, "JPEG", os);     

  78.             os.close();    

  79.             return;  

  80.         } else if ("validate".equals(type)){  

  81.             String captchaNo = request.getParameter("captchaNo");  

  82.               

  83.             if (null == captchaNo || "".equals(captchaNo)) {  

  84.                 response.getWriter().print("false");  

  85.             }  

  86.               

  87.             if (captchaNo.equals(request.getSession().getAttribute("captchaNo").toString())) {  

  88.                 response.getWriter().print("true");  

  89.             } else {  

  90.                 response.getWriter().print("false");  

  91.             }  

  92.             //校验过的验证码立即失效,换成一个随机数  

  93. //          request.getSession().removeAttribute("captchaNo");  

  94. //          request.getSession().setAttribute("captchaNo", Math.round(Math.random()*10000));  

  95.             return;  

  96.         }  

  97.     }     

  98.     /**   

  99.      * 生成随机颜色  

  100.      * @param fc   

  101.      * @param bc   

  102.      * @return   

  103.      */    

  104.     private Color getRandColor(int fc, int bc) {     

  105.         if (fc > 255)     

  106.             fc = 255;     

  107.         if (bc > 255)     

  108.             bc = 255;     

  109.         int r = fc + random.nextInt(bc - fc);     

  110.         int g = fc + random.nextInt(bc - fc);     

  111.         int b = fc + random.nextInt(bc - fc);     

  112.         return new Color(r, g, b);     

  113.     }     

  114.        

  115.     /** 

  116.      * 生成运算式  

  117.      * @param request 

  118.      * @return 

  119.      */  

  120.     private String createEquation(HttpServletRequest request) {  

  121.          // 定义数组存放加减二个运算符     

  122.          char[] arr = {'+''-'};     

  123.      

  124.          // 生成10以内的随机整数num1     

  125.          int num1 = random.nextInt(30);     

  126.      

  127.          // 生成一个0-2之间的随机整数operate     

  128.          int operate = random.nextInt(2);     

  129.      

  130.          // 生成10以内的随机整数num1     

  131.          int num2 = random.nextInt(30);     

  132.      

  133.          // 避免出现除数为0的情况    

  134.          if (num1 < num2) {  

  135.                 int temp = num1;  

  136.                 num1 = num2;  

  137.                 num2 = temp;  

  138.              }  

  139.      

  140.          // 运算结果     

  141.          int captchaNo = 0;     

  142.      

  143.          // 假定position值0/1分别代表”+”,”-”,计算前面操作数的运算结果     

  144.          switch (operate) {     

  145.          case 0:     

  146.              captchaNo = num1 + num2;     

  147.              break;     

  148.          case 1:     

  149.              captchaNo = num1 - num2;   

  150.              break;       

  151.          }     

  152.      

  153.          // 将生成的验证码值(即运算结果的值)放到session中,以便于后台做验证。     

  154.          HttpSession session = request.getSession();       

  155.          session.setAttribute("captchaNo", captchaNo);   

  156.          return num1+" "+ arr[operate]+" "+num2+" = ?";  

  157.     }  

  158.       

  159. }  


文章转载自开发者的花花世界,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论