当前位置:首页 » 编程语言 » java数字验证码

java数字验证码

发布时间: 2024-08-23 16:21:30

1. 怎样用java实现验证码

现在许多系统的注册 登录或者发布信息模块都添加的随机验证码功能 就是为了避免自动注册程序或者自动发布程序的使用

验证码实际上就是随机选择一些字符以图片的形式展现在页面上 如果进行提交操作的同时需要将图片上的字符同时提交 如果提交的字符与服务器session保存的不同 则认为提交信息无效 为了避免自动程序分析解析图片 通常会在图片上随机生成一些干扰线或者将字符进行扭曲 增加自动识别验证码的难度

在这里 我们使用java实现验证码

<%@ page contentType= image/jpeg import= java awt * java awt image * java util * javax imageio * %>

<%!

Color getRandColor(int fc int bc){//给定范围获得随机颜色

Random random = new Random();

if(fc> ) fc= ;

if(bc> ) bc= ;

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);

}

%>

<%

//设置页面不缓存

response setHeader( Pragma No cache );

response setHeader( Cache Control no cache );

response setDateHeader( Expires );

// 在内存中创建图象

int width= height= ;

BufferedImage image = new BufferedImage(width height BufferedImage TYPE_INT_RGB);

// 获取图形上下文

Graphics g = image getGraphics();

//生成随机类

Random random = new Random();

// 设定背景色

g setColor(getRandColor( ));

g fillRect( width height);

//设定字体

g setFont(new Font( Times New Roman Font PLAIN ));

// 随机产生 条干扰线 使图象中的认证码不易被其它程序探测到

g setColor(getRandColor( ));

for (int i= ;i< ;i++)

{

int x = random nextInt(width);

int y = random nextInt(height);

int xl = random nextInt( );

int yl = random nextInt( );

g drawLine(x y x+xl y+yl);

}

// 取随机产生的认证码( 位数字)

String codeList = ;

String sRand= ;

for (int i= ;i< ;i++){

int a=random nextInt(codeList length() );

String rand=codeList substring(a a+ );

sRand+=rand;

// 将认证码显示到图象中

g setColor(new Color( +random nextInt( ) +random nextInt( ) +random nextInt( )));//调用函数出来的颜色相同 可能是因为种子太接近 所以只能直接生成

g drawString(rand *i+ );

}

// 将认证码存入SESSION

session setAttribute( rand sRand);

// 图象生效

g dispose();

// 输出图象到页面

ImageIO write(image JPEG response getOutputStream());

out clear();

out = pageContext pushBody();

lishixin/Article/program/Java/hx/201311/25536

2. java怎么实现随机4个带有数字和字母的验证码

importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Graphics;
importjava.awt.image.BufferedImage;
importjava.util.Random;

importjavax.imageio.ImageIO;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjavax.servlet.http.HttpSession;

publicclassRandomValidateCode{

="RANDOMVALIDATECODEKEY";//放到session中的key
privateRandomrandom=newRandom();
privateStringrandString="";//随机产生的字符串

privateintwidth=80;//图片宽
privateintheight=26;//图片高
privateintlineSize=40;//干扰线数量
privateintstringNum=4;//随机产生字符数量
/*
*获得字体
*/
privateFontgetFont(){
returnnewFont("Fixedsys",Font.CENTER_BASELINE,18);
}
/*
*获得颜色
*/
privateColorgetRandColor(intfc,intbc){
if(fc>255)
fc=255;
if(bc>255)
bc=255;
intr=fc+random.nextInt(bc-fc-16);
intg=fc+random.nextInt(bc-fc-14);
intb=fc+random.nextInt(bc-fc-18);
returnnewColor(r,g,b);
}
/**
*生成随机图片
*/
publicvoidgetRandcode(HttpServletRequestrequest,
HttpServletResponseresponse){
HttpSessionsession=request.getSession();
//BufferedImage类是具有缓冲区的Image类,Image类是用于描述图像信息的类
BufferedImageimage=newBufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
Graphicsg=image.getGraphics();//产生Image对象的Graphics对象,改对象可以在图像上进行各种绘制操作
g.fillRect(0,0,width,height);
g.setFont(newFont("TimesNewRoman",Font.ROMAN_BASELINE,18));
g.setColor(getRandColor(110,133));
//绘制干扰线
for(inti=0;i<=lineSize;i++){
drowLine(g);
}
//绘制随机字符
StringrandomString="";
for(inti=1;i<=stringNum;i++){
randomString=drowString(g,randomString,i);
}
session.removeAttribute(RANDOMCODEKEY);
session.setAttribute(RANDOMCODEKEY,randomString);
System.out.println(randomString);
g.dispose();
try{
ImageIO.write(image,"JPEG",response.getOutputStream());//将内存中的图片通过流动形式输出到客户端
}catch(Exceptione){
e.printStackTrace();
}
}
/*
*绘制字符串
*/
privateStringdrowString(Graphicsg,StringrandomString,inti){
g.setFont(getFont());
g.setColor(newColor(random.nextInt(101),random.nextInt(111),random.nextInt(121)));
Stringrand=String.valueOf(getRandomString(random.nextInt(randString.length())));
randomString+=rand;
g.translate(random.nextInt(3),random.nextInt(3));
g.drawString(rand,13*i,16);
returnrandomString;
}
/*
*绘制干扰线
*/
privatevoiddrowLine(Graphicsg){
intx=random.nextInt(width);
inty=random.nextInt(height);
intxl=random.nextInt(13);
intyl=random.nextInt(15);
g.drawLine(x,y,x+xl,y+yl);
}
/*
*获取随机的字符
*/
publicStringgetRandomString(intnum){
returnString.valueOf(randString.charAt(num));
}
}

3. 用java怎么制作验证码

验证方法很多
蠢一点的后台写代码,或者前台页面加js
当然你用框架自带的也行,例如struts的

4. 用java生成6位验证码,要求字母大小写数字组成,不能重复字符

表达式转换类
${expression}计算expression并输出
#{ expression }数字计算#{ expression ;format}安格式输出数字format为M和m
M表示小数点后最多的位数,m表示小数点后最少的位数如#{121.2322;m2M2}输出121.23 !

5. java 登陆时的验证码怎么做

后台写一个生成图片随机的代码,生成图片给前台。切换图片的时候,使用ajax获取图片数据就行。
附上生成图片的代码
public class ValidateCode {

private int width=180;
private int height=60;
private int codeCount = 4;
private int x = 0;
private int codeY;
private String Code;
private BufferedImage buffImg;
static char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z','a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
'x', 'y', 'z', 'o', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
private int fontHeight;

public ValidateCode() {
x = width / (codeCount + 2);
fontHeight = height - 2;
codeY = height - 4;
CreateCode();
}

public void CreateCode(){

// 定义图像buffer
BufferedImage buffImg = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics();
// 创建一个随机数生成器类
Random random = new Random();

// 将图像填充为白色
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);

// 创建字体,字体的大小应该根据图片的高度来定。
Font font = new Font("Fixedsys", Font.PLAIN, fontHeight);
// 设置字体。
g.setFont(font);

// 画边框。
g.setColor(Color.BLACK);
g.drawRect(0, 0, width - 1, height - 1);

// randomCode用于保存随机产生的验证码,以便用户登录后进行验证。
StringBuffer randomCode = new StringBuffer();
int red = 0, green = 0, blue = 0;

// 随机产生codeCount数字的验证码。
for (int i = 0; i < codeCount; i++) {
// 得到随机产生的验证码数字。
String strRand = String.valueOf(codeSequence[random.nextInt(62)]);
// 产生随机的颜色分量来构造颜色值,这样输出的每位数字的颜色值都将不同。
red = random.nextInt(255);
green = random.nextInt(255);
blue = random.nextInt(255);

// 用随机产生的颜色将验证码绘制到图像中。
g.setColor(new Color(red, green, blue));
g.drawString(strRand, (i ) * x+20, codeY);

// 将产生的四个随机数组合在一起。
randomCode.append(strRand);
}
this.Code=randomCode.toString().toUpperCase();
this.buffImg=buffImg;

}

public String getCode() {
return Code;
}

public void setCode(String code) {
Code = code;
}

public BufferedImage getBuffImg() {
return buffImg;
}

public void setBuffImg(BufferedImage buffImg) {
this.buffImg = buffImg;
}
}

热点内容
密码子的原料是什么 发布:2024-09-19 09:11:42 浏览:347
半夜编程 发布:2024-09-19 09:11:36 浏览:103
海康威视存储卡质量如何 发布:2024-09-19 08:55:35 浏览:939
python3默认安装路径 发布:2024-09-19 08:50:22 浏览:516
环卫视频拍摄脚本 发布:2024-09-19 08:35:44 浏览:418
sqlserveronlinux 发布:2024-09-19 08:16:54 浏览:256
编程常数 发布:2024-09-19 08:06:36 浏览:952
甘肃高性能边缘计算服务器云空间 发布:2024-09-19 08:06:26 浏览:162
win7家庭版ftp 发布:2024-09-19 07:59:06 浏览:717
数据库的优化都有哪些方法 发布:2024-09-19 07:44:43 浏览:269