⑴ 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 web中驗證碼代碼
生成code.jsp文件的完整代碼如下:
<%@ page language="java" contentType="image/jpeg; charset=gb2312"
pageEncoding="gb2312"%>
<%@ page import="java.awt.*,java.awt.image.*" %>
<%@ page import="java.util.*,javax.imageio.*" %>
<%!
Color getRandColor(int fc,int bc){
Random r=new Random();
if(fc>255) fc=255;
if(bc>200) bc=255;
int red=fc+r.nextInt(bc-fc);
int green=fc+r.nextInt(bc-fc);
int blue=fc+r.nextInt(bc-fc);
return new Color(red,green,blue);
}%>
<% //設置頁面不緩存
response.setHeader("Pragma","No-cache");
response.setHeader("cache-Control","no-cache");
response.setDateHeader("Expires",0);
//創建隨機類
Random r=new Random();
//在內存中創建圖像,寬度,高度
int width=80,height=30;
BufferedImage pic=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//獲取圖形上下文環境
Graphics gc=pic.getGraphics();
//設定背景顏色並進行填充
gc.setColor(getRandColor(200,250));
gc.fillRect(0,0,width,height);
//設定圖形上下文環境字體
gc.setFont(new Font("Times New Roman",Font.PLAIN,20));
//畫邊框
//gc.setColor(new Color(1));
//gc.drawRect(0,0,width-1,height-1);
//隨機產生200條干擾直線,使圖像中的認證碼不易被其他分析程序探測
gc.setColor(getRandColor(160,200));
for(int i=0;i<200;i++)
{
int x1=r.nextInt(width);
int y1=r.nextInt(height);
int x2=r.nextInt(15);
int y2=r.nextInt(15);
gc.drawLine(x1,y1,x1+x2,y1+y2);
}
//隨即產生100個干擾點
gc.setColor(getRandColor(120,240));
for(int i=1;i<100;i++){
int x=r.nextInt(width);
int y=r.nextInt(height);
gc.drawOval(x,y,0,0);
}
//隨機產生四位數字的驗證碼
String RS="";
String rn="";
for(int i=0;i<4;i++)
{
//產生十以內隨機數字
rn=String.valueOf(r.nextInt(10));
RS+=rn;
//將認證碼用drawString函數顯示到圖像里
gc.setColor(new Color(20+r.nextInt(110),20+r.nextInt(110),20+r.nextInt(110)));//使字體顏色效果明顯
gc.drawString(rn,13*i+16,16);
}
//釋放圖形上下文環境
gc.dispose();
//將認證碼RS存入session中共享
session.setAttribute("random",RS);
//輸出生成後的圖象到頁面
ImageIO.write(pic,"JPEG",response.getOutputStream());
out.clear();
out = pageContext.pushBody();
%>
使用隨即碼的jsp文件中加入下面一句即可
<image src="code.jsp">
⑶ java web中生成的驗證碼為什麼不放在session
java web中session是服務端機制,是占伺服器的內存的,一個用戶可能會提取多個驗證碼那上千個用戶差點的伺服器就高壓了,一般都是一次傳遞,沒必要用session這個貴重的東西,session是在萬不得已的情況下存儲一些重要的用戶信息。
驗證碼只需要存放在客戶端,每次重新驗證即可。
⑷ 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中如何實時判定驗證碼的正誤
你好,你先把圖片生成,然後把驗證碼放在session中,然後傳到頁面上,當用戶照著驗證碼輸入後,ajax非同步提交到後台進行校驗,跟之前放session的那個值對比就可以了
⑹ java web 驗證碼生成後一般在什麼地方保存這個驗證碼存到資料庫還是怎麼地
說起驗證碼,關系它的是安全性,再聯繫到你的時效性,這就非session(本身是客戶端的唯一使用伺服器資源的憑證,而且是有時效限制的,用戶長期未訪問伺服器,這個session是就會被主動注銷掉)不能當此大任了,可用性不在話下,至於你說的加密這是畫蛇添足了。
依樓上所言,未免誤人,伺服器端是必須保存這個驗證碼的,就像我給你了一個驗證碼,你可以使用,我自己也得備份一下和你比對,不然那不就變成了:你說你的驗證碼是對的,你已經驗證過了,我就信任你了,那就等於沒有密碼,客戶端的一切安全認證都是不足為信的!
⑺ javaweb的驗證碼問題
點擊後,src屬性地址換一下,後面帶個時間戳欄位,這樣就不會緩存
⑻ javaweb登錄三次失敗驗證碼怎麼實現
使用redis存儲,存儲當前登錄的用戶名與登錄失敗次數,超過三次就顯示驗證碼
session和cookie都不建議使用,建議服務端開啟驗證,瀏覽器端都可以繞過這些檢查
⑼ 如何製作 java web 驗證碼
java web的驗證碼原理如下: 根據參數隨機生成一個字元串,將字元串存儲在一個靜態變數中,然後將字元串寫在圖片上傳遞到前台供用戶識別。用戶將識別出的字元串再提交到伺服器, 伺服器取出靜態變數中的字元串與之對比。然後將對比結果返回給用戶。
可以自己在網上找些實現好的代碼學習一下,也可以使用一些比較成熟的驗證碼組件。
隨著越來越發達的網路技術, 驗證碼技術也在不斷進步,有的驗證碼中的字元變成了中文,有的在圖片中生成問題來讓用戶回答,有的甚至用到了圖片歸類,但原理沒變。
⑽ java web 項目驗證碼的刷新問題
用js換 img的src就行了啊。
function change(){
document.getElementById("CreateCheckCode").src="PictureCheckCode?"+Math.random();
}
</script>
<img id="CreateCheckCode" src="PictureCheckCode" onclick="change()" />