您的位置:首页 > 其它

【学习笔记⑦】登录页面怎样实现验证码登录功能

2013-11-22 18:44 771 查看
用户登录时,有需要填写验证码的需求,而怎样用java代码实现的呢,以下是实现该工能的代码.

1.验证码随机生成方法,方法封装在RandomPic.java文件中,代码如下:

package com.qidian.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;

public class RandomPic
{
private final int width=60;
private final int height=20;

private final int interfereLineNum = 88;
private final int interferePointNum = 88;
private final int interfereLineLenMax = 12;
private final int authenticationCodeNumber = 4;

private final int backgroundColorBegin =200;
private final int backgroundColorFinal =250;

private final int interfereColorBegin =160;
private final int interfereColorFinal =200;

private final int radomNumberColorBegin =20;
private final int radomNumberColorFinal =130;

private final String fontName="Times New Roman";
private final int fontStyle = Font.PLAIN;
private final int fontSize = 18;

private final int stringWidth =13;
private final int stringHight =16;
private final int stringClearance =6;

private final int rgbValue = 255;

private Graphics graphics;

public String authenticationCode=new String();
public BufferedImage image;

public RandomPic(){
init();
}

public RandomPic(String rn){
init();
authenticationCode = rn;
}

private void init(){
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
graphics = image.getGraphics();
}
public void drawServer(){

drawBasic();

drawInterfereLine();

drawRadomNumberServer();

graphics.dispose();
}
public void drawClient(){

drawBasic();

drawInterfereLine();

drawRadomNumberClient();

graphics.dispose();
}

private Color getRandColor(int fc,int bc){
Random random = new Random();
if(fc>rgbValue) fc=rgbValue;
if(bc>rgbValue) bc=rgbValue;
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 void drawBasic(){

graphics.setColor(getRandColor(backgroundColorBegin,backgroundColorFinal));
graphics.fillRect(0, 0, width, height);

graphics.setFont(new Font(fontName,fontStyle,fontSize));

graphics.setColor(new Color(rgbValue,rgbValue,rgbValue));
graphics.drawRect(0,0,width-1,height-1);
}

private void drawInterfereLine(){
Random random = new Random();
graphics.setColor(getRandColor(interfereColorBegin,interfereColorFinal));
for (int i=0;i<interfereLineNum;i++){
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(interfereLineLenMax);
int yl = random.nextInt(interfereLineLenMax);
graphics.drawLine(x,y,x+xl,y+yl);
}
}

@SuppressWarnings("unused")
private void drawInterferePoint(){
Random random = new Random();
graphics.setColor(getRandColor(interfereColorBegin,interfereColorFinal));
for (int i=0;i<interferePointNum;i++){
int x = random.nextInt(width);
int y = random.nextInt(height);
graphics.drawLine(x,y,x,y);
}
}

private void drawRadomNumberServer(){
Random random = new Random();
for (int i=0;i<authenticationCodeNumber;i++){
String rand=String.valueOf(random.nextInt(10));
authenticationCode+=rand;
graphics.setColor(getRandColor(radomNumberColorBegin,radomNumberColorFinal));

graphics.drawString(rand,stringWidth*i+stringClearance,stringHight);
}
}
private void drawRadomNumberClient(){
authenticationCode = authenticationCode.substring(0,authenticationCode.indexOf("."));
switch(authenticationCode.length()){
case 1: authenticationCode = "000"+authenticationCode; break;
case 2: authenticationCode = "00"+authenticationCode; break;
case 3: authenticationCode = "0"+authenticationCode; break;
default: authenticationCode = authenticationCode.substring(0,4); break;
}
graphics.setColor(getRandColor(radomNumberColorBegin,radomNumberColorFinal));
graphics.drawString(authenticationCode,stringWidth,stringHight);
}
}

2.调用RandomPic对象,实现功能,写在单独的checkPic.jsp页面,代码如下:

<%@ page contentType="image/jpeg"
import="java.awt.image.*,
javax.imageio.*,
com.sias.qidian.education.util.RandomPic,
java.io.*"%>
<%
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
double rand = Math.random() * 10000;
RandomPic rp = new RandomPic(Double.toString(rand));
rp.drawClient();
session.setAttribute("checkPic", rp.authenticationCode);//通过session传递,然后后台接受判断是否正确
BufferedImage image = rp.image;
OutputStream output=response.getOutputStream();
try
{
ImageIO.write(image, "JPEG", output);
} catch (IOException e) {
e.printStackTrace();
}finally{
output.flush();
output.close();
output=null;
response.flushBuffer();
out.clear();
out = pageContext.pushBody();
}

%>
3.在login.jsp页面编写html代码,通过input标签实现,代码如下:

<td>  验证码:<input name="checkPic" type="text" maxLength=4 size=8>
<img border='0' src='<%=basePath %>commons/checkPic.jsp'/></td>


4.在servlet中进行登录判断,代码如下:



以上步骤是实现用户验证码验证功能。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐