java實現動態驗證碼
java動態實現驗證碼,供大家參考,具體內容如下
【實現效果】
點擊圖片或者文字可以更換驗證碼 驗證碼隨機生成,由大小寫字母和數字組成 驗證碼字體顏色隨機生成,字母角度有偏轉 干擾線隨機分布驗證碼的功能: 防止惡意的表單注冊
VerificationCode.java 驗證碼功能實現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:驗證碼生成 */@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.創建對象,驗證碼圖片對象 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //2.美化圖片 //2.1填充背景色 //Graphics g = image.getGraphics(); Graphics2D g = (Graphics2D) image.getGraphics();//畫筆對象,2D來旋轉驗證碼字母 g.setColor(Color.WHITE);//設置畫筆顏色 g.fillRect(0, 0, width, height); //2.2畫邊框 g.setColor(Color.BLUE); g.drawRect(0, 0, width - 1, height - 1); //2.3生成驗證碼 String str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; //生成隨機角標 Random random = new Random(); //改變字體 g.setFont(new Font('宋體',Font.BOLD,35)); //將驗證碼偏轉并寫到畫布上 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);//隨機字符 //獲取正負30的角度 int angle = random.nextInt(60) - 30; double radian = angle * Math.PI/180; //設置驗證碼中的字體顏色 //g.setColor(Color.BLUE); int red = 0; int green = 0; int blue = 0; int codeY = 32; // 得到隨機產生的驗證碼數字 // 產生隨機的顏色分量來構造顏色值,使輸出的每位數字的顏色值都不同 red = random.nextInt(255); green = random.nextInt(255); blue = random.nextInt(255); // 用隨機產生的顏色將驗證碼繪制到圖像中 g.setColor(new Color(red, green, blue)); //寫驗證碼 g.rotate(radian, x, y); //把字母畫在畫布上 //g.drawString(ch+'', x, y); g.drawString(String.valueOf(ch)+'', x, codeY); //把每次旋轉的再旋轉回來 g.rotate(-radian, x, y); //每次向右移動20像素 x += 15; msg += ch; } //2.4隨機產生20條干擾線,使圖象中的認證碼不易被其它程序探測到 g.setColor(Color.MAGENTA); //隨機生成坐標點 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.將圖片輸出到頁面展示 //將圖片對象寫入流中 ImageIO.write(image, 'jpg', response.getOutputStream()); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); }}insex.jsp 前臺頁面展示
分析:
點擊超鏈接或者圖片,需要換一張 給超鏈接和圖片綁定單擊事件 重新設置圖片的src屬性值生成的圖片先要緩存在本地,每次請求是不會修改,所以驗證碼圖片不會切換;將圖片路徑后添加時間戳,通過錯誤的路徑來欺騙服務器重新請求
<%@ page contentType='text/html;charset=UTF-8' language='java' %><html><head> <title>驗證碼</title> <script> window.onload = function () { //1.獲取圖片對象 var img = document.getElementById('checkCode'); //2.綁定圖片單擊事件 img.onclick = function () { //加時間戳 var date = new Date().getTime(); //加時間戳,防止瀏覽器利用緩存 img.src = 'http://www.aoyou183.cn/ServletResponse/VerificationCode?' + date; } //綁定鏈接點擊事件 var ahref = document.getElementById('change'); ahref.onclick = function () { var date = new Date().getTime(); img.src = 'http://www.aoyou183.cn/ServletResponse/VerificationCode?' + date; } } </script></head><body><h2>驗證碼動態實現</h2><img src='http://www.aoyou183.cn/ServletResponse/VerificationCode'><a href='http://www.aoyou183.cn/bcjs/5074.html'>看不清?換一張</a></body></html>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
