實現認證碼技術,需要以下幾方面的準備:
1.生成認證碼:
我們可以讓系統隨機產生一個數字來作為認證碼,這方面在javascript腳本語言和java語言中均可得以實現。前者可以用math.random()得到一個介于0與1之間的小數,用它乘以10000再取整數部分,即可得到0至9999之間的隨機數。后者可以用random類的nextint(n)方法得到一個介于0至n-1之間的隨機類。
在實現時,我們采用了前者,即用javascript生成的隨機數作為認證碼。主要原因是javascript是html內置的腳本語言,不管頁面是前進、后退還是刷新,都能保證及時產生新的認證碼,增加了隨機性。而用java實現時,則不具備此特性,瀏覽器還保存原先的認證碼,隨機性不強。
2.生成認證碼圖象:
這是比較關鍵的部分。幸運的是,java語言給我們提供了強大的支持。我們可以利用bufferedimage類在內存中繪制圖象,并可利用imageio類將圖象輸出到jsp頁面中。在繪制圖象時,我們就可以將隨機產生的認證碼,繪制到圖象中,進而展現在用戶面前。另,為了增加破譯的難度,我們可以隨機畫一些點。
3.保存認證碼:
在jsp語言中,我們可以充分利用該語言內置的session對象來保存認證碼之值,方法是:session.setattribute("認證碼名字",認證碼之值)。并可用session.getattribute("認證碼名字")得到系統保存的認證碼之值,用來和用戶輸入的認證碼相比較,很是方便。
三、認證碼技術的jsp實現
1.image.jsp
這個jsp程序的功能是:根據頁面參數rand生成相應的認證碼圖象,同時設定session變量rand,以便check.jsp驗證用戶輸入的認證碼時使用。
源程序如下:
<%@ page contenttype="image/jpeg" import="java.awt.*,
java.awt.image.*,java.util.*,javax.imageio.*" %>
<%
// 在內存中創建圖象
int width=60, height=20;
bufferedimage image = new bufferedimage(width, height,
bufferedimage.type_int_rgb);
// 獲取圖形上下文
graphics g = image.getgraphics();
// 設定背景色
g.setcolor(color.white);
g.fillrect(0, 0, width, height);
//畫邊框
g.setcolor(color.black);
g.drawrect(0,0,width-1,height-1);
// 取隨機產生的認證碼(4位數字)
string rand = request.getparameter("rand");
rand = rand.substring(0,rand.indexof("."));
switch(rand.length())
{
case 1: rand = "000"+rand; break;
case 2: rand = "00"+rand; break;
case 3: rand = "0"+rand; break;
default: rand = rand.substring(0,4); break;
}
// 將認證碼存入session
session.setattribute("rand",rand);
// 將認證碼顯示到圖象中
g.setcolor(color.black);
g.setfont(new font("times new roman",font.plain,18));
g.drawstring(rand,10,15);
// 隨機產生88個干擾點,使圖象中的認證碼不易被其它程序探測到
random random = new random();
for (int i=0;i<88;i++)
{
int x = random.nextint(width);
int y = random.nextint(height);
g.drawline(x,y,x,y);
}
// 圖象生效
g.dispose();
// 輸出圖象到頁面
imageio.write(image, "jpeg", response.getoutputstream());
%>
2.a.jsp
這個jsp程序的功能是:顯示認證碼,提供表單讓用戶輸入認證碼供校驗用。注意,程序中顯示認證碼圖象時,用了javascript的document.write,并用了math.random函數,從而保證了認證碼的及時更新特性。
源程序如下:
<%@ page contenttype="text/html;charset=gb2312" %>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<title>認證碼輸入頁面</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<form method=post action="check.jsp">
<table>
<tr>
<td align=left>系統產生的認證碼:</td>
<td><script>document.write("<img border=0 src=image.jsp?
rand="+math.random()*10000+">");</script></td>
</tr>
<tr>
<td align=left>輸入上面的認證碼:</td>
<td><input type=text name=rand maxlength=4 value=""></td>
</tr>
<tr>
<td colspan=2 align=center><input type=submit value="提交檢測"></td>
</tr>
</form>
</body>
</html>
3.check.jsp
這個jsp程序的作用是比較用戶輸入的認證碼與session變量中保存的認證碼,相同時提示認證成功,否則提示認證失敗。
源程序如下:
<%@ page contenttype="text/html; charset=gb2312" language="java"
import="java.sql.*" errorpage="" %>
<html>
<head>
<title>認證碼驗證頁面</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<%
string rand = (string)session.getattribute("rand");
string input = request.getparameter("rand");
%>
系統產生的認證碼為: <%= rand %><br>
您輸入的認證碼為: <%= input %><br>
<br>
<%
if (rand.equals(input)) {
%>
<font color=green>輸入相同,認證成功!</font>
<%
} else {
%>
<font color=red>輸入不同,認證失??!</font>
<%
}
%>
</body>
</html>
新聞熱點
疑難解答