实现动态验证码生成
现在网上有很多在用户登陆或注册的时候为了防止程序攻击,加入了动态验证的技术,一般是让用户输入随即生成的验证码来实现。下面是一个用Servlet生成动态验证码的一个实例,可以根据实际需要进行修改。
1、生成动态验证码的Servlet
/** 验证码图片的宽度 */
private int width = 90;
/** 验证码图片的高度 */
private int height = 30;
/** 验证码字符个数 */
private int codeCount = 4;
/** 字符间距 */
private int x = 0;
/** 字体高度 */
private int fontHeight;
private int codeY;
/** 随机码可生成字符 */
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' };
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getCodeCount() {
return codeCount;
}
public void setCodeCount(int codeCount) {
this.codeCount = codeCount;
}
@Override
public void execute(Context context) {
codeY = height - 4;
fontHeight = height - 2;
x = width / (codeCount + 1);
// 定义图像buffer
BufferedImage buffImg = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics();
// 创建一个随机数生成器类
Random random = new Random();
// 将图像填充为白色
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
// 创建字体,字体的大小应该根据图片的高度来定。
Font font = new Font("Fixedsys", Font.PLAIN | Font.BOLD, fontHeight);
// 设置字体。
g.setFont(font);
// 画边框。
g.setColor(Color.BLACK);
g.drawRect(0, 0, width - 1, height - 1);
// 随机产生160条干扰线,使图象中的认证码不易被其它程序探测到。
g.setColor(Color.pink);
for (int i = 0; i < 160; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
}
// randomCode用于保存随机产生的验证码,以便用户登录后进行验证。
StringBuffer randomCode = new StringBuffer();
int red = 0, green = 0, blue = 0;
// 随机产生codeCount数字的验证码。
for (int i = 0; i < codeCount; i++) {
// 得到随机产生的验证码数字。
String strRand = String.valueOf(codeSequence[random.nextInt(36)]);
// 产生随机的颜色分量来构造颜色值,这样输出的每位数字的颜色值都将不同。
red = random.nextInt(255);
green = random.nextInt(255);
blue = random.nextInt(255);
// 用随机产生的颜色将验证码绘制到图像中。
g.setColor(new Color(red, green, blue));
g.drawString(strRand, (i + 1) * x - 6, codeY);
// 将产生的codeCount个随机数组合在一起。
randomCode.append(strRand);
}
// 将四位数字的验证码保存到Session中。
WebContext webContext = (WebContext)context;
HttpSession session = webContext.getRequest().getSession();
session.setAttribute("randomCode", randomCode.toString());
HttpServletResponse resp = webContext.getResponse();
// 禁止图像缓存。
resp.setHeader("Pragma", "no-cache");
resp.setHeader("Cache-Control", "no-cache");
resp.setDateHeader("Expires", 0);
// 设置响应的类型格式为图片格式
resp.setContentType("image/jpeg");
// 将图像输出到Servlet输出流中。
ServletOutputStream sos = null;
try {
sos = resp.getOutputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
ImageIO.write(buffImg, "jpeg", sos);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
sos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
最后,在JSP页面中加入动态生成的图片验证码.
如:<img src="${basePath}/bankPageControl.pageflow?serviceId=CP01-00-M07" id="randomCode" onclick="javascript:changeCode();" title="单击刷新验证码"/>
src赋值为对应Servlet路径。这时新的问题来了,如果用户看不清楚这个图片怎么为呢?
对了,就是加入刷新功能.
说两种简单的方法吧:
1.直接点击图片更换验证码:
可用JS脚本,添加如下代码到JSP页面中:
对了,就是加入刷新功能.
说两种简单的方法吧:
1.直接点击图片更换验证码:
可用JS脚本,添加如下代码到JSP页面中:
/**
*动态生成验证码
*/
function changeCode(){
$("#randomCode")[0].src = "${basePath}/bankPageControl.pageflow?serviceId=CP01-00-M07&&t="+new Date().getTime();
}
2.添加文字链接,使其更换:
<a href="javascript:changeCode();">单击刷新验证码</a>
最后注意验证Session的时候需要将这个URL设置为允许访问
这时候运行JSP页面,效果出来了。