SpringMVC中生成验证码的一个实例

来源:互联网 发布:九龙擒庄指标源码破译 编辑:程序博客网 时间:2024/05/22 01:26

在我们平常的网页开发中,需要验证码的地方还是挺多的,下面用一个小实例展示一个Java验证码的生成过程。


步骤:
1.首先定义验证的的宽高,字母个数,以及干扰线的多少。
2.这里通过将时间戳作为url参数的方式,因为有些浏览器会缓存验证码,测试的时候,随机输入一段字符串即可。
3.定义画布,得到画笔
4.画边框,填充内部,然后设置干扰线,随机产生字母(记得设置颜色字体),填充到画布中,将流保存为文件格式输出到屏幕上。
5.把验证码的值放入session中,方便以后使用。
6.设置浏览器禁止缓存验证码。

package com.ysk.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import javax.imageio.ImageIO;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import java.awt.*;import java.awt.image.BufferedImage;import java.io.IOException;import java.util.Random;/** * Created by Y.S.K on 2017/8/24 in ThymeleafTest. */@Controllerpublic class CodeController {    private int width = 90;//验证码宽度    private int height = 40;//验证码高度    private int codeCount = 4;//验证码个数    private int lineCount = 19;//干扰线个数    char[] codeSequence = { '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', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };    /**     * 具体获取验证码的方法     * @param time  time为时戳,这样的话可以避免浏览器缓存验证码     * @throws IOException     */    @RequestMapping(value = "/code/{time}",method = RequestMethod.GET)    public void getCode(@PathVariable("time") String time, HttpServletRequest request,                        HttpServletResponse response) throws IOException {        //定义随机数类        Random r = new Random();        //定义存储验证码的类        StringBuilder builderCode = new StringBuilder();        //定义画布        BufferedImage buffImg = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);        //得到画笔        Graphics g = buffImg.getGraphics();        //1.设置颜色,画边框        g.setColor(Color.black);        g.drawRect(0,0,width,height);        //2.设置颜色,填充内部        g.setColor(Color.white);        g.fillRect(1,1,width-2,height-2);        //3.设置干扰线        g.setColor(Color.gray);        for (int i = 0; i < lineCount; i++) {            g.drawLine(r.nextInt(width),r.nextInt(width),r.nextInt(width),r.nextInt(width));        }        //4.设置验证码        g.setColor(Color.blue);        //4.1设置验证码字体        g.setFont(new Font("宋体",Font.BOLD|Font.ITALIC,15));        for (int i = 0; i < codeCount; i++) {            char c = codeSequence[r.nextInt(codeSequence.length)];            builderCode.append(c);            g.drawString(c+"",15*(i+1),15);        }        //5.输出到屏幕        ServletOutputStream sos = response.getOutputStream();        ImageIO.write(buffImg,"png",sos);        //6.保存到session中        HttpSession session = request.getSession();        session.setAttribute("codeValidate",builderCode.toString());        //7.禁止图像缓存。        response.setHeader("Pragma", "no-cache");        response.setHeader("Cache-Control", "no-cache");        response.setDateHeader("Expires", 0);        response.setContentType("image/png");        //8.关闭sos        sos.close();    }}

打开浏览器,
这里写图片描述

原创粉丝点击