Spring mvc实现验证码
来源:互联网 发布:快手解析软件 编辑:程序博客网 时间:2024/06/16 07:34
参考博客:
http://www.cnblogs.com/xql4j/archive/2013/03/30/2990998.html
1.验证码封装类
package com.huangqi.util;import java.util.Random;import java.awt.image.BufferedImage;import java.awt.Graphics;import java.awt.Font;import java.awt.Color;/** * 验证码生成器类,可生成数字�?大写、小写字母及三�?混合类型的验证码�?支持自定义验证码字符数量�?支持自定义验证码图片的大小; 支持自定义需排除的特殊字符; * 支持自定义干扰线的数量; 支持自定义验证码图文颜色 */public class ValidateCode { /** * 验证码类型为仅数�?0~9 */ public static final int TYPE_NUM_ONLY = 0; /** * 验证码类型为仅字母,即大写�?小写字母混合 */ public static final int TYPE_LETTER_ONLY = 1; /** * 验证码类型为数字、大写字母�?小写字母混合 */ public static final int TYPE_ALL_MIXED = 2; /** * 验证码类型为数字、大写字母混�? */ public static final int TYPE_NUM_UPPER = 3; /** * 验证码类型为数字、小写字母混�? */ public static final int TYPE_NUM_LOWER = 4; /** * 验证码类型为仅大写字�? */ public static final int TYPE_UPPER_ONLY = 5; /** * 验证码类型为仅小写字�? */ public static final int TYPE_LOWER_ONLY = 6; private ValidateCode() { } /** * 生成验证码字符串 * * @param type * 验证码类型,参见本类的静态属�? * @param length * 验证码长度,大于0的整�? * @param exChars * �?��除的特殊字符(仅对数字�?字母混合型验证码有效,无�?��除则为null�? * @return 验证码字符串 */ public static String generateTextCode(int type, int length, String exChars) { if (length <= 0) return ""; StringBuffer code = new StringBuffer(); int i = 0; Random r = new Random(); switch (type) { // 仅数�? case TYPE_NUM_ONLY: while (i < length) { int t = r.nextInt(10); if (exChars == null || exChars.indexOf(t + "") < 0) {// 排除特殊字符 code.append(t); i++; } } break; // 仅字母(即大写字母�?小写字母混合�? case TYPE_LETTER_ONLY: while (i < length) { int t = r.nextInt(123); if ((t >= 97 || (t >= 65 && t <= 90)) && (exChars == null || exChars.indexOf((char) t) < 0)) { code.append((char) t); i++; } } break; // 数字、大写字母�?小写字母混合 case TYPE_ALL_MIXED: while (i < length) { int t = r.nextInt(123); if ((t >= 97 || (t >= 65 && t <= 90) || (t >= 48 && t <= 57)) && (exChars == null || exChars.indexOf((char) t) < 0)) { code.append((char) t); i++; } } break; // 数字、大写字母混�? case TYPE_NUM_UPPER: while (i < length) { int t = r.nextInt(91); if ((t >= 65 || (t >= 48 && t <= 57)) && (exChars == null || exChars.indexOf((char) t) < 0)) { code.append((char) t); i++; } } break; // 数字、小写字母混�? case TYPE_NUM_LOWER: while (i < length) { int t = r.nextInt(123); if ((t >= 97 || (t >= 48 && t <= 57)) && (exChars == null || exChars.indexOf((char) t) < 0)) { code.append((char) t); i++; } } break; // 仅大写字�? case TYPE_UPPER_ONLY: while (i < length) { int t = r.nextInt(91); if ((t >= 65) && (exChars == null || exChars.indexOf((char) t) < 0)) { code.append((char) t); i++; } } break; // 仅小写字�? case TYPE_LOWER_ONLY: while (i < length) { int t = r.nextInt(123); if ((t >= 97) && (exChars == null || exChars.indexOf((char) t) < 0)) { code.append((char) t); i++; } } break; } return code.toString(); } /** * 已有验证码,生成验证码图�? * * @param textCode * 文本验证�? * @param width * 图片宽度 * @param height * 图片高度 * @param interLine * 图片中干扰线的条�? * @param randomLocation * 每个字符的高低位置是否随�? * @param backColor * 图片颜色,若为null,则采用随机颜色 * @param foreColor * 字体颜色,若为null,则采用随机颜色 * @param lineColor * 干扰线颜色,若为null,则采用随机颜色 * @return 图片缓存对象 */ public static BufferedImage generateImageCode(String textCode, int width, int height, int interLine, boolean randomLocation, Color backColor, Color foreColor, Color lineColor) { BufferedImage bim = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = bim.getGraphics(); // 画背景图 g.setColor(backColor == null ? getRandomColor() : backColor); g.fillRect(0, 0, width, height); // 画干扰线 Random r = new Random(); if (interLine > 0) { int x = 0, y = 0, x1 = width, y1 = 0; for (int i = 0; i < interLine; i++) { g.setColor(lineColor == null ? getRandomColor() : lineColor); y = r.nextInt(height); y1 = r.nextInt(height); g.drawLine(x, y, x1, y1); } } // 写验证码 // g.setColor(getRandomColor()); // g.setColor(isSimpleColor?Color.BLACK:Color.WHITE); // 字体大小为图片高度的80% int fsize = (int) (height * 0.8); int fx = height - fsize; int fy = fsize; g.setFont(new Font("Default", Font.PLAIN, fsize)); // 写验证码字符 for (int i = 0; i < textCode.length(); i++) { fy = randomLocation ? (int) ((Math.random() * 0.3 + 0.6) * height) : fy;// 每个字符高低是否随机 g.setColor(foreColor == null ? getRandomColor() : foreColor); g.drawString(textCode.charAt(i) + "", fx, fy); fx += fsize * 0.9; } g.dispose(); return bim; } /** * 生成图片验证�? * * @param type * 验证码类型,参见本类的静态属�? * @param length * 验证码字符长度,大于0的整�? * @param exChars * �?��除的特殊字符 * @param width * 图片宽度 * @param height * 图片高度 * @param interLine * 图片中干扰线的条�? * @param randomLocation * 每个字符的高低位置是否随�? * @param backColor * 图片颜色,若为null,则采用随机颜色 * @param foreColor * 字体颜色,若为null,则采用随机颜色 * @param lineColor * 干扰线颜色,若为null,则采用随机颜色 * @return 图片缓存对象 */ public static BufferedImage generateImageCode(int type, int length, String exChars, int width, int height, int interLine, boolean randomLocation, Color backColor, Color foreColor, Color lineColor) { String textCode = generateTextCode(type, length, exChars); BufferedImage bim = generateImageCode(textCode, width, height, interLine, randomLocation, backColor, foreColor, lineColor); return bim; } /** * 产生随机颜色 * * @return */ private static Color getRandomColor() { Random r = new Random(); Color c = new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)); return c; }}
2.引用
/** * 生成验证码 * @param request * @param response * @throws IOException */ @RequestMapping(value = "/validateCode") public void validateCode(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setHeader("Cache-Control", "no-cache");//使用服务器端控制AJAX页面缓存 String verifyCode = ValidateCode.generateTextCode(ValidateCode.TYPE_NUM_ONLY, 4, null); request.getSession().setAttribute("validateCode", verifyCode); response.setContentType("image/jpeg"); BufferedImage bim = ValidateCode.generateImageCode(verifyCode, 90, 30, 3, true, Color.WHITE, Color.BLACK, null); ImageIO.write(bim, "JPEG", response.getOutputStream()); }
3.jsp页面的引用
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <h1>login page</h1> <form id="" action="dologin" method="post"> <label>User Name</label> <input tyep="text" name="userName" maxLength="40" /> <label>Password</label><input type="password" name="password" /> <li>验证码:<input type="text" name="validateCode" /> <img id="validateCodeImg" src="<%=basePath%>/validateCode" /> <a href="#" onclick="javascript:reloadValidateCode();">看不清?</a></li><input type="submit" value="login" /> </form> <%--用于输入后台返回的验证错误信息 --%> <P><c:out value="${message }" /></P> </body> <script src="js/jquery.min.js"></script> <script type="text/javascript"> <!-- function reloadValidateCode(){ $("#validateCodeImg").attr("src","<%=basePath%>/validateCode?data=" + new Date() + Math.floor(Math.random()*24)); } //--> </script></html>
项目源码:http://pan.baidu.com/s/1qYBtvnm
0 0
- Spring mvc实现验证码
- Spring MVC验证码实现
- CAS验证码,spring mvc实现
- Spring MVC+Kaptcha实现验证码功能
- Spring MVC 验证码
- Spring Mvc Captcha验证码实现(附Servlet实现方式)
- spring-mvc 运用 kaptcha实现验证码的生成
- 基于spring mvc的图片验证码实现
- Spring MVC Captcha 验证码 (附Servlet实现方式)
- MVC实现验证码
- Spring mvc登陆验证码
- Spring MVC 图片验证码
- Spring MVC生成验证码
- spring mvc 验证码功能
- spring mvc 图形验证码
- Spring Mvc添加验证码
- Spring MVC 生成验证码
- Spring MVC + Shiro 实现权限验证
- LeetCode——006
- xib
- tornado实现在JS文件的国际化和静态文件的引用
- 面试题86:八数码问题
- bzoj 4520: [Cqoi2016]K远点对 k-d tree
- Spring mvc实现验证码
- C++标准库和标准模板库
- MFC的多国语言界面的实现
- Win7中双击bat文件变成打开文本编辑器后如何恢复
- 第3周 C程序语言设计(第4版)-7.2示例分配考生的分数段位人数
- Android笔试题目学习笔记
- UITableView使用指南(呕心原创)
- CMap的模板
- 【湖南集训 4.14】C