日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

验证码设计

發布時間:2025/6/17 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 验证码设计 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

首先,創建生成驗證碼類:

//需要導入的包 import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.util.Random;import javax.imageio.ImageIO; import javax.imageio.stream.ImageOutputStream;

//生成具有背景圖像的驗證碼類

public class IdentifyCode {

?private String randCode;
?private ByteArrayInputStream? inputStream;
?//參數:隨機碼個數
?public IdentifyingCode( int?codeNum ) {
??try {
???createRandCode ( codeNum ) ;
??} catch (Exception e) {
???e.printStackTrace();
??}

?

public String getRandCode() {
??return randCode;
?}

?

?public ByteArrayInputStream getInputStream() {
??return inputStream;
?}?


?}

定義createRandCode方法:

private void createRandCode (int codeNum ) throws Exception{//在內存中創建圖象 int width = 15*codeNum;
int height = 25; //創建一個不帶透明色的BufferedImage對象BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);//獲取圖形上下文 Graphics g = image.getGraphics();//生成隨機類 Random random = new Random();//設定背景色 g.setColor(getRandColor(200, 250));g.fillRect(0, 0, width, height);//設定字體 g.setFont(new Font("Times New Roman", Font.PLAIN, 20));//隨機產生1000條干擾線,使圖象中的認證碼不易被其它程序探測到
g.setColor(getRandColor(160, 200)); ?//獲取隨機顏色

for (int i = 0; i < 1000; i++) {int x = random.nextInt(width);int y = random.nextInt(height);int xl = random.nextInt(14);int yl = random.nextInt(14);g.drawLine(x, y, x + xl, y + yl);}//定義數字數組int[] number = {56,57,58,59,60,61,62,63,64,65};//定義小寫字母數組int[] lowerAlphabet = {65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90};//定義大字母數組int[] upperAlphabet = {97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122};//取隨機產生的認證碼(codeNum位數字) StringBuilder sRand = new StringBuilder();for (int i = 0; i < codeNum; i++) {char rand;int nowNum = random.nextInt(3);nowNum=0;switch (nowNum) {case 0: {rand = (char) number[random.nextInt(number.length)];break;}case 1: {rand = (char) lowerAlphabet[random.nextInt(lowerAlphabet.length)];break;}case 2: {rand = (char) upperAlphabet[random.nextInt(upperAlphabet.length)];break;}default: {rand =(char) lowerAlphabet[random.nextInt(lowerAlphabet.length)];break;}}sRand.append(rand);// 將認證碼顯示到圖象中g.setColor(new Color(10 + random.nextInt(110), 10 + random.nextInt(110), 10 + random.nextInt(110))); g.drawString(rand+"", 13 * i + 7, 18);}g.dispose();this.randCode=sRand.toString();ByteArrayOutputStream output = new ByteArrayOutputStream();ImageOutputStream imageOut = ImageIO.createImageOutputStream(output);ImageIO.write(image, "JPEG", imageOut);this.inputStream = new ByteArrayInputStream(output.toByteArray());imageOut.close();output.close();}
?/*?
? * 給定范圍獲得隨機顏色?
? */
?
private? Color getRandColor(int fc, int bc) {
??
Random random = new Random();
??
if (fc > 255)
???fc = 255;
??
if (bc > 255)
???bc = 255;
??
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);
?
}



前臺頁面處理:

<!--加載頁面的同時加載隨機碼-->
<
body onload="loadRandCode()">
<form id="login" name="login" method="post">
<table>
<tr>
??????
<td height="27" colspan="2" bordercolor="#666666">
???????
<span class="style8">驗證碼</span>

??????</td>
??????<td width="128" valign="bottom">

???????<input type="text" name="randCode" id="randCode" class="input_css" tabindex="3" maxlength="4"? />
?
??????</td>

??????<td>

???????<span class="STYLE6">&nbsp;<span class="style7"><img id ="randCodeImg"? οnclick="changeValidateCode(this)"/></span></span>

??????</td>

?????</tr>

????</table>
</form>
</body>
<script type="text/javascript">

??
/**
???*獲取圖形驗證碼
???*/

??? function changeValidateCode(obj) {
? //獲取當前的時間作為參數,無具體意義?

??????var timenow = new Date().getTime();

?
?????obj.src="${pageContext.request.contextPath}/Login_rand.do?d="+timenow;
?
?????? }

?????? //第一次加載
?????? function loadRandCode(){

?????? ?changeValidateCode(document.getElementById("randCodeImg"))

?????? }

? ??/**
???*登錄驗證和請求
???*/

???? function userLogin(){
var webForm = document.getElementById("login");

??? ?webForm.action = "${pageContext.request.contextPath}/Login_login.do";

???? webForm.submit();}


?
</script>

后臺驗證判斷處理:

public String login() {if(!randCode.equalsIgnoreCase((String)session.get("randCode"))){request.setAttribute("error", "驗證碼輸入錯誤!");return INPUT;} }

?

?

轉載于:https://www.cnblogs.com/zhangchunxi/archive/2013/03/15/2961007.html

總結

以上是生活随笔為你收集整理的验证码设计的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。