當前位置:首頁 » 編程語言 » 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-17 03:13:05 瀏覽:542
html文件上傳表單 發布:2024-09-17 03:08:02 瀏覽:783
聊天軟體編程 發布:2024-09-17 03:00:07 瀏覽:725
linuxoracle安裝路徑 發布:2024-09-17 01:57:29 瀏覽:688
兩個安卓手機照片怎麼同步 發布:2024-09-17 01:51:53 瀏覽:207
cf編譯後沒有黑框跳出來 發布:2024-09-17 01:46:54 瀏覽:249
安卓怎麼禁用應用讀取列表 發布:2024-09-17 01:46:45 瀏覽:524
win10設密碼在哪裡 發布:2024-09-17 01:33:32 瀏覽:662
情逢敵手迅雷下載ftp 發布:2024-09-17 01:32:35 瀏覽:337
安卓如何讓軟體按照步驟自動運行 發布:2024-09-17 01:28:27 瀏覽:197