验证码生成有两种形式,一种是保存一定的验证码图片在数据库中然后把相应的验证码也保存起来。通过查询数据库来确定用户输入的验证码是否正确。但是这种方式有很大的问题,其一就是验证码图片数量过少很容易就会发生重复,其二是每次验证都要查询数据库影响性能。
所以我采用了第二种方式利用java画笔画出一个验证码图片出来。这样的好处是验证码基本每次都不一样,而且生成的验证码是放在session中的,验证起来也比较省时省力。推荐大家也使用这种方式。
下面就开始上代码介绍这种机制。
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
public class CodeUtil {
private HttpServletRequest request;
private HttpServletResponse response;
private int imgWidth = 0;//验证码图片的宽度
private int imgHeight = 0;//验证码图片的高度
private int codeCount = 0;//验证码的个数
private int x = 0;
private int fontHeight;
private int codeY;
private String fontStyle;
private static final long serialVersionUID = 128554012633034503L;
public CodeUtil(){
init();
try {
response = ServletActionContext.getResponse();
request = ServletActionContext.getRequest();
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 初始化配置参数
*/
public void init(){
// 宽度
String strWidth = "60";
// 高度
String strHeight ="20";
// 字符个数
String strCodeCount = "4";
fontStyle = "Times New Roman";
// 将配置的信息转换成数值
try {
if (strWidth != null && strWidth.length() != 0) {
imgWidth = Integer.parseInt(strWidth);
}
if (strHeight != null && strHeight.length() != 0) {
imgHeight = Integer.parseInt(strHeight);
}
if (strCodeCount != null && strCodeCount.length() != 0) {
codeCount = Integer.parseInt(strCodeCount);
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
x = imgWidth / (codeCount + 1);
fontHeight = imgHeight - 2;
codeY = imgHeight - 12;
}
/**
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void processRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("image/jpg");
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
HttpSession session = request.getSession();
// 在内存中创建图象
BufferedImage image = new BufferedImage(imgWidth, imgHeight,
BufferedImage.TYPE_INT_RGB);
// 获取图形上下文
Graphics2D g = image.createGraphics();
// 生成随机类
Random random = new Random();
// 设定背景色
g.setColor(Color.WHITE);
g.fillRect(0, 0, imgWidth, imgHeight);
// 设定字体
g.setFont(new Font(fontStyle, Font.PLAIN + Font.ITALIC, fontHeight));
// 画边框
g.setColor(new Color(55, 55, 12));
g.drawRect(0, 0, imgWidth - 1, imgHeight - 1);
// 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
g.setColor(getRandColor(160, 200));
// for (int i = 0; i < 160; i++) {
// int x = random.nextInt(imgWidth);
// int y = random.nextInt(imgHeight);
// int xl = random.nextInt(12);
// int yl = random.nextInt(12);
// g.drawLine(x, y, x + xl, y + yl);
// }
//
// 取随机产生的认证码(4位数字)
String sRand = "";
int red = 0, green = 0, blue = 0;
for (int i = 0; i < codeCount; i++) {
red = random.nextInt(255);
green = random.nextInt(255);
blue = random.nextInt(255);
int wordType = random.nextInt(3);
char retWord = 0;
switch (wordType) {
case 0:
retWord = this.getSingleNumberChar();
break;
case 1:
retWord = this.getLowerOrUpperChar(0);
break;
case 2:
retWord = this.getLowerOrUpperChar(1);
break;
}
sRand += String.valueOf(retWord);
g.setColor(new Color(red, green, blue));
g.drawString(String.valueOf(retWord), (i) * x+5, codeY+7);//这个地方非常重要,关系到验证码是否居中显示。要根据大小进行调整。
}
// 将认证码存入SESSION
sRand=sRand.toLowerCase();
session.setAttribute("rand", sRand);
// 图象生效
g.dispose();
ServletOutputStream responseOutputStream = response.getOutputStream();
// 输出图象到页面
ImageIO.write(image, "JPG", responseOutputStream);
//
// // 以下关闭输入流!
responseOutputStream.flush();
responseOutputStream.close();
}
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);
}
private char getSingleNumberChar() {
Random random = new Random();
int numberResult = random.nextInt(10);
int ret = numberResult + 48;
return (char) ret;
}
private char getLowerOrUpperChar(int upper) {
Random random = new Random();
int numberResult = random.nextInt(26);
int ret = 0;
if (upper == 0) {// 小写
ret = numberResult + 97;
} else if (upper == 1) {// 大写
ret = numberResult + 65;
}
return (char) ret;
}
public void execute(){//程序执行的路口
try {
processRequest(request,response);
} catch (Exception e) {
e.printStackTrace();
}
}
}
我这里用的是Struts2实现的,大家也可以用 Servlet实现。Demo下载地址点击打开链接
到这验证码的功能就实现了,这里的验证码生成的是图片的格式所以可以直接放在img的src中就可以访问到了。