web项目动态生成验证码

来源:互联网 发布:mac os shimo 破解版 编辑:程序博客网 时间:2024/06/17 14:15

jsp页面显示:

<input name="code" type="text" class="width70" /><img src="code" id="code"  alt="验证码" title="点击更换" />

解释:src:生成image的路径对应后台Controller的code方法 动态生成图片

js:

$(document).ready(function() {    $("#code").click(function(){        var path="code?number="+Math.random();        $("#code").attr("src",path);    });   }); 

解释:更换图片的路径,再次生成新的图片。

生成图片对应的后台方法:

@RequestMapping("/code")    public static void createImg(            HttpServletRequest req,            HttpServletResponse res)             throws ServletException, IOException {            //创建验证码及图片            Object[] objs = ImageUtil.createImage();            //将验证码存入session            HttpSession sn = req.getSession();            sn.setAttribute("imgCode", objs[0]);            //将图片输出给浏览器            res.setContentType("image/png");            OutputStream os = res.getOutputStream();            ImageIO.write(                (BufferedImage) objs[1], "png", os);            os.close();  }

java生成image的方法:

public final class ImageUtil {    // 验证码字符集    private static final char[] chars = {         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',         'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};    // 字符数量    private static final int SIZE = 4;    // 干扰线数量    private static final int LINES = 5;    // 宽度    private static final int WIDTH = 80;    // 高度    private static final int HEIGHT = 40;    // 字体大小    private static final int FONT_SIZE = 30;    /**     * 生成随机验证码及图片     * Object[0] : 验证码字符串   String     * Object[1] : 验证码图片    BufferedImage     */    public static Object[] createImage() {        StringBuffer sb = new StringBuffer();        // 1.创建空白图片        BufferedImage image = new BufferedImage(            WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);        // 2.获取图片画笔        Graphics graphic = image.getGraphics();        // 3.设置画笔颜色        graphic.setColor(Color.LIGHT_GRAY);        // 4.绘制矩形背景        graphic.fillRect(0, 0, WIDTH, HEIGHT);        // 5.画随机字符        Random ran = new Random();        for (int i = 0; i <SIZE; i++) {            // 取随机字符索引            int n = ran.nextInt(chars.length);            // 设置随机颜色            graphic.setColor(getRandomColor());            // 设置字体大小            graphic.setFont(new Font(                null, Font.BOLD + Font.ITALIC, FONT_SIZE));            // 画字符            graphic.drawString(                chars[n] + "", i * WIDTH / SIZE, HEIGHT / 2);            // 记录字符            sb.append(chars[n]);        }        // 6.画干扰线        for (int i = 0; i < LINES; i++) {            // 设置随机颜色            graphic.setColor(getRandomColor());            // 随机画线            graphic.drawLine(ran.nextInt(WIDTH), ran.nextInt(HEIGHT),                    ran.nextInt(WIDTH), ran.nextInt(HEIGHT));        }        // 7.返回验证码和图片        return new Object[]{sb.toString(), image};    }    /**     * 随机取色     */    public static Color getRandomColor() {        Random ran = new Random();        Color color = new Color(ran.nextInt(256),                 ran.nextInt(256), ran.nextInt(256));        return color;    }    public static void main(String[] args) throws IOException {        Object[] objs = createImage();        BufferedImage image =             (BufferedImage) objs[1];        // /home/soft01/x.png        OutputStream os =             new FileOutputStream("d:/x.png");        ImageIO.write(image, "png", os);        os.close();    }}

以上是生成验证码流程
总结: 请求跳转到login页面, 加载login页面时 img标签的src会向后台请求动态生成验证码(验证码放后台session中),通过这里写图片描述
写给img标签。 login页输入验证码请求登录后台接收和session中的验证码对比,完成校验。

0 0
原创粉丝点击