|
Posted on 2009-05-14 23:02 Gavin.lee 阅读(1155) 评论(0) 编辑 收藏 所属分类: web 积累(前端 + 后台)
这是我以前做BBS时候用的一个验证码机制。
register.jsp + RegistServlet.java +web.xml + RandomValidateCode.java
register.jsp 页面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<base href="<%=basePath% />"/>
<title>注册首页面</title>
</style>
</head>
<body>
<form action="register.do" name="registerForm">
<table border="0" width="798" height="146">
<tr>
<td > 验证码:</td>
<td ><input type="text" size="20" name="validate" />
<td ><img src="validate.do" onclick="javascript:this.src='validate.do?ran='+Math.random()" alt="" />(如果看不清请点击图片进行更换)</td>
</tr>
</table>
<img src="images/button/register.jpg" type="submit" style="cursor:pointer" />
</form>
</body>
</html>
RegistServlet.java:
package com.handson.bbs.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.handson.commons.util.RandomValidateCode;
public class RegisterServlet extends BaseServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String validate = request.getParameter("validate");
String sessionValidate = RandomValidateCode.getValidateCodeFromSession(request.getSession());
if(!validate.equals(sessionValidate)){
request.setAttribute("error", "请正确输入验证码!");
this.forward(request, response, "/register.jsp");
return;
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
web.xml 配置:
<servlet>
<servlet-name>validateCodeServlet</servlet-name>
<servlet-class>com.handson.commons.util.ValidateCodeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>validateCodeServlet</servlet-name>
<url-pattern>/validate.do</url-pattern>
</servlet-mapping>
RandomValidateCode.java:
package com.handson.commons.util;
import java.util.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
import javax.servlet.http.*;
/**
* @descripte create validate code
* @author Gavin.lee
* @date 2009-5-14 22:58
*
*/
public class RandomValidateCode {
public static final String KEY_VALIDATE_CODE = "validate_code";
public static final int DIGITAL_VALIDATE_CODE = 1; //数字型验证码
public static final int LETTER_VALIDATE_CODE = 2; //字母型验证码
public static final int MIX_VALIDATE_CODE = 3; //数字和字母混合型验证码
private static final String PIC_FORMAT = "JPEG";
private int numberCount = 4;
private BufferedImage image;
private int width = 72;
private int height= 24;
private String validateCode;
private Font font = new Font("Times New Roman",Font.PLAIN,20);
private int validateCodeType = DIGITAL_VALIDATE_CODE;
public RandomValidateCode() {
this(DIGITAL_VALIDATE_CODE);
}
public RandomValidateCode(int validateCodeType) {
if(validateCodeType == DIGITAL_VALIDATE_CODE
|| validateCodeType == MIX_VALIDATE_CODE
|| validateCodeType == LETTER_VALIDATE_CODE) {
this.validateCodeType = validateCodeType;
}
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
generateImage();
}
/**
* 给定范围获得随机颜色
* @param colorFrom
* @param colorEnd
* @return
*/
private Color generateRandomColor(int colorFrom, int colorEnd) {
Random random = new Random();
colorFrom = colorFrom > 255 ? 255 : colorFrom;
colorEnd = colorEnd > 255 ? 255 : colorEnd;
int red = colorFrom + random.nextInt(colorEnd - colorFrom);
int green = colorFrom + random.nextInt(colorEnd - colorFrom);
int blue = colorFrom + random.nextInt(colorEnd - colorFrom);
return new Color(red, green, blue);
}
private void generateImage() {
Graphics g = image.getGraphics(); //获取图形上下文
Random random = new Random();
g.setFont(font);
drawBackground(g, random);
StringBuffer strBuf = new StringBuffer();
int count = 0;
while(count < numberCount) {
int randNum = random.nextInt((int)'z');
if(this.validateCodeType == MIX_VALIDATE_CODE && ! Character.isLetterOrDigit(randNum)) {
continue;
} else if(this.validateCodeType == DIGITAL_VALIDATE_CODE && ! Character.isDigit(randNum)) { //只显示数字验证码
continue;
} else if(this.validateCodeType == LETTER_VALIDATE_CODE && ! Character.isLetter(randNum)) { //只显示数字验证码
continue;
}
// 将认证码显示到图象中
String randChar = String.valueOf((char)randNum);
g.setColor(new Color(30+random.nextInt(110), 30+random.nextInt(110), 30+random.nextInt(110)));
g.drawString(randChar, 16*count+4, 18);
strBuf.append(randChar);
count++;
}
validateCode = strBuf.toString();
g.dispose();//释放图形相关资源
}
private void drawBackground(Graphics g, Random random) {
//设定背景色
g.setColor(generateRandomColor(210, 250));
g.fillRect(0, 0, width, height);
g.setColor(new Color(255, 255, 255));
g.drawRect(0, 0, width-1, height-1); //画边框
//随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
g.setColor(generateRandomColor(160,200));
for(int i = 0; i < 155; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(16);
int yl = random.nextInt(16);
g.drawLine(x,y,x+xl,y+yl);
}
}
public void outputImage(OutputStream outputStream) {
if(outputStream == null)
return;
try {
ImageIO.write(image, PIC_FORMAT, outputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public String getValidateCode() {
return this.validateCode;
}
public void bindingRequest(HttpServletRequest request, HttpServletResponse response) {
try {
//set the random number into session
request.getSession().setAttribute(KEY_VALIDATE_CODE, validateCode);
//set page to no cache for Web Browser
response.reset();
response.setContentType("image/*");
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
//output the image stream into response
outputImage(response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getValidateCodeFromSession(HttpSession session) {
return (String)session.getAttribute(KEY_VALIDATE_CODE);
}
public static void removeValidateCodeFromSession(HttpSession session) {
session.removeAttribute(KEY_VALIDATE_CODE);
}
}
|