登陆验证码实现
基于对普通登陆的加强,以及某306的傻逼验证,想着对学生管理系统的登陆加个功能验证。。。挺无聊的,就不知道这个验证有啥用。。。。。。
贴个效果图:
对。大概是这么个玩意,来看看思路:
具体步骤
1.创建图片缓冲区
2.设置图片缓冲区的宽高以及保存图片的类型
3.得到这个图片的绘制环境(拿到画笔)
4.将图片保存起来
//第一,二步:传入参数为缓冲区的高,宽,保存图片类型
//BufferedImage image = new BufferedImage(width1,height1,Buffered.TYPE_INT_RGB);
//第三部,得到绘制环境(得到画笔,用到Graphics类)
// Graphics paint = image.getGraphics();
// paint.setColor(Color.WHITE);//设置画笔颜色为白色
// paint.fillRect(start,end,width2,height2);//绘制矩形并填充,将图片缓冲区的(start,end)作为绘制图片的左上角,绘制图形宽为width2,高为height2;
// paint.setColor(Color.RED);//设置画笔颜色为红色
// paint.drawString(str,x,y);//设置需要绘制在图片上的文本,参数为字符换,字符串所在的X坐标和Y坐标
//第四步,将图片保存起来(需要用到ImageIO类)
//ImageIO.write(image,"JPEG",out); 参数为图片缓冲区,图片类型,输出流
验证码工具类:
贴代码:
VerifyUtil:
public class VerifyCode {
private int width = 200;//设置图片缓冲区的宽和高
private int height = 35;
private Random r = new Random();//产生随机数
private String[] fontNames = {"宋体","楷体","黑体","微软雅黑"};//字体随机种类哦
private Color bgColor = new Color(255,255,255);//设置图片缓冲区为被色背景
private String codes="23456789zxcvbnmasdfgjkqwertyui";//验证码的字符
private String text;//验证码字符串,用于登陆验证
/*创建图片缓冲区*/
public BufferedImage getImage() {
BufferedImage image = createImage();//调用创建图片缓冲区的方法
Graphics g = image.getGraphics(); //得到绘制环境
StringBuilder sb = new StringBuilder();//用来装载生成的验证码文本,可变,线性不安全
//循环四次 每次生成同一个字符
for (int i = 0;i < 4;i++) {
String str = randomChar()+"";//调用产生随机字符方法,随机生成一个字符
sb.append(str);
g.setFont(randomFont());//设置画笔字体(随机选择)
g.setColor(randomColor());//设置画笔颜色(随机选择)
g.drawString(str, (width/4)*i, height-5);//在图片缓冲区(width/4*i,height-5)写入文本
}
this.text = sb.toString();
drawLine(image); //调用增加干扰线方法对图片文字进行干扰
return image;
}
//返回验证码图片上的文字,需要先将字符换写入图片缓冲区才可得到text,即
//要先调用getImage()
public String getText() {
return this.text;
}
//保存图片到指定输出流
//将图片缓冲区的图片以某种格式通过输出流传出
public static void output(BufferedImage image,OutputStream out) throws IOException {
ImageIO.write(image, "JPEG", out);
}
//创建图片缓冲区方法
private BufferedImage createImage() {
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setColor(bgColor);
g.fillRect(0, 0, width, height);
return image;
}
//生成随机字符
public char randomChar() {
int index = r.nextInt(codes.length()); //产生0-codes.length()之间的随机数
char y = codes.charAt(index);
return y;
}
public Font randomFont() {
int index = r.nextInt(fontNames.length);
int style = r.nextInt(4);
int size = r.nextInt(4)+24;
return new Font(fontNames[index],style,size);
}
public Color randomColor() {
int red = r.nextInt(150);
int green = r.nextInt(150);
int blue = r.nextInt(150);
return new Color(red,green,blue);
}
public void drawLine(BufferedImage image) {
//循环三次 画三条干扰线
//先取到画笔
Graphics graphics = image.getGraphics();
for (int i=0;i<3;i++) {
int x1 = r.nextInt(width);
int y1 = r.nextInt(height);
int x2 = r.nextInt(width);
int y2 = r.nextInt(height); //设置线的起点和终点
graphics.setColor(Color.BLUE); //干扰颜色
graphics.drawLine(x1, y1, x2, y2); //画干扰线
}
}
}
servlet:
VerifyCode vc = new VerifyCode();
BufferedImage image = vc.getImage();
request.getSession().setAttribute("text", vc.getText());
VerifyCode.output(image, response.getOutputStream());
jsp:
用户名: 密码: 验证码:
wake up
你的灵魂在等待着你最尖叫的回应