Java生成随机验证码

java

Java代码  

  1. package com.tg.snail.core.util;  
  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. import javax.imageio.ImageIO;  
  10. import javax.servlet.http.HttpServletRequest;  
  11. import javax.servlet.http.HttpServletResponse;  
  12. import javax.servlet.http.HttpSession;  
  13. import org.apache.log4j.Logger;  
  14. import org.springframework.util.StringUtils;  
  15.   
  16. /** 
  17.  * 验证码生成工具类 
  18.  * @author penghy 
  19.  * @date 2014-02-27 
  20.  */  
  21. public abstract class RandomCodeUtils {  
  22.   
  23.     private static Logger logger = Logger.getLogger(RandomCodeUtils.class);  

  24.       
  25.     private static final String RANDOM_CODE_KEY = "random"; //验证码放在session中的key  

  26.       
  27.     private static final int CODE_NUM = 4; //验证码字符个数  

  28.       
  29.     // 设置图形验证码中字符串的字体和大小    

  30.     private static Font myFont = new Font("Arial", Font.BOLD, 16);     

  31.     
  32.     //随机字符数组  

  33.     private static char[] charSequence = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();  

  34.           
  35.     private static Random random = new Random();  

  36.       
  37.       
  38.     /** 

  39.      * 生成随机验证码 
  40.      * @param request 
  41.      * @param response 
  42.      */  
  43.     public static void createRandomCode(HttpServletRequest request, HttpServletResponse response){  

  44.         // 阻止生成的页面内容被缓存,保证每次重新生成随机验证码    

  45.         response.setHeader("Pragma", "No-cache");    

  46.         response.setHeader("Cache-Control", "no-cache");    

  47.         response.setDateHeader("Expires", 0);    

  48.         response.setContentType("image/jpeg");    

  49.         // 指定图形验证码图片的大小    

  50.         int width = 80, height = 25;    

  51.         // 生成一张新图片    

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

  53.         // 在图片中绘制内容    

  54.         Graphics g = image.getGraphics();     
  55.         g.setColor(getRandomColor(200, 250));    

  56.         g.fillRect(1, 1, width - 1, height - 1);    

  57.         g.setColor(new Color(102, 102, 102));    

  58.         g.drawRect(0, 0, width - 1, height - 1);    

  59.         g.setFont(myFont);    
  60.         // 随机生成线条,让图片看起来更加杂乱    

  61.         g.setColor(getRandomColor(160, 200));    

  62.         for (int i = 0; i < 155; i++) {    

  63.             int x = random.nextInt(width - 1);// 起点的x坐标    

  64.             int y = random.nextInt(height - 1);// 起点的y坐标    

  65.             int x1 = random.nextInt(6) + 1;// x轴偏移量    

  66.             int y1 = random.nextInt(12) + 1;// y轴偏移量    

  67.             g.drawLine(x, y, x + x1, y + y1);    
  68.         }    
  69.         // 随机生成线条,让图片看起来更加杂乱    

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

  71.             int x = random.nextInt(width - 1);    

  72.             int y = random.nextInt(height - 1);    

  73.             int x1 = random.nextInt(12) + 1;    

  74.             int y1 = random.nextInt(6) + 1;    

  75.             g.drawLine(x, y, x - x1, y - y1);    
  76.         }    
  77.     
  78.         // 该变量用来保存系统生成的随机字符串    

  79.         StringBuilder sRand = new StringBuilder(CODE_NUM);    

  80.         for (int i = 0; i < CODE_NUM; i++) {    

  81.             // 取得一个随机字符    

  82.             String tmp = getRandomChar();    
  83.             sRand.append(tmp);  
  84.             // 将系统生成的随机字符添加到图形验证码图片上    

  85.             g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));    

  86.             g.drawString(tmp, 15 * i + 10, 20);    

  87.         }    
  88.     
  89.         // 取得用户Session    

  90.         HttpSession session = request.getSession(true);    

  91.         // 将系统生成的图形验证码添加    

  92.         session.setAttribute(RANDOM_CODE_KEY, sRand.toString());    
  93.         g.dispose();    
  94.         // 输出图形验证码图片    

  95.         try {  

  96.             ImageIO.write(image, "JPEG", response.getOutputStream());  

  97.         } catch (IOException e) {  

  98.             e.printStackTrace();  
  99.         }    
  100.     
  101.     }  
  102.       
  103.     /** 

  104.      * 检查用户输入的验证码是否正确 
  105.      * @param request 
  106.      * @param inputCode 
  107.      * @return true: 正确, false:不正确 
  108.      */  
  109.     public static boolean checkRandomCode(HttpServletRequest request, String inputCode){  

  110.         HttpSession session = request.getSession(false);  

  111.         if(session != null && StringUtils.hasLength(inputCode)){  

  112.             String code = (String) session.getAttribute(RANDOM_CODE_KEY);  
  113.             logger.info("inputCode:"+inputCode.trim()+",code:"+code);  

  114.             return inputCode.trim().equalsIgnoreCase(code);  

  115.         }  
  116.         return false;  

  117.     }  
  118.       
  119.     /** 

  120.      * 移除验证码 
  121.      * @param request 
  122.      * @param inputCode 
  123.      */  
  124.     public static void removeRandomCode(HttpServletRequest request){  

  125.         HttpSession session = request.getSession(false);  

  126.         if(session != null){  

  127.            session.removeAttribute(RANDOM_CODE_KEY);  
  128.         }  
  129.     }  
  130.       
  131.     // 生成随机颜色    

  132.     private static Color getRandomColor(int fc, int bc) {    

  133.         Random random = new Random();    

  134.         if (fc > 255)    

  135.             fc = 255;    

  136.         if (bc > 255)    

  137.             bc = 255;    

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

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

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

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

  142.     }    
  143.       
  144.     // 随机生成一个字符    

  145.     private static String getRandomChar() {    

  146.         int index = random.nextInt(charSequence.length);  

  147.         return String.valueOf(charSequence[index]);  

  148.     }  

以上是 Java生成随机验证码 的全部内容, 来源链接: utcz.com/z/392554.html

回到顶部