導航:首頁 > 編程語言 > java登陸驗證碼

java登陸驗證碼

發布時間:2025-06-11 19:20:56

㈠ 用java怎麼製作驗證碼

原理:

1.隨機生成4個數字 用到了Random類
2.對這4個數字設置字體格式 用 setFont方法
3.改變字體顏色用setColor 然後隨機生成顏色

代碼如下
package s1;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.jms.Session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class GetImage extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

this.doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 發送圖片不能夠添加這2行代碼
// response.setContentType("text/html;charset=UTF-8");
// request.setCharacterEncoding("UTF-8");

int width=100;
int height=50;
//獲得一張圖片
BufferedImage image=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

Graphics g=image.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(1, 1, width-2, height-2);
g.setFont(new Font("宋體",Font.BOLD,30));
Random random=new Random();

/虛枯兄/ 填充的字元串
String str="";

//緩存生成的驗證碼敗橘
StringBuffer stringbuffer=new StringBuffer();

//隨機生成驗證碼的顏差襲色和字元
for(int i=0;i<4;i++)
{ //設置隨機顏色
g.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));

int index=random.nextInt(62);//這里的62就是從填充字元段中隨意選取一個位置
String str1=str.substring(index,index+1);
g.drawString(str1, 20*i, 30);//x,y數值設置太小會顯示不出來
stringbuffer.append(str1);
}

//將生成的驗證碼存到伺服器
request.getSession().setAttribute("checkcode", stringbuffer.toString());//key和value

//將圖片發送給瀏覽器
ImageIO.write(image, "jpg", response.getOutputStream());

}

}

用戶登錄界面代碼
package s1;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Login extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");// 設置伺服器發送給瀏覽器的編碼方式
request.setCharacterEncoding("UTF-8"); // 客戶端向伺服器提交的數據的解碼方式
// 獲得用戶提交的數據
String checkcode = request.getParameter("checkcode");
System.out.println(checkcode);

// 判斷輸入的驗證碼是不是符合
HttpSession session = request.getSession();// session是存放數據的地方
String str = (String) session.getAttribute("checkcode");

if (str != null) {
if (checkcode.compareToIgnoreCase(str) == 0) // 驗證碼忽略大小寫
response.getWriter().println("驗證碼輸入正確");

else
response.getWriter().println("驗證碼輸入錯誤");
}

else response.getWriter().println("驗證碼失效");

// 使用完的驗證碼信息要刪除,返回原頁面再輸一次,驗證碼就失效了
session.removeAttribute("checkcode");

}

}

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

後台寫一個生成圖片隨機的代碼,生成圖片給前台。切換圖片的時候,使用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登陸驗證碼相關的資料

熱點內容
八卦匯總421頁pdf 瀏覽:286
android應用自動升級 瀏覽:747
遠程屏幕監控源碼 瀏覽:569
雲伺服器的ip怎麼查詢 瀏覽:155
大學c語言搜題app在哪裡下載 瀏覽:109
pdf文檔被保護 瀏覽:345
有沒有電腦公司網站源碼下載 瀏覽:230
智能電視哪個app看電影好用 瀏覽:224
微信頁面源碼下載 瀏覽:957
怎麼看5代噴頭加密 瀏覽:359
linux查找文件並刪除文件 瀏覽:872
單片機里的編程軟體 瀏覽:164
鑽石投票網站源碼 瀏覽:973
cidrphp 瀏覽:882
android測試用例文檔 瀏覽:820
單片機素數 瀏覽:838
怎麼在桌面上發送文件夾 瀏覽:759
海外貸款源碼 瀏覽:717
北航單片機實驗 瀏覽:799
私有雲伺服器在哪裡 瀏覽:939