Spring MVC验证码实现

来源:互联网 发布:js 回调函数 返回值 编辑:程序博客网 时间:2024/06/04 18:24

自己练习项目中使用到的登录验证码的实现,写个文章记录一下:

首先工具类的代码如下:

/** * 生成随机数字验证码 */public class RandomCodeUtil {    private ByteArrayInputStream byteImg;//buff图像1    private BufferedImage buffImg;//buff图像2    private String str;//验证码    private int codeCount = 4;//定义图片上显示验证码的个数    private int xx = 28;//验证码水平位置偏移量    private int codeY = 30;//验证码垂直位置偏移量    private int fontHeight = 30;//字体大小    private int runLin = 60;//干扰线数    private int red = 255, green = 100, blue = 50;//字体数值范围内的随机颜色    private int bgfc = 150, bgbc = 200;//背景颜色    private int linfc = 200, linbc = 250;//干扰线颜色    private int width = 158, height = 40;//图像的宽、高    //不包含字母i l o I,数字0 1 防止输入验证码时混淆    char[] codeSequence = {            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',            '2', '3', '4', '5', '6', '7', '8', '9',    };    //0-runNumber之间的随着自然数 获得验证码    private int ruNumber = codeSequence.length - 1;    private RandomCodeUtil() {        init();// 初始化属性    }    /**     * 取得RandomNumUtil实例     */    public static RandomCodeUtil Instance() {        return new RandomCodeUtil();    }    /**     * 取得验证码图片byteImg     */    public ByteArrayInputStream GetByteImg() {        return this.byteImg;    }    /**     * 取得验证码图片buffImg     */    public BufferedImage getBuffImg() {        return this.buffImg;    }    /**     * 取得图片的验证码字符串     */    public String getString() {        return this.str;    }    /**     * 初始化属性     */    private void init() {        //在内存中创建图像        BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);        //获取图形上下文        Graphics g = buffImg.getGraphics();        //生成随机类        Random random = new Random();        //设定背景色        g.setColor(getRandColor(bgfc, bgbc));        g.fillRect(0, 0, width, height);        //设定字体        g.setFont(new Font("Time New Roman", Font.BOLD, fontHeight));        //随机产生runLine条干扰线,使图像中的认证码不易被其他程序探测到        g.setColor(getRandColor(linfc, linbc));        for (int i = 0; i < runLin; i++) {            int x = random.nextInt(width);            int y = random.nextInt(height);            int xl = random.nextInt(12);            int y1 = random.nextInt(12);            g.drawLine(x, y, x + xl, y + y1);        }        //randomCode用于保存随机产生的验证码,以便用户登录后进行验证        StringBuffer randomCode = new StringBuffer();        //随机产生codeCount数字的验证码        for (int i = 0; i < codeCount; i++) {            //得到随机产生的验证码数字            String code = String.valueOf(codeSequence[random.nextInt(ruNumber)]);            //产生随机的颜色分量来构造颜色值,这样输出的每位数字的颜色值都将不同            //用随机产生的颜色将颜色码绘制到图像中            g.setColor(new Color(random.nextInt(red), random.nextInt(green), random.nextInt(blue)));            //调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成            g.drawString(code, (i + 1) * xx, codeY);            //将产生 的四个随机数组合在一起            randomCode.append(code);        }        //赋值验证码        this.str = randomCode.toString();        //图像生效        g.dispose();        //赋值byte图像1 输出图片流        ByteArrayInputStream input = null;        ByteArrayOutputStream output = new ByteArrayOutputStream();        try {            ImageOutputStream imageOut = ImageIO                    .createImageOutputStream(output);            ImageIO.write(buffImg, "JPEG", imageOut);            imageOut.close();            input = new ByteArrayInputStream(output.toByteArray());        } catch (Exception e) {            System.out.println("验证码图片产生出现错误:" + e.toString());        }        this.byteImg = input;    // 赋值byte图像1        /************赋值buff图像2 输出图像**************/        this.buffImg = buffImg; // 赋值buff图像2    }    /**     * 给定范围获得随机颜色     *     * @param fc     * @param bc     * @return     */    private Color getRandColor(int fc, int bc) {        Random random = new Random();        if (fc > 255)            fc = 255;        if (bc > 255)            bc = 255;        int r = fc + random.nextInt(bc - fc);        int g = fc + random.nextInt(bc - fc);        int b = fc + random.nextInt(bc - fc);        return new Color(r, g, b);    }}

Controller的代码如下:

/** *  * @author wxc * */@Controllerpublic class CodeController {@RequestMapping("/code")     public void getCode(HttpServletRequest req, HttpServletResponse resp) throws IOException { RandomCodeUtil rdnu = RandomCodeUtil.Instance();HttpSession session = req.getSession(); // 取得随机字符串放入Session中session.setAttribute("RANDOMCODE", rdnu.getString());// 禁止图像缓存。          resp.setHeader("Pragma", "no-cache");         resp.setHeader("Cache-Control", "no-cache");         resp.setDateHeader("Expires", 0);          resp.setContentType("image/jpeg");          // 将图像输出到Servlet输出流中。          ServletOutputStream sos = resp.getOutputStream();         ImageIO.write(rdnu.getBuffImg(), "jpeg", sos);         sos.close();     }}

Jsp页面的代码如下:

<div class="login_info"><label>验证码:</label> <input type="text" name="code" id="code"class="login_input" />  <span id="codeerr" class="errInfo"></span></div><div class="login_info"><label> </label>         <imgid="codeImg" alt="点击更换" title="点击更换" src="" /></div>
function changeCode() {$("#codeImg").attr("src", "code?t=" + genTimestamp());}
最终实现效果如下图所示:



如果需要源码的话,请点击链接获得源码

该链接为使用ssm框架搭建的crm系统,方便上手学习使用

原创粉丝点击