SpringBoot實(shí)現(xiàn)發(fā)送驗(yàn)證碼功能(圖片驗(yàn)證碼)
提示:本次內(nèi)容主要學(xué)習(xí)如何做一個(gè)發(fā)送驗(yàn)證碼和識(shí)別驗(yàn)證碼的功能
前言
提示:本次內(nèi)容主要學(xué)習(xí)如何做一個(gè)發(fā)送驗(yàn)證碼和識(shí)別驗(yàn)證碼的功能
例如:隨著現(xiàn)在互聯(lián)網(wǎng)的不斷發(fā)展,Web發(fā)展可謂是越來越好,但是在這種情況之下,就會(huì)出現(xiàn)很多不守規(guī)矩的人,可能會(huì)利用爬蟲來不斷地去破解你的用戶,此時(shí)驗(yàn)證碼的作用就發(fā)揮出來了,可以有效地阻止一些不法分子來破解你的網(wǎng)站
一、圖片驗(yàn)證碼是什么?
圖片驗(yàn)證碼(Captcha)是一種通過生成圖片來驗(yàn)證用戶身份的技術(shù),用于區(qū)分人類用戶和自動(dòng)化程序。其主要目的是防止機(jī)器人的惡意訪問和攻擊,保護(hù)網(wǎng)站的安全。
二、使用步驟
1.創(chuàng)建驗(yàn)證碼生成
代碼如下(示例):這個(gè)代碼主要來源于程序員老羅的教程
package com.easybbs.entity.dto;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
public class CreateImageCode {
// 圖片的寬度。
private int width = 160;
// 圖片的高度。
private int height = 40;
// 驗(yàn)證碼字符個(gè)數(shù)
private int codeCount = 4;
// 驗(yàn)證碼干擾線數(shù)
private int lineCount = 20;
// 驗(yàn)證碼
private String code = null;
// 驗(yàn)證碼圖片Buffer
private BufferedImage buffImg = null;
Random random = new Random();
public CreateImageCode() {
creatImage();
}
public CreateImageCode(int width, int height) {
this.width = width;
this.height = height;
creatImage();
}
public CreateImageCode(int width, int height, int codeCount) {
this.width = width;
this.height = height;
this.codeCount = codeCount;
creatImage();
}
public CreateImageCode(int width, int height, int codeCount, int lineCount) {
this.width = width;
this.height = height;
this.codeCount = codeCount;
this.lineCount = lineCount;
creatImage();
}
// 生成圖片
private void creatImage() {
int fontWidth = width / codeCount;// 字體的寬度
int fontHeight = height - 5;// 字體的高度
int codeY = height - 8;
// 圖像buffer
buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = buffImg.getGraphics();
//Graphics2D g = buffImg.createGraphics();
// 設(shè)置背景色
g.setColor(getRandColor(200, 250));
g.fillRect(0, 0, width, height);
// 設(shè)置字體
//Font font1 = getFont(fontHeight);
Font font = new Font("Fixedsys", Font.BOLD, fontHeight);
g.setFont(font);
// 設(shè)置干擾線
for (int i = 0; i < lineCount; i++) {
int xs = random.nextInt(width);
int ys = random.nextInt(height);
int xe = xs + random.nextInt(width);
int ye = ys + random.nextInt(height);
g.setColor(getRandColor(1, 255));
g.drawLine(xs, ys, xe, ye);
}
// 添加噪點(diǎn)
float yawpRate = 0.01f;// 噪聲率
int area = (int) (yawpRate * width * height);
for (int i = 0; i < area; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
buffImg.setRGB(x, y, random.nextInt(255));
}
String str1 = randomStr(codeCount);// 得到隨機(jī)字符
this.code = str1;
for (int i = 0; i < codeCount; i++) {
String strRand = str1.substring(i, i + 1);
g.setColor(getRandColor(1, 255));
// g.drawString(a,x,y);
// a為要畫出來的東西,x和y表示要畫的東西最左側(cè)字符的基線位于此圖形上下文坐標(biāo)系的 (x, y) 位置處
g.drawString(strRand, i * fontWidth + 3, codeY);
}
}
// 得到隨機(jī)字符
private String randomStr(int n) {
String str1 = "ABCDEFGHJKMNOPQRSTUVWXYZabcdefghjkmnopqrstuvwxyz234567890";
String str2 = "";
int len = str1.length() - 1;
double r;
for (int i = 0; i < n; i++) {
r = (Math.random()) * len;
str2 = str2 + str1.charAt((int) r);
}
return str2;
}
// 得到隨機(jī)顏色
private Color getRandColor(int fc, int bc) {// 給定范圍獲得隨機(jī)顏色
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);
}
/**
* 產(chǎn)生隨機(jī)字體
*/
private Font getFont(int size) {
Random random = new Random();
Font font[] = new Font[5];
font[0] = new Font("Ravie", Font.PLAIN, size);
font[1] = new Font("Antique Olive Compact", Font.PLAIN, size);
font[2] = new Font("Fixedsys", Font.PLAIN, size);
font[3] = new Font("Wide Latin", Font.PLAIN, size);
font[4] = new Font("Gill Sans Ultra Bold", Font.PLAIN, size);
return font[random.nextInt(5)];
}
// 扭曲方法
private void shear(Graphics g, int w1, int h1, Color color) {
shearX(g, w1, h1, color);
shearY(g, w1, h1, color);
}
private void shearX(Graphics g, int w1, int h1, Color color) {
int period = random.nextInt(2);
boolean borderGap = true;
int frames = 1;
int phase = random.nextInt(2);
for (int i = 0; i < h1; i++) {
double d = (double) (period >> 1) * Math.sin((double) i / (double) period + (6.2831853071795862D * (double) phase) / (double) frames);
g.copyArea(0, i, w1, 1, (int) d, 0);
if (borderGap) {
g.setColor(color);
g.drawLine((int) d, i, 0, i);
g.drawLine((int) d + w1, i, w1, i);
}
}
}
private void shearY(Graphics g, int w1, int h1, Color color) {
int period = random.nextInt(40) + 10; // 50;
boolean borderGap = true;
int frames = 20;
int phase = 7;
for (int i = 0; i < w1; i++) {
double d = (double) (period >> 1) * Math.sin((double) i / (double) period + (6.2831853071795862D * (double) phase) / (double) frames);
g.copyArea(i, 0, 1, h1, 0, (int) d);
if (borderGap) {
g.setColor(color);
g.drawLine(i, (int) d, i, 0);
g.drawLine(i, (int) d + h1, i, h1);
}
}
}
public void write(OutputStream sos) throws IOException {
ImageIO.write(buffImg, "png", sos);
sos.close();
}
public BufferedImage getBuffImg() {
return buffImg;
}
public String getCode() {
return code.toLowerCase();
}
}2.生成驗(yàn)證碼
代碼如下(示例):
@RequestMapping("/checkCode")
public void checkCode(HttpServletResponse response, HttpSession session) throws IOException {
// 創(chuàng)建一個(gè)驗(yàn)證碼對(duì)象,參數(shù)分別指定驗(yàn)證碼的寬度、高度、字符數(shù)量和干擾線數(shù)量。
CreateImageCode vCode = new CreateImageCode(130, 38, 5, 10);
// 設(shè)置響應(yīng)頭,確保瀏覽器不緩存驗(yàn)證碼圖像。
response.setHeader("Pragma","no-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
// 設(shè)置響應(yīng)類型為JPEG圖像。
response.setContentType("image/jpeg");
// 生成驗(yàn)證碼字符串。
String code = vCode.getCode();
// 根據(jù)類型將驗(yàn)證碼存儲(chǔ)在不同的session屬性中。
session.setAttribute("checkCodeKey",code);
// 將生成的驗(yàn)證碼圖像寫入響應(yīng)輸出流。
vCode.write(response.getOutputStream());
}具體識(shí)別的時(shí)候可以從session里面取到,具體情況具體對(duì)待吧
總結(jié)
這里主要是記錄了一下如何生成圖片驗(yàn)證碼
到此這篇關(guān)于SpringBoot實(shí)現(xiàn)發(fā)送驗(yàn)證碼功能的文章就介紹到這了,更多相關(guān)SpringBoot發(fā)送驗(yàn)證碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
maven無法依賴spring-cloud-stater-zipkin的解決方案
這篇文章主要介紹了maven無法依賴spring-cloud-stater-zipkin如何解決,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05
淺談Java finally語句到底是在return之前還是之后執(zhí)行(必看篇)
下面小編就為大家?guī)硪黄獪\談Java finally語句到底是在return之前還是之后執(zhí)行(必看篇)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06
基于接口實(shí)現(xiàn)java動(dòng)態(tài)代理示例
這篇文章主要介紹了基于接口實(shí)現(xiàn)java動(dòng)態(tài)代理示例,需要的朋友可以參考下2014-04-04
Java判斷文件或者文件夾是否存在的功能(不存在創(chuàng)建,存在刪除)
在Java中,要判斷文件或文件夾是否存在,并根據(jù)判斷結(jié)果來執(zhí)行創(chuàng)建或刪除操作,你可以使用java.io.File類或者java.nio.file包中的類,本文介紹Java判斷文件或者文件夾是否存在,不存在創(chuàng)建,存在刪除,感興趣的朋友一起看看吧2025-03-03
WPF實(shí)現(xiàn)自定義一個(gè)自刪除的多功能ListBox
這篇文章主要為大家詳細(xì)介紹了如何利用WPF實(shí)現(xiàn)自定義一個(gè)自刪除的多功能ListBox,文中示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-12-12
spring?retry實(shí)現(xiàn)方法請(qǐng)求重試的使用步驟
這篇文章主要介紹了spring?retry實(shí)現(xiàn)方法請(qǐng)求重試及使用步驟,本文分步驟通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07
SpringMVC中Invalid bound statement (not f
本文主要介紹了SpringMVC中Invalid bound statement (not found)常見報(bào)錯(cuò)問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
Java編程中利用InetAddress類確定特殊IP地址的方法
這篇文章主要介紹了Java編程中利用InetAddress類確定特殊IP地址的方法,InetAddress類是Java網(wǎng)絡(luò)編程中一個(gè)相當(dāng)實(shí)用的類,需要的朋友可以參考下2015-11-11

