JAVA 验证码生成工具类及使用
java生成的验证码机制,是在指定的字符中随机抽取字符,可以指定字符的个数(验证码个数),然后通过java.awt.image.BufferedImage;创建画布,拿到 java.awt.Graphics;画笔在指定的位置画出文字,包括混淆视觉的线条(其实就是一些细小的线条),然后把呈现的画面生成图片的形式已流的形式打印在指定的窗口或页面中。最后把生成的验证码用字符串的形式放入session中。在使用的时候,只要拿到用户输入的验证码与存放在session中验证码进行比对就行了,最后进行相关的业务处理。
ValidateCode.java //验证码工具类 (该类我写了很多构造方法,可以根据自己的需求,构造出自己想要的验证码)
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* 验证码工具类
*/
public class ValidateCode{
private int width = 90;//验证码宽度 默认值:90
private int height = 40;//验证码高度 默认值:40
private int codeCount = 4;//验证码个数 默认值:4
private int lineCount = 19;//混淆线个数 默认值:19
private int fontSize = 20;//字体大小像素
//存储session中的key值 默认值:"validateCode"
private String sessionKey = "validateCode";
public ValidateCode(){}
/**
*
* @param width 验证码宽度
* @param height 验证码高度
* @param fontSize 字体大小像素
*/
public ValidateCode(int width,int height,int fontSize){
this.width = width;
this.height = height;
this.fontSize = fontSize;
}
/**
*
* @param width 验证码宽度
* @param height 验证码高度
* @param fontSize 字体大小像素
* @param sessionKey 存储session中的key值
*/
public ValidateCode(int width,int height,int fontSize,String sessionKey){
this.width = width;
this.height = height;
this.fontSize = fontSize;
this.sessionKey = sessionKey;
}
/**
*
* @param width 验证码宽度
* @param height 验证码高度
* @param codeCount 验证码个数
* @param fontSize 字体大小像素
* @param sessionKey 存储session中的key值
*/
public ValidateCode(int width,int height,int codeCount,int fontSize,String sessionKey){
this.width = width;
this.height = height;
this.codeCount = codeCount;
this.fontSize = fontSize;
this.sessionKey = sessionKey;
}
/**
*
* @param width 验证码宽度
* @param height 验证码高度
* @param codeCount 验证码个数
* @param lineCount 混淆线个数
* @param fontSize 字体大小像素
* @param sessionKey 存储session中的key值
*/
public ValidateCode(int width,int height,int codeCount,int lineCount,int fontSize,String sessionKey){
this.width = width;
this.height = height;
this.codeCount = codeCount;
this.lineCount = lineCount;
this.fontSize = fontSize;
this.sessionKey = sessionKey;
}
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
*/
public void getCode(HttpServletRequest request,HttpServletResponse response){
//定义随机数类
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.gray);
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++) {
int _R = (int)Math.floor(Math.random()*256);
int _G = (int)Math.floor(Math.random()*256);
int _B = (int)Math.floor(Math.random()*256);
g.setColor(new Color(_R, _G, _B, 255));
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,fontSize));
for (int i = 0; i < codeCount; i++) {
char c = codeSequence[r.nextInt(codeSequence.length)];
builderCode.append(c);
g.drawString(c+"",((width/codeCount)*i+2),height*4/5);
}
try {
//5.输出到屏幕
ServletOutputStream sos = response.getOutputStream();
ImageIO.write(buffImg,"png",sos);
//6.保存到session中
HttpSession session = request.getSession();
session.setAttribute(""+sessionKey+"",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();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 生成验证码
* @param time
* @param request
* @param response
* @return
*/
@RequestMapping(value="getCode")
public void getCode(@RequestParam(value = "time") String time,HttpServletRequest request,HttpServletResponse response){
ValidateCode code = new ValidateCode(100,30,4,30,25,"validateCode");
code.getCode(request, response);
}
最后在页面中的使用
<div class="authCode">
<input id="coldValue" name="coldValue" type="text" placeholder="验证码" >
<img id="validateCode" class="authCodeImg" src="./getCode.action?time=<%=new Date().getTime() %>"
title="看不清可单击图片刷新" οnclick="this.src='./getCode.action?time='+new Date().getTime();">
</div>
看看效果吧