日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

验证码设计

發(fā)布時(shí)間:2025/6/17 52 豆豆
生活随笔 收集整理的這篇文章主要介紹了 验证码设计 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

首先,創(chuàng)建生成驗(yàn)證碼類:

//需要導(dǎo)入的包 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;

//生成具有背景圖像的驗(yàn)證碼類

public class IdentifyCode {

?private String randCode;
?private ByteArrayInputStream? inputStream;
?//參數(shù):隨機(jī)碼個(gè)數(shù)
?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{//在內(nèi)存中創(chuàng)建圖象 int width = 15*codeNum;
int height = 25; //創(chuàng)建一個(gè)不帶透明色的BufferedImage對(duì)象BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);//獲取圖形上下文 Graphics g = image.getGraphics();//生成隨機(jī)類 Random random = new Random();//設(shè)定背景色 g.setColor(getRandColor(200, 250));g.fillRect(0, 0, width, height);//設(shè)定字體 g.setFont(new Font("Times New Roman", Font.PLAIN, 20));//隨機(jī)產(chǎn)生1000條干擾線,使圖象中的認(rèn)證碼不易被其它程序探測(cè)到
g.setColor(getRandColor(160, 200)); ?//獲取隨機(jī)顏色

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);}//定義數(shù)字?jǐn)?shù)組int[] number = {56,57,58,59,60,61,62,63,64,65};//定義小寫字母數(shù)組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};//定義大字母數(shù)組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};//取隨機(jī)產(chǎn)生的認(rèn)證碼(codeNum位數(shù)字) 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);// 將認(rèn)證碼顯示到圖象中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();}
?/*?
? * 給定范圍獲得隨機(jī)顏色?
? */
?
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);
?
}



前臺(tái)頁(yè)面處理:

<!--加載頁(yè)面的同時(shí)加載隨機(jī)碼-->
<
body onload="loadRandCode()">
<form id="login" name="login" method="post">
<table>
<tr>
??????
<td height="27" colspan="2" bordercolor="#666666">
???????
<span class="style8">驗(yàn)證碼</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">

??
/**
???*獲取圖形驗(yàn)證碼
???*/

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

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

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

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

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

?????? }

? ??/**
???*登錄驗(yàn)證和請(qǐng)求
???*/

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

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

???? webForm.submit();}


?
</script>

后臺(tái)驗(yàn)證判斷處理:

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

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/zhangchunxi/archive/2013/03/15/2961007.html

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。