分享

SpringMVC杂记(十) 验证码生成

 feimishiwo 2014-08-13
     以前写过一篇关于这个的博客,现在用SpringMVC了,重写一遍好了。
源代码在GitHub上

Java代码  收藏代码
  1. package com.github.yingzhuo.mycar.controller;  
  2.   
  3. import java.awt.Color;  
  4. import java.awt.Font;  
  5. import java.awt.Graphics;  
  6. import java.awt.image.BufferedImage;  
  7. import java.io.IOException;  
  8. import java.util.Random;  
  9.   
  10. import javax.imageio.ImageIO;  
  11. import javax.servlet.http.HttpSession;  
  12.   
  13. import org.apache.commons.io.IOUtils;  
  14. import org.apache.commons.io.output.ByteArrayOutputStream;  
  15. import org.springframework.beans.factory.annotation.Value;  
  16. import org.springframework.http.HttpHeaders;  
  17. import org.springframework.http.HttpStatus;  
  18. import org.springframework.http.MediaType;  
  19. import org.springframework.http.ResponseEntity;  
  20. import org.springframework.stereotype.Controller;  
  21. import org.springframework.web.bind.annotation.RequestMapping;  
  22.   
  23. @Controller  
  24. public class CaptchaController {  
  25.   
  26.     public static final String CAPTCHA_SESSION_ATTR_NAME = UserController.class  
  27.             .getName() + "#CAPTCHA_SESSION_ATTR_NAME";  
  28.   
  29.     private static final HttpHeaders HTTP_HEADERS;  
  30.     private static final Random RANDOM;  
  31.   
  32.     static {  
  33.         HTTP_HEADERS = new HttpHeaders();  
  34.         HTTP_HEADERS.set("Pragma", "No-cache");  
  35.         HTTP_HEADERS.set("Cache-Control", "No-cache");  
  36.         HTTP_HEADERS.setDate("Expires", 0);  
  37.         HTTP_HEADERS.setContentType(MediaType.IMAGE_JPEG);  
  38.         RANDOM = new Random();  
  39.     }  
  40.   
  41.     private int width;  
  42.     private int height;  
  43.   
  44.     @RequestMapping(value = "/images/captcha.jpeg")  
  45.     public ResponseEntity<byte[]> captcha(HttpSession session) throws IOException {  
  46.   
  47.         BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
  48.         Graphics g = image.getGraphics();  
  49.         g.setColor(getRandColor(200, 250));  
  50.         g.fillRect(1, 1, width - 1, height - 1);  
  51.         g.setColor(new Color(102, 102, 102));  
  52.         g.drawRect(0, 0, width - 1, height - 1);  
  53.         g.setFont(new Font("Times New Roman", Font.PLAIN, 17));  
  54.         g.setColor(getRandColor(160, 200));  
  55.   
  56.         // 画随机线  
  57.         for (int i = 0; i < 155; i++) {  
  58.             int x = RANDOM.nextInt(width - 1);  
  59.             int y = RANDOM.nextInt(height - 1);  
  60.             int xl = RANDOM.nextInt(6) + 1;  
  61.             int yl = RANDOM.nextInt(12) + 1;  
  62.             g.drawLine(x, y, x + xl, y + yl);  
  63.         }  
  64.   
  65.         // 从另一方向画随机线  
  66.         for (int i = 0; i < 70; i++) {  
  67.             int x = RANDOM.nextInt(width - 1);  
  68.             int y = RANDOM.nextInt(height - 1);  
  69.             int xl = RANDOM.nextInt(12) + 1;  
  70.             int yl = RANDOM.nextInt(6) + 1;  
  71.             g.drawLine(x, y, x - xl, y - yl);  
  72.         }  
  73.           
  74.         // 生成随机数,并将随机数字转换为字母  
  75.         String captchaStr = "";  
  76.         for (int i = 0; i < 6; i++) {  
  77.             int itmp = RANDOM.nextInt(26) + 65;  
  78.             char ctmp = (char) itmp;  
  79.             captchaStr += String.valueOf(ctmp);  
  80.             g.setColor(new Color(20 + RANDOM.nextInt(110), 20 + RANDOM.nextInt(110), 20 + RANDOM.nextInt(110)));  
  81.             g.drawString(String.valueOf(ctmp), 15 * i + 10, 16);  
  82.         }  
  83.         g.dispose();  
  84.           
  85.         session.setAttribute(CAPTCHA_SESSION_ATTR_NAME, captchaStr);  
  86.           
  87.         ByteArrayOutputStream out = new ByteArrayOutputStream();  
  88.         ImageIO.write(image, "JPEG", out);  
  89.   
  90.         try {  
  91.             return new ResponseEntity<byte[]>(out.toByteArray(), HTTP_HEADERS, HttpStatus.OK);  
  92.         } finally {  
  93.             IOUtils.closeQuietly(out);  
  94.         }  
  95.     }  
  96.   
  97.     private Color getRandColor(int fc, int bc) {  
  98.         if (fc > 255)  
  99.             fc = 255;  
  100.         if (bc > 255)  
  101.             bc = 255;  
  102.         int r = fc + RANDOM.nextInt(bc - fc);  
  103.         int g = fc + RANDOM.nextInt(bc - fc);  
  104.         int b = fc + RANDOM.nextInt(bc - fc);  
  105.         return new Color(r, g, b);  
  106.     }  
  107.   
  108.     // access method  
  109.     // ----------------------------------------------------------------------------------------------------------------------  
  110.   
  111.     @Value("100")  
  112.     public void setWidth(int width) {  
  113.         this.width = width;  
  114.     }  
  115.   
  116.     @Value("18")  
  117.     public void setHeight(int height) {  
  118.         this.height = height;  
  119.     }  
  120.       
  121. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约