日期:2014-05-16  浏览次数:20374 次

JSP中实现随机数字验证码的生成
有时候在我们的网络应用中,防止程序自动登录搞破坏,我们一般都会加上验证码,这些验证码一般来说都是由人来识别的,当然,如果验 证码很有规律,或者说很清楚,漂亮,那么也是可能被程序识别的,我以前就识别过某网站的验证码,因为比较有规律,所以被识别了,并且识别率达到99%左右,其实我们可以制作很复杂一点的验证码,添加一些干扰的线条或者字体变形,使程序识别的难度加大,这样,我们的目的也就达到了.

下面是生成的图片:


代码如下,JSP代码
<%@page contentType="image/jpeg"%>
<%@page pageEncoding="UTF-8"%>
<%@ page import="java.awt.*,javax.imageio.*,java.io.*,java.util.*,java.awt.image.*" %>
<%--
The taglib directive below imports the JSTL library. If you uncomment it,
you must also add the JSTL library to the project. The Add Library action
on Libraries node in Projects view can be used to add the JSTL 1.1 library.
--%>
<%--
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
--%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%!String s="";%>
<%
java.util.List<String> fonts=new ArrayList<String>();
GraphicsEnvironment.getLocalGraphicsEnvironment().preferLocaleFonts();
String[] names=GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(Locale.CHINA);
for(String s:names){
char c=s.charAt(0);
if(Character.isLowerCase(c)||Character.isUpperCase(c)){

}else{
fonts.add(s);
}
}
BufferedImage bi=new BufferedImage(200,50,BufferedImage.TYPE_INT_RGB);
Graphics2D g=bi.createGraphics();
char[] cs={'0','1','2','3','4','5','6','7','8','9'};
char[] use=new char[4];
g.setColor(new Color(240,240,240));
g.fillRect(0,0,200,50);
for(int i=0;i<4;i++){
Point p=new Point(5+(i*((int)(Math.random()*10)+40)),40);
int size=0;
int[] sizes=new int[20];
for(int j=0;j<20;j++){
sizes[j]=30+j;
}
size=sizes[(int)(Math.random()*sizes.length)];
int face=0;
if(Math.random()*10>5){
face=Font.BOLD;
}else{
face=Font.ITALIC;
}
use[i]=cs[(int)(Math.random()*cs.length)];
g.setPaint(new GradientPaint(p.x,p.y,new Color((int)(Math.random()*256),0,(int)(Math.random()*256)),
p.x,p.y-size,new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256))));
g.setFont(new Font(fonts.get((int)(Math.random()*fonts.size())),face,size));
g.drawString(""+use[i],p.x,p.y);
}
s=new String(use);
session.setAttribute("code", s);
g.setPaint(null);
for(int i=0;i<4;i++){
g.setColor(new Color((int)(Math.random()*0x00FFFFFFF)));
g.drawLine((int)(Math.random()*200),(int)(Math.random()*50),(int)(Math.random()*200),(int)(Math.random()*50));
}
Random random = new Random();
for (int i=0;i<88;i++) {
int x = random.nextInt(200);
int y = random.nextInt(50);
g.setColor(new Color((int)(Math.random()*0x00FFFFFFF)));
g.setStroke(new BasicStroke((float)(Math.random()*3)));
g.drawLine(x,y,x,y);
}
OutputStream ot=response.getOutputStream();
ImageIO.write(bi,"JPEG",ot);
g.dispose();
ot.close();
%>

以下是Servlet代码
/*
* Code.java
*
* Created on 2007年9月21日, 下午12:08
*/

package com.hadeslee;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.Paint;
import java.awt.Point;
import java.awt.Stroke;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import javax.imageio.ImageIO;

import javax.servlet.*;
imp