java實(shí)現(xiàn)動(dòng)態(tài)驗(yàn)證碼
java動(dòng)態(tài)實(shí)現(xiàn)驗(yàn)證碼,供大家參考,具體內(nèi)容如下
【實(shí)現(xiàn)效果】
點(diǎn)擊圖片或者文字可以更換驗(yàn)證碼 驗(yàn)證碼隨機(jī)生成,由大小寫(xiě)字母和數(shù)字組成 驗(yàn)證碼字體顏色隨機(jī)生成,字母角度有偏轉(zhuǎn) 干擾線隨機(jī)分布驗(yàn)證碼的功能: 防止惡意的表單注冊(cè)
VerificationCode.java 驗(yàn)證碼功能實(shí)現(xiàn)package com.iqqcode.servlet.checkcode;import javax.imageio.ImageIO;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.awt.*;import java.awt.image.BufferedImage;import java.io.IOException;import java.util.Random;/** * @Author: Mr.Q * @Date: 2020-02-12 10:12 * @Description:驗(yàn)證碼生成 */@WebServlet('/VerificationCode')public class VerificationCode extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int width = 120; int height = 50; //1.創(chuàng)建對(duì)象,驗(yàn)證碼圖片對(duì)象 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //2.美化圖片 //2.1填充背景色 //Graphics g = image.getGraphics(); Graphics2D g = (Graphics2D) image.getGraphics();//畫(huà)筆對(duì)象,2D來(lái)旋轉(zhuǎn)驗(yàn)證碼字母 g.setColor(Color.WHITE);//設(shè)置畫(huà)筆顏色 g.fillRect(0, 0, width, height); //2.2畫(huà)邊框 g.setColor(Color.BLUE); g.drawRect(0, 0, width - 1, height - 1); //2.3生成驗(yàn)證碼 String str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; //生成隨機(jī)角標(biāo) Random random = new Random(); //改變字體 g.setFont(new Font('宋體',Font.BOLD,35)); //將驗(yàn)證碼偏轉(zhuǎn)并寫(xiě)到畫(huà)布上 for (int i = 1; i <= 4; i++) { int x = width/5 * i; int y = height/2; String msg = ''; int index = random.nextInt(str.length()); //獲取字符 char ch = str.charAt(index);//隨機(jī)字符 //獲取正負(fù)30的角度 int angle = random.nextInt(60) - 30; double radian = angle * Math.PI/180; //設(shè)置驗(yàn)證碼中的字體顏色 //g.setColor(Color.BLUE); int red = 0; int green = 0; int blue = 0; int codeY = 32; // 得到隨機(jī)產(chǎn)生的驗(yàn)證碼數(shù)字 // 產(chǎn)生隨機(jī)的顏色分量來(lái)構(gòu)造顏色值,使輸出的每位數(shù)字的顏色值都不同 red = random.nextInt(255); green = random.nextInt(255); blue = random.nextInt(255); // 用隨機(jī)產(chǎn)生的顏色將驗(yàn)證碼繪制到圖像中 g.setColor(new Color(red, green, blue)); //寫(xiě)驗(yàn)證碼 g.rotate(radian, x, y); //把字母畫(huà)在畫(huà)布上 //g.drawString(ch+'', x, y); g.drawString(String.valueOf(ch)+'', x, codeY); //把每次旋轉(zhuǎn)的再旋轉(zhuǎn)回來(lái) g.rotate(-radian, x, y); //每次向右移動(dòng)20像素 x += 15; msg += ch; } //2.4隨機(jī)產(chǎn)生20條干擾線,使圖象中的認(rèn)證碼不易被其它程序探測(cè)到 g.setColor(Color.MAGENTA); //隨機(jī)生成坐標(biāo)點(diǎn) for (int i = 0; i < 20; i++) { int x1 = random.nextInt(width); int x2 = random.nextInt(width); int y1 = random.nextInt(height); int y2 = random.nextInt(height); g.drawLine(x1, x2, y1, y2); } //3.將圖片輸出到頁(yè)面展示 //將圖片對(duì)象寫(xiě)入流中 ImageIO.write(image, 'jpg', response.getOutputStream()); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); }}insex.jsp 前臺(tái)頁(yè)面展示
分析:
點(diǎn)擊超鏈接或者圖片,需要換一張 給超鏈接和圖片綁定單擊事件 重新設(shè)置圖片的src屬性值生成的圖片先要緩存在本地,每次請(qǐng)求是不會(huì)修改,所以驗(yàn)證碼圖片不會(huì)切換;將圖片路徑后添加時(shí)間戳,通過(guò)錯(cuò)誤的路徑來(lái)欺騙服務(wù)器重新請(qǐng)求
<%@ page contentType='text/html;charset=UTF-8' language='java' %><html><head> <title>驗(yàn)證碼</title> <script> window.onload = function () { //1.獲取圖片對(duì)象 var img = document.getElementById('checkCode'); //2.綁定圖片單擊事件 img.onclick = function () { //加時(shí)間戳 var date = new Date().getTime(); //加時(shí)間戳,防止瀏覽器利用緩存 img.src = 'http://www.baoyu77737.com/ServletResponse/VerificationCode?' + date; } //綁定鏈接點(diǎn)擊事件 var ahref = document.getElementById('change'); ahref.onclick = function () { var date = new Date().getTime(); img.src = 'http://www.baoyu77737.com/ServletResponse/VerificationCode?' + date; } } </script></head><body><h2>驗(yàn)證碼動(dòng)態(tài)實(shí)現(xiàn)</h2><img src='http://www.baoyu77737.com/ServletResponse/VerificationCode'><a href='http://www.baoyu77737.com/bcjs/5074.html'>看不清?換一張</a></body></html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. XML入門(mén)的常見(jiàn)問(wèn)題(三)2. HTTP協(xié)議常用的請(qǐng)求頭和響應(yīng)頭響應(yīng)詳解說(shuō)明(學(xué)習(xí))3. ASP將數(shù)字轉(zhuǎn)中文數(shù)字(大寫(xiě)金額)的函數(shù)4. XML在語(yǔ)音合成中的應(yīng)用5. .NET Framework各版本(.NET2.0 3.0 3.5 4.0)區(qū)別6. jscript與vbscript 操作XML元素屬性的代碼7. 不要在HTML中濫用div8. php使用正則驗(yàn)證密碼字段的復(fù)雜強(qiáng)度原理詳細(xì)講解 原創(chuàng)9. ASP基礎(chǔ)入門(mén)第四篇(腳本變量、函數(shù)、過(guò)程和條件語(yǔ)句)10. HTML5實(shí)戰(zhàn)與剖析之觸摸事件(touchstart、touchmove和touchend)
