當前位置:首頁 » 編程語言 » java生成驗證碼

java生成驗證碼

發布時間: 2025-08-24 18:30:03

『壹』 怎樣用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

『貳』 java怎麼生成驗證碼圖片

原理,在servlet中隨機生成一個4位數字1000-9999
然後把這數字寫入session
輸出一個圖片,上面寫有這四個數字
在伺服器端根據用戶輸入的數字和
session中的值比較。
注意比較結束後要清空session中的值

有很多人問到驗證碼的作用。
我作個簡單的解釋。
驗證碼的作用:有效防止這種問題對某一個特定注冊用戶用特定程序暴力破解方式進行不斷的登陸嘗試,實際上是用驗證碼是現在很多網站通行的方式(比如招商銀行的網上個人銀行,騰訊的QQ社區),我們利用比較簡易的方式實現了這個功能。雖然登陸麻煩一點,但是對社區還來說這個功能還是很有必要,也很重要。但我們還是提醒大家主要保護自己的密碼,盡量使用混雜了數字、字母、符號在內的6位以上密碼,不要使用諸如1234之類的簡單密碼或者與用戶名相同、類似的密碼。
還有就是注冊時發表文章時加驗證碼,就防人用注冊機,無限的自動注冊,

package com.schoolwx.util;
import java.io.*;
import java.util.*;
import com.sun.image.codec.jpeg.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.awt.*;
import java.awt.image.*;

public class getImg extends HttpServlet {
private Font mFont=new Font("宋體", Font.PLAIN,12);//設置字體
//處理post
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException {
doGet(request,response);
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException {
//取得一個1000-9999的隨機數
String s="";
int intCount=0;
intCount=(new Random()).nextInt(9999);//
if(intCount<1000)intCount+=1000;
s=intCount+"";
//對session付值。
HttpSession session=request.getSession (true);
session.setAttribute("getImg",s);
response.setContentType("image/gif");
ServletOutputStream out=response.getOutputStream();
BufferedImage image=new BufferedImage(35,14,BufferedImage.TYPE_INT_RGB);
Graphics gra=image.getGraphics();
//設置背景色
gra.setColor(Color.yellow);
gra.fillRect(1,1,33,12);
//設置字體色
gra.setColor(Color.black);
gra.setFont(mFont);
//輸出數字
char c;
for(int i=0;i<4;i++) {
c=s.charAt(i);
gra.drawString(c+"",i*7+4,11); //7為寬度,11為上下高度位置
}
JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
}
}

『叄』 用java怎麼製作驗證碼

驗證方法很多
蠢一點的後台寫代碼,或者前台頁面加js
當然你用框架自帶的也行,例如struts的

『肆』 用java生成6位驗證碼,要求字母大小寫數字組成,不能重復字元

表達式轉換類
${expression}計算expression並輸出
#{ expression }數字計算#{ expression ;format}安格式輸出數字format為M和m
M表示小數點後最多的位數,m表示小數點後最少的位數如#{121.2322;m2M2}輸出121.23 !

『伍』 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));
}
}

熱點內容
我的世界伺服器游戲嘉年華ip 發布:2025-08-24 23:50:22 瀏覽:213
oracle資料庫的優化 發布:2025-08-24 23:42:41 瀏覽:973
python字元串空 發布:2025-08-24 23:19:27 瀏覽:333
馬來溯源碼 發布:2025-08-24 22:44:57 瀏覽:766
伺服器雙電源注意什麼 發布:2025-08-24 22:44:48 瀏覽:10
c語言在字元串中查找 發布:2025-08-24 22:27:23 瀏覽:625
聯通光貓ftp沒有usb介面 發布:2025-08-24 22:12:47 瀏覽:761
python讀取exif 發布:2025-08-24 22:11:25 瀏覽:542
左端演算法 發布:2025-08-24 21:53:26 瀏覽:530
安卓系統怎麼編譯環境 發布:2025-08-24 21:53:24 瀏覽:785