验证码设计
首先,创建生成验证码类:
//需要导入的包 import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.util.Random; import javax.imageio.ImageIO; import javax.imageio.stream.ImageOutputStream;
//生成具有背景图像的验证码类
public class IdentifyCode {
private String randCode;
private ByteArrayInputStream inputStream;
//参数:随机码个数
public IdentifyingCode( int codeNum ) {
try {
createRandCode ( codeNum ) ;
} catch (Exception e) {
e.printStackTrace();
}
public String getRandCode() {
return randCode;
}
public ByteArrayInputStream getInputStream() {
return inputStream;
}
}
定义createRandCode方法:
private void createRandCode (int codeNum ) throws Exception{ //在内存中创建图象 int width = 15*codeNum;
int height = 25; //创建一个不带透明色的BufferedImage对象 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //获取图形上下文 Graphics g = image.getGraphics(); //生成随机类 Random random = new Random(); //设定背景色 g.setColor(getRandColor(200, 250)); g.fillRect(0, 0, width, height); //设定字体 g.setFont(new Font("Times New Roman", Font.PLAIN, 20)); //随机产生1000条干扰线,使图象中的认证码不易被其它程序探测到
g.setColor(getRandColor(160, 200)); //获取随机颜色
for (int i = 0; i < 1000; i++) { int x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt(14); int yl = random.nextInt(14); g.drawLine(x, y, x + xl, y + yl); } //定义数字数组 int[] number = {56,57,58,59,60,61,62,63,64,65}; //定义小写字母数组 int[] lowerAlphabet = {65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90}; //定义大字母数组 int[] upperAlphabet = {97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122}; //取随机产生的认证码(codeNum位数字) StringBuilder sRand = new StringBuilder(); for (int i = 0; i < codeNum; i++) { char rand; int nowNum = random.nextInt(3); nowNum=0; switch (nowNum) { case 0: { rand = (char) number[random.nextInt(number.length)]; break; } case 1: { rand = (char) lowerAlphabet[random.nextInt(lowerAlphabet.length)]; break; } case 2: { rand = (char) upperAlphabet[random.nextInt(upperAlphabet.length)]; break; } default: { rand =(char) lowerAlphabet[random.nextInt(lowerAlphabet.length)]; break; } } sRand.append(rand); // 将认证码显示到图象中 g.setColor(new Color(10 + random.nextInt(110), 10 + random.nextInt(110), 10 + random.nextInt(110))); g.drawString(rand+"", 13 * i + 7, 18); } g.dispose(); this.randCode=sRand.toString(); ByteArrayOutputStream output = new ByteArrayOutputStream(); ImageOutputStream imageOut = ImageIO.createImageOutputStream(output); ImageIO.write(image, "JPEG", imageOut); this.inputStream = new ByteArrayInputStream(output.toByteArray()); imageOut.close(); output.close(); }
/*
* 给定范围获得随机颜色
*/
private 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);
}
前台页面处理:
<!--加载页面的同时加载随机码-->
<body onload="loadRandCode()">
<form id="login" name="login" method="post">
<table>
<tr>
<td height="27" colspan="2" bordercolor="#666666">
<span class="style8">验证码</span>
</td>
<td width="128" valign="bottom">
<input type="text" name="randCode" id="randCode" class="input_css" tabindex="3" maxlength="4" />
</td>
<td>
<span class="STYLE6"> <span class="style7"><img id ="randCodeImg" onclick="changeValidateCode(this)"/></span></span>
</td>
</tr>
</table>
</form>
</body>
<script type="text/javascript">
/**
*获取图形验证码
*/
function changeValidateCode(obj) {
//获取当前的时间作为参数,无具体意义
var timenow = new Date().getTime();
obj.src="${pageContext.request.contextPath}/Login_rand.do?d="+timenow;
}
//第一次加载
function loadRandCode(){
changeValidateCode(document.getElementById("randCodeImg"))
}
/**
*登录验证和请求
*/
function userLogin(){
var webForm = document.getElementById("login");
webForm.action = "${pageContext.request.contextPath}/Login_login.do";
webForm.submit();}
</script>
后台验证判断处理:
public String login() { if(!randCode.equalsIgnoreCase((String)session.get("randCode"))){ request.setAttribute("error", "验证码输入错误!"); return INPUT; } }