java實現(xiàn)動態(tài)驗證碼
更新時間:2021年03月04日 17:43:06 作者:iqqcode
這篇文章主要為大家詳細介紹了java實現(xiàn)動態(tài)驗證碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
java動態(tài)實現(xiàn)驗證碼,供大家參考,具體內(nèi)容如下
【實現(xiàn)效果】
- 點擊圖片或者文字可以更換驗證碼
- 驗證碼隨機生成,由大小寫字母和數(shù)字組成
- 驗證碼字體顏色隨機生成,字母角度有偏轉(zhuǎn)
- 干擾線隨機分布


驗證碼的功能: 防止惡意的表單注冊
VerificationCode.java 驗證碼功能實現(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:驗證碼生成
*/
@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)建對象,驗證碼圖片對象
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//2.美化圖片
//2.1填充背景色
//Graphics g = image.getGraphics();
Graphics2D g = (Graphics2D) image.getGraphics();//畫筆對象,2D來旋轉(zhuǎn)驗證碼字母
g.setColor(Color.WHITE);//設(shè)置畫筆顏色
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));
//將驗證碼偏轉(zhuǎn)并寫到畫布上
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;
//設(shè)置驗證碼中的字體顏色
//g.setColor(Color.BLUE);
int red = 0; int green = 0; int blue = 0;
int codeY = 32;
// 得到隨機產(chǎn)生的驗證碼數(shù)字
// 產(chǎn)生隨機的顏色分量來構(gòu)造顏色值,使輸出的每位數(shù)字的顏色值都不同
red = random.nextInt(255);
green = random.nextInt(255);
blue = random.nextInt(255);
// 用隨機產(chǎn)生的顏色將驗證碼繪制到圖像中
g.setColor(new Color(red, green, blue));
//寫驗證碼
g.rotate(radian, x, y);
//把字母畫在畫布上
//g.drawString(ch+"", x, y);
g.drawString(String.valueOf(ch)+"", x, codeY);
//把每次旋轉(zhuǎn)的再旋轉(zhuǎn)回來
g.rotate(-radian, x, y);
//每次向右移動20像素
x += 15;
msg += ch;
}
//2.4隨機產(chǎn)生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 前臺頁面展示
分析:
- 點擊超鏈接或者圖片,需要換一張
- 給超鏈接和圖片綁定單擊事件
- 重新設(shè)置圖片的src屬性值
生成的圖片先要緩存在本地,每次請求是不會修改,所以驗證碼圖片不會切換;
將圖片路徑后添加時間戳,通過錯誤的路徑來欺騙服務(wù)器重新請求
<%@ 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 = "/ServletResponse/VerificationCode?" + date;
}
//綁定鏈接點擊事件
var ahref = document.getElementById("change");
ahref.onclick = function () {
var date = new Date().getTime();
img.src = "/ServletResponse/VerificationCode?" + date;
}
}
</script>
</head>
<body>
<h2>驗證碼動態(tài)實現(xiàn)</h2>
<img id="checkCode" src="/ServletResponse/VerificationCode">
<a id="change" href="">看不清?換一張</a>
</body>
</html>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Intellij Idea插件開發(fā)之創(chuàng)建項目層級的右鍵菜單
這篇文章主要介紹了Intellij Idea插件開發(fā)之創(chuàng)建項目層級的右鍵菜單,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02
Java CompletableFuture 異步超時實現(xiàn)深入研究
這篇文章主要為大家介紹了Java CompletableFuture 異步超時實現(xiàn)深入研究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02
關(guān)于QueryWrapper,實現(xiàn)MybatisPlus多表關(guān)聯(lián)查詢方式
這篇文章主要介紹了關(guān)于QueryWrapper,實現(xiàn)MybatisPlus多表關(guān)聯(lián)查詢方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教。2022-01-01

