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

java登錄驗證碼

發布時間: 2025-04-23 10:32:57

⑴ 本人初學java,有什麼好的圖形驗證碼推薦嗎

如果你正在學習Java,可以使用Java的一些開源庫來生成圖形驗證碼。以下是一些常用的Java圖形驗證碼庫:

  • Kaptcha:Kaptcha是一個Java驗證碼庫,可以生成隨機的驗證碼圖片,包括數字、字母、漢字等。Kaptcha支持自定義驗證碼圖片大小、顏色、字體、干擾線等參數,並且支持多種圖片格式,如JPEG、PNG等。

  • Jcaptcha:Jcaptcha是一個Java驗證碼庫,可以生成隨機的驗證碼圖片,包括數字、字母、符號等。Jcaptcha支持自定義驗證碼圖片大小、顏色、字體、干擾線等參數,並且支持多種圖片格式,如JPEG、PNG等。

  • SimpleCaptcha:SimpleCaptcha是一個簡單的Java驗證碼庫,可以生成隨機的驗證碼圖片,包括數字、字母、符號等。SimpleCaptcha支持自定義驗證碼圖片大小、顏色、字體、干擾線等參數,並且支持多種圖片格式,如JPEG、PNG等。

  • 以上是一些常用的Java圖形驗證碼庫,它們都比較簡單易用,適合初學者使用。你可以根據自己的需求選擇一個適合自己的庫來生成驗證碼。

⑵ 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 登陸時的驗證碼怎麼做

後台寫一個生成圖片隨機的代碼,生成圖片給前台。切換圖片的時候,使用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;
}
}

⑸ 怎樣用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

熱點內容
iar編譯ram超限 發布:2025-04-29 09:53:10 瀏覽:946
存儲晶元龍頭 發布:2025-04-29 09:26:11 瀏覽:276
資料庫線 發布:2025-04-29 09:19:44 瀏覽:75
linux命令使用 發布:2025-04-29 09:00:22 瀏覽:681
安卓手機選什麼手機 發布:2025-04-29 08:50:10 瀏覽:214
安卓解壓者 發布:2025-04-29 08:49:59 瀏覽:338
找不到system文件夾 發布:2025-04-29 08:45:00 瀏覽:832
外貿企業用什麼伺服器 發布:2025-04-29 08:33:45 瀏覽:828
linuxiperf 發布:2025-04-29 08:09:43 瀏覽:258
路由器密碼在哪裡能看見 發布:2025-04-29 07:34:25 瀏覽:856