當前位置:首頁 » 網頁前端 » javaweb登陸驗證碼
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

javaweb登陸驗證碼

發布時間: 2022-12-27 12:49:17

Ⅰ javaweb,郵箱發送驗證碼後進行校驗。

簡單點,就是保存在緩存裡面,new一個map放在裡面就是了。校驗的話直接和map裡面的去比。

publicclassaaa{
publicstaticMap<String,String>map=newHashMap<String,String>();//定義一個靜態map,放驗證碼

//比如這個是生成驗證碼
publicStringCreateCheckCode(Stringname){
StringcheckCode="";
String[]arrs={"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"};
//隨機生成驗證碼、
for(inti=0;i<6;i++){
inta=(int)(Math.random()*28);
checkCode+=arrs[a];
}
//為了保證唯一性質,我們可以使用注冊的客戶的名字作為key
map.put(name,checkCode);
System.out.println(checkCode);
returncheckCode;
}
//這個可以作為控制層:比如客戶點擊驗證的連接,就會進入這個方法:來驗證驗證碼
publicbooleanverifyCheckCode(Stringname,StringcheckCode)
{
booleanflag=false;
Stringcode=map.get(name);
if(null!=code&&code.equals(checkCode)){
flag=true;
}

returnflag;

}


}

Ⅱ 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 web 項目驗證碼的刷新問題

用js換 img的src就行了啊。
function change(){
document.getElementById("CreateCheckCode").src="PictureCheckCode?"+Math.random();
}
</script>
<img id="CreateCheckCode" src="PictureCheckCode" onclick="change()" />

Ⅳ javaweb的驗證碼問題

點擊後,src屬性地址換一下,後面帶個時間戳欄位,這樣就不會緩存

Ⅳ 北大青鳥java培訓:解決WEB性能測試中的驗證碼問題

現在越來越多的網站為了安全性或是防止Spam的侵害,採用了驗證碼的校驗技術。
簡單地說,驗證碼就是在進行登錄或是內容提交的時候,頁面上會隨機出現一個人工可識別,但機器不可識別的驗證字元串(一般是採用背景、扭曲等方式產生的圖片),要求登錄或是提交內容時同時輸入這個驗證碼。
驗證碼可以有效防止對口令的刺探和所謂的網路推廣軟體帶來的大量的Spam內容,目前已經被許多Internet或是Intranet應用接受為標準的實現方式。
但對性能測試來說,這種驗證碼又帶來了很大的問題。
最突出的問題是,性能測試工具本身是自動化工具,由於這種驗證碼採用的是「防止自動化工具嘗試」的方法,因此,在錄制了腳本之後會發現,很難對腳本進行調整,以使其適應驗證碼驗證的需要。
已經不止一次有人提到這個問題,並詢問有沒有較好的解決方案。
對這個問題,我個人的看法是,基本上可以考慮從三個途徑來解決該問題:1、第一種方法,也是最容易想到的,在被測系統中暫時屏蔽驗證功能,也就是說,臨時修改應用,無論用戶輸入的是什麼驗證碼,都認為是正確的。
這種方法最容易實現,對測試結果也不會有太大的影響(當然,這種方式去掉了「驗證驗證碼」這個環節,不過這個環節本來就很難成為系統性能瓶頸)。
但這種方法有一個致命的問題:如果被測系統是一個實際已上線的系統,屏蔽驗證功能會對已經在運行的業務造成非常大的安全性的風險,因此,對於已上線的系統來說,用這種方式就不合適了;2、第二種方法,在第一種方法的基礎上稍微進行一些改進。
第一種方法帶來了很大的安全性問題,那麼我們可以考慮,不取消驗證,但在其中留一個後門,我們設定一個所謂的「萬能驗證碼」,只要用戶輸入這個「萬能驗證碼」,我們就驗證通過,否則,還是按照原先的驗證方式進行驗證。
這種方式仍然存在安全性的問題,但由於我們可以通過管理手段將「萬能驗證碼」控制在一個小的范圍內,而且只在性能測試期間保留這個小小的後門,相對第一種方法來說,在安全性方面已經有較大的改進了;3、如果安全性對應用來說真的是至關重要的,不容許有一絲一毫的閃失,那我們還可以用更進一步的方法來處理這個問題。
一般的性能測試工具(MI的LR、Seague的Silkperformer等)都能夠調用外部的DLL或是組件介面,因此,廣西電腦培訓http://www.kmbdqn.cn/建議可以考慮獲得「驗證碼驗證」部分的實現,寫一個驗證碼獲取的DLL,在測試腳本中進行調用即可。

Ⅵ JAVAWEB項目怎麼實現驗證碼

importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Graphics;
importjava.awt.image.BufferedImage;
importjava.io.IOException;
importjava.io.OutputStream;
importjava.util.Random;
importjavax.imageio.ImageIO;

publicclassCode{

//圖片的寬度。
privateintwidth=160;
//圖片的高度。
privateintheight=38;
//驗證碼字元個數
privateintcodeCount=4;
//驗證碼干擾線數
privateintlineCount=20;
//驗證碼
privateStringcode=null;
//驗證碼圖片Buffer
privateBufferedImagebuffImg=null;
Randomrandom=newRandom();

privatebooleantype=false;

publicCode(){

}

publicCode(intwidth,intheight){
this.width=width;
this.height=height;
}

publicCode(intwidth,intheight,intcodeCount){
this.width=width;
this.height=height;
this.codeCount=codeCount;
}

publicCode(intwidth,intheight,intcodeCount,intlineCount){
this.width=width;
this.height=height;
this.codeCount=codeCount;
this.lineCount=lineCount;
}

publicvoidinit(booleantype){
this.type=type;
}

//生成圖片
privatevoidcreatImage(booleantype){
intfontWidth=width/codeCount;//字體的寬度
intfontHeight=height-5;//字體的高度
intcodeY=height-8;

//圖像buffer
buffImg=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphicsg=buffImg.getGraphics();
//Graphics2Dg=buffImg.createGraphics();
//設置背景色
g.setColor(getRandColor(200,250));
g.fillRect(0,0,width,height);//設置字體
Fontfont=null;
if(!type)font=newFont("Fixedsys",Font.BOLD,fontHeight);
elsefont=getFont(fontHeight);
g.setFont(font);

//設置干擾線
for(inti=0;i<lineCount/2;i++){
intxs=random.nextInt(width);
intys=random.nextInt(height);
intxe=xs+random.nextInt(width);
intye=ys+random.nextInt(height);
g.setColor(getRandColor(1,255));
if(!type)g.drawLine(xs,ys,xe,ye);
elseshear(g,width,height,getRandColor(1,255));
}

//添加噪點
floatyawpRate=0.01f;//雜訊率
intarea=(int)(yawpRate*width*height);
for(inti=0;i<area;i++){
intx=random.nextInt(width);
inty=random.nextInt(height);

buffImg.setRGB(x,y,random.nextInt(255));
}


Stringstr1=randomStr(codeCount);//得到隨機字元
this.code=str1;
for(inti=0;i<codeCount;i++){
StringstrRand=str1.substring(i,i+1);
g.setColor(getRandColor(1,255));
//g.drawString(a,x,y);
//a為要畫出來的東西,x和y表示要畫的東西最左側字元的基線位於此圖形上下文坐標系的(x,y)位置處

g.drawString(strRand,i*fontWidth+3,codeY);
}


}

//得到隨機字元
privateStringrandomStr(intn){
Stringstr1="";//I和l不要
Stringstr2="";
intlen=str1.length()-1;
doubler;
for(inti=0;i<n;i++){
r=(Math.random())*len;
str2=str2+str1.charAt((int)r);
}
returnstr2;
}

//得到隨機顏色
privateColorgetRandColor(intfc,intbc){//給定范圍獲得隨機顏色
if(fc>255)
fc=255;
if(bc>255)
bc=255;
intr=fc+random.nextInt(bc-fc);
intg=fc+random.nextInt(bc-fc);
intb=fc+random.nextInt(bc-fc);
returnnewColor(r,g,b);
}

/**
*產生隨機字體
*/
privateFontgetFont(intsize){
Randomrandom=newRandom();
Fontfont[]=newFont[5];
font[0]=newFont("Ravie",Font.PLAIN,size);
font[1]=newFont("AntiqueOliveCompact",Font.PLAIN,size);
font[2]=newFont("Fixedsys",Font.PLAIN,size);
font[3]=newFont("WideLatin",Font.PLAIN,size);
font[4]=newFont("GillSansUltraBold",Font.PLAIN,size);
returnfont[random.nextInt(5)];
}

//扭曲方法
privatevoidshear(Graphicsg,intw1,inth1,Colorcolor){
shearX(g,w1,h1,color);
shearY(g,w1,h1,color);
}

privatevoidshearX(Graphicsg,intw1,inth1,Colorcolor){

intperiod=random.nextInt(2);

booleanborderGap=true;
intframes=1;
intphase=random.nextInt(2);

for(inti=0;i<h1;i++){
doubled=(double)(period>>1)
*Math.sin((double)i/(double)period
+(6.2831853071795862D*(double)phase)
/(double)frames);
g.Area(0,i,w1,1,(int)d,0);
if(borderGap){
g.setColor(color);
g.drawLine((int)d,i,0,i);
g.drawLine((int)d+w1,i,w1,i);
}
}

}

privatevoidshearY(Graphicsg,intw1,inth1,Colorcolor){

intperiod=random.nextInt(40)+10;//50;

booleanborderGap=true;
intframes=20;
intphase=7;
for(inti=0;i<w1;i++){
doubled=(double)(period>>1)
*Math.sin((double)i/(double)period
+(6.2831853071795862D*(double)phase)
/(double)frames);
g.Area(i,0,1,h1,0,(int)d);
if(borderGap){
g.setColor(color);
g.drawLine(i,(int)d,i,0);
g.drawLine(i,(int)d+h1,i,h1);
}

}

}publicvoidwrite(OutputStreamsos)throwsIOException{
if(buffImg==null)creatImage(type);
ImageIO.write(buffImg,"png",sos);
//JPEGImageEncoderencoder=JPEGCodec.createJPEGEncoder(sos);
//encoder.encode(buffImg);
sos.close();
}

publicBufferedImagegetBuffImg(){
if(buffImg==null)creatImage(type);
returnbuffImg;
}

publicStringgetCode(){
returncode.toLowerCase();
}

//使用方法
/*publicvoidgetCode3(HttpServletRequestreq,HttpServletResponseresponse,HttpSessionsession)throwsIOException{
//設置響應的類型格式為圖片格式
response.setContentType("image/jpeg");
//禁止圖像緩存。
response.setHeader("Pragma","no-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires",0);


CreateImageCodevCode=newCreateImageCode(100,30,5,10);
session.setAttribute("code",vCode.getCode());
vCode.write(response.getOutputStream());
response.flushBuffer();
}*/

}

Ⅶ javaweb登錄三次失敗驗證碼怎麼實現

使用redis存儲,存儲當前登錄的用戶名與登錄失敗次數,超過三次就顯示驗證碼
session和cookie都不建議使用,建議服務端開啟驗證,瀏覽器端都可以繞過這些檢查

Ⅷ java web 驗證碼生成後一般在什麼地方保存這個驗證碼存到資料庫還是怎麼地

說起驗證碼,關系它的是安全性,再聯繫到你的時效性,這就非session(本身是客戶端的唯一使用伺服器資源的憑證,而且是有時效限制的,用戶長期未訪問伺服器,這個session是就會被主動注銷掉)不能當此大任了,可用性不在話下,至於你說的加密這是畫蛇添足了。

依樓上所言,未免誤人,伺服器端是必須保存這個驗證碼的,就像我給你了一個驗證碼,你可以使用,我自己也得備份一下和你比對,不然那不就變成了:你說你的驗證碼是對的,你已經驗證過了,我就信任你了,那就等於沒有密碼,客戶端的一切安全認證都是不足為信的!

Ⅸ Java Web程序中的驗證碼為什麼顯示不出來,只是一個X狀

src="<%basePath%>CheckCodeServlet" 如果還是不行可將圖片輸出到文件,看看能不能生成圖片文件