【Java】登录验证码

Html:

1 <input id="verifyCode" name="verifyCode" type="text"
2                         value="" onclick="JavaScript:this.value=''" /></span><cite><img id="imageCode" alt="验证码" src="<%=request.getContextPath()%>/getCodeImage.do" onclick="resetCode()"></cite>

Java:

  1 package cn.hk.base.action;
  2 
  3 import java.awt.Color;
  4 import java.awt.Font;
  5 import java.awt.Graphics;
  6 import java.awt.Graphics2D;
  7 import java.awt.image.BufferedImage;
  8 import java.io.IOException;
  9 import java.util.Random;
 10 
 11 import javax.imageio.ImageIO;
 12 import javax.servlet.http.HttpServletRequest;
 13 import javax.servlet.http.HttpServletResponse;
 14 
 15 import org.springframework.stereotype.Controller;
 16 import org.springframework.web.bind.annotation.RequestMapping;
 17 import org.springframework.web.bind.annotation.ResponseBody;
 18 
 19 import cn.zy.action.BaseAction;
 20 
 21 @Controller
 22 public class CommonAction extends BaseAction{
 23 
 24     /**
 25      * 
 26      */
 27     private static final long serialVersionUID = -8902195523837272079L;
 28 
 29      /**
 30      * 验证图片宽
 31      */
 32     private static final int WIDTH = 114;
 33     /**
 34      * 验证图片高度
 35      */
 36     private static final int HEIGHT = 46;
 37     
 38     @RequestMapping("/getCodeImage.do")
 39     public @ResponseBody void getCodeImage(HttpServletRequest request,HttpServletResponse response) throws IOException{
 40          // 获得图片
 41         BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
 42                 BufferedImage.TYPE_INT_RGB);
 43         // 获取画笔
 44         Graphics g = image.getGraphics();
 45         // 设置边框
 46         g.setColor(Color.LIGHT_GRAY);
 47         g.fillRect(0, 0, WIDTH, HEIGHT);
 48  
 49         g.setColor(Color.WHITE);
 50         g.fillRect(1, 1, WIDTH - 2, HEIGHT - 2);
 51       /*  // 设置背景
 52         g.setColor(Color.getColor("#68B71A"));
 53         g.drawRect(1, 1, WIDTH-2, HEIGHT-2);*/
 54         // 干扰线
 55         g.setColor(Color.GREEN);       
 56         int x1, y1;
 57         int x2, y2;
 58         Random r = new Random();
 59         for (int i = 0; i < 10; i++) {
 60             // 起始xy
 61             x1 = r.nextInt(WIDTH);
 62             y1 = r.nextInt(HEIGHT);
 63             // 结束xy
 64             x2 = r.nextInt(WIDTH);
 65             y2 = r.nextInt(HEIGHT);
 66  
 67             g.drawLine(x1, y1, x2, y2);
 68         }
 69         // 验证码
 70         g.setColor(Color.RED);
 71         g.setFont(new Font("宋体", Font.BOLD, 20));
 72         String base = "qwertyupasdfghjkzxcvbnm23456789QWERTYUIOPASDFGHJKLZXCVBNM";
 73         StringBuilder sb = new StringBuilder();
 74         char ch = 'u0000';
 75         int degree = 0;
 76         r = new Random();
 77         int startX = 8;
 78         for (int i = 0; i < 4; i++) {
 79             ch = base.charAt(r.nextInt(base.length()));
 80  
 81             // 设置旋转, ±20°
 82             degree = r.nextInt() % 20;
 83             ((Graphics2D) g).rotate(degree * Math.PI / 180, startX, 14);
 84             g.drawString(ch + "", startX, 26);
 85             // 取消旋转
 86             ((Graphics2D) g).rotate(-degree * Math.PI / 180, startX, 14);
 87  
 88             startX += 24;
 89             sb.append(ch);
 90         }
 91         request.getSession().setAttribute("checkCode", sb.toString().toLowerCase()); 
 92         // 发送给页面
 93         response.setContentType("image/jpeg");
 94         response.setDateHeader("expries", -1);
 95         response.setHeader("cache-control", "no-cache");
 96         response.setHeader("progma", "no-cache");
 97         ImageIO.write(image, "jpg", response.getOutputStream());
 98        
 99     }
100 }
持之以恒
原文地址:https://www.cnblogs.com/zwqh/p/6276777.html