SpringBoot——登錄驗證碼實現

来源:https://www.cnblogs.com/qiantao/archive/2020/04/09/12670517.html
-Advertisement-
Play Games

今天記錄一下驗證碼的實現,希望能夠幫助到大家! 首先我們看一下實現的效果: 此驗證碼的實現沒有用到太多的插件,話不多說直接上代碼,大家拿過去就可以用。 中間用到了org.apache.commons.lang3.RandomUtils工具類,需要pom配置: <!-- https://mvnrepo ...


今天記錄一下驗證碼的實現,希望能夠幫助到大家!

首先我們看一下實現的效果:

 

此驗證碼的實現沒有用到太多的插件,話不多說直接上代碼,大家拿過去就可以用。

中間用到了org.apache.commons.lang3.RandomUtils工具類,需要pom配置:

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.10</version>
        </dependency>

1.驗證碼類

package com.youyou.login.util.validatecode;

import lombok.Data;

/**
* 驗證碼類
*/
public class VerifyCode {
    private String code;
    private byte[] imgBytes;
    private long expireTime;
    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    public byte[] getImgBytes() {
        return imgBytes;
    }
    public void setImgBytes(byte[] imgBytes) {
        this.imgBytes = imgBytes;
    }
    public long getExpireTime() {
        return expireTime;
    }
    public void setExpireTime(long expireTime) {
        this.expireTime = expireTime;
    }

}

2.驗證碼生成介面

package com.youyou.login.util.validatecode;

import java.io.IOException;
import java.io.OutputStream;

/**
* 驗證碼生成介面
*/
public interface IVerifyCodeGen {

/**
* 生成驗證碼並返回code,將圖片寫的os中
*
* @param width
* @param height
* @param os
* @return
* @throws IOException
*/
String generate(int width, int height, OutputStream os) throws IOException;

/**
* 生成驗證碼對象
*
* @param width
* @param height
* @return
* @throws IOException
*/
VerifyCode generate(int width, int height) throws IOException;
}

3.驗證碼生成實現類

package com.youyou.login.util.validatecode;

import com.youyou.util.RandomUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

/**
* 驗證碼實現類
*/
public class SimpleCharVerifyCodeGenImpl implements IVerifyCodeGen {

private static final Logger logger = LoggerFactory.getLogger(SimpleCharVerifyCodeGenImpl.class);

private static final String[] FONT_TYPES = { "\u5b8b\u4f53", "\u65b0\u5b8b\u4f53", "\u9ed1\u4f53", "\u6977\u4f53", "\u96b6\u4e66" };

private static final int VALICATE_CODE_LENGTH = 4;

/**
* 設置背景顏色及大小,干擾線
*
* @param graphics
* @param width
* @param height
*/
private static void fillBackground(Graphics graphics, int width, int height) {
// 填充背景
graphics.setColor(Color.WHITE);
//設置矩形坐標x y 為0
graphics.fillRect(0, 0, width, height);

// 加入干擾線條
for (int i = 0; i < 8; i++) {
//設置隨機顏色演算法參數
graphics.setColor(RandomUtils.randomColor(40, 150));
Random random = new Random();
int x = random.nextInt(width);
int y = random.nextInt(height);
int x1 = random.nextInt(width);
int y1 = random.nextInt(height);
graphics.drawLine(x, y, x1, y1);
}
}

/**
* 生成隨機字元
*
* @param width
* @param height
* @param os
* @return
* @throws IOException
*/
@Override
public String generate(int width, int height, OutputStream os) throws IOException {
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics graphics = image.getGraphics();
fillBackground(graphics, width, height);
String randomStr = RandomUtils.randomString(VALICATE_CODE_LENGTH);
createCharacter(graphics, randomStr);
graphics.dispose();
//設置JPEG格式
ImageIO.write(image, "JPEG", os);
return randomStr;
}

/**
* 驗證碼生成
*
* @param width
* @param height
* @return
*/
@Override
public VerifyCode generate(int width, int height) {
VerifyCode verifyCode = null;
try (
//將流的初始化放到這裡就不需要手動關閉流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
) {
String code = generate(width, height, baos);
verifyCode = new VerifyCode();
verifyCode.setCode(code);
verifyCode.setImgBytes(baos.toByteArray());
} catch (IOException e) {
logger.error(e.getMessage(), e);
verifyCode = null;
}
return verifyCode;
}

/**
* 設置字元顏色大小
*
* @param g
* @param randomStr
*/
private void createCharacter(Graphics g, String randomStr) {
char[] charArray = randomStr.toCharArray();
for (int i = 0; i < charArray.length; i++) {
//設置RGB顏色演算法參數
g.setColor(new Color(50 + RandomUtils.nextInt(100),
50 + RandomUtils.nextInt(100), 50 + RandomUtils.nextInt(100)));
//設置字體大小,類型
g.setFont(new Font(FONT_TYPES[RandomUtils.nextInt(FONT_TYPES.length)], Font.BOLD, 26));
//設置x y 坐標
g.drawString(String.valueOf(charArray[i]), 15 * i + 5, 19 + RandomUtils.nextInt(8));
}
}
}

4.工具類

package com.youyou.util;

import java.awt.*;
import java.util.Random;

public class RandomUtils extends org.apache.commons.lang3.RandomUtils {

private static final char[] CODE_SEQ = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J',
'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7', '8', '9' };

private static final char[] NUMBER_ARRAY = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

private static Random random = new Random();

public static String randomString(int length) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
sb.append(String.valueOf(CODE_SEQ[random.nextInt(CODE_SEQ.length)]));
}
return sb.toString();
}

public static String randomNumberString(int length) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
sb.append(String.valueOf(NUMBER_ARRAY[random.nextInt(NUMBER_ARRAY.length)]));
}
return sb.toString();
}

public static Color randomColor(int fc, int bc) {
int f = fc;
int b = bc;
Random random = new Random();
if (f > 255) {
f = 255;
}
if (b > 255) {
b = 255;
}
return new Color(f + random.nextInt(b - f), f + random.nextInt(b - f), f + random.nextInt(b - f));
}

public static int nextInt(int bound) {
return random.nextInt(bound);
}
}

經過以上代碼,我們的驗證碼生成功能基本上已經實現了,現在還需要一個controller來調用它。

@ApiOperation(value = "驗證碼")
@GetMapping("/verifyCode")
public void verifyCode(HttpServletRequest request, HttpServletResponse response) {
IVerifyCodeGen iVerifyCodeGen = new SimpleCharVerifyCodeGenImpl();
try {
//設置長寬
VerifyCode verifyCode = iVerifyCodeGen.generate(80, 28);
String code = verifyCode.getCode();
LOGGER.info(code);
//將VerifyCode綁定session
request.getSession().setAttribute("VerifyCode", code);
//設置響應頭
response.setHeader("Pragma", "no-cache");
//設置響應頭
response.setHeader("Cache-Control", "no-cache");
//在代理伺服器端防止緩衝
response.setDateHeader("Expires", 0);
//設置響應內容類型
response.setContentType("image/jpeg");
response.getOutputStream().write(verifyCode.getImgBytes());
response.getOutputStream().flush();
} catch (IOException e) {
LOGGER.info("", e);
}
}

搞定!後臺編寫到此結束了。那麼又會有博友說了:“說好的實現效果呢?”

好吧,那麼我們繼續前端的代碼編寫。

前端代碼:

<html>
<body>

<div>
<input id="code" placeholder="驗證碼" type="text" class=""
style="width:170px">
<!-- 驗證碼 顯示 -->
<img οnclick="javascript:getvCode()" id="verifyimg" style="margin-left: 20px;"/>
</div>

<script type="text/javascript">
getvCode();

/**
* 獲取驗證碼
* 將驗證碼寫到login.html頁面的id = verifyimg 的地方
*/
function getvCode() {
document.getElementById("verifyimg").src = timestamp("http://127.0.0.1:81/verifyCode");
}
//為url添加時間戳
function timestamp(url) {
var getTimestamp = new Date().getTime();
if (url.indexOf("?") > -1) {
url = url + "&timestamp=" + getTimestamp
} else {
url = url + "?timestamp=" + getTimestamp
}
return url;
};
</script>
</body>

</html>

可以實現點擊圖片更換驗證碼。

實現效果:

 

當然文章開頭的截圖是我系統中的截圖,需要大家自己去根據自己的情況去開發前端了。
原文鏈接:https://blog.csdn.net/qq_37651267/article/details/99305573


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • System類中的屬性和方法都是靜態的。 out:代表標準輸出,預設是控制台 in:標準輸入,預設鍵盤 getProperties:獲取系統屬性信息 java -Dpro=value class 在jvm啟動時添加屬性 public class Demo { public static void m ...
  • 一、異常的捕獲和處理 KEY WORDS : try, catch, finally, throw, throws. (一)syntax(代碼) try{ //需要運行的代碼 }catch(異常類型 異常變數名){ //異常處理代碼 }finally{ //異常發生,方法返回之前,需要執行的代碼 } ...
  • 當類名重名時,需要指定具體的包名。 當方法重名時,需要指定具體的對象或者類。 import java.util.Arrays; import static java.util.Arrays.*; import static java.lang.System.*; //導入System類中所有靜態成員 ...
  • public class Demo { public static void main(String[] args) { show(2, 3, 4, 5, 6); } public static void show(int... arr) { System.out.println(arr.lengt ...
  • 格式: for(數據類型 變數名: 被遍歷的集合(Collection)或者數組) 只能取出,不能增刪。 對集合進行遍歷:只能獲取集合元素。但是不能對集合進行操作。 迭代器除了遍歷還能進行remove集合中元素的動作。 如何使用ListIterator還可以在遍歷過程中對集合進行增刪改查的動作。 傳 ...
  • 一、join方法 1.該方法為成員方法 2.線程合併 package com.bjpowernode.java_learning; ​ public class D107_1_JoinMethod { public static void main(String[] args) throws Int ...
  • 1. 回顧Boosting提升演算法 AdaBoost是典型的Boosting演算法,屬於Boosting家族的一員。在說AdaBoost之前,先說說Boosting提升演算法。Boosting演算法是將“弱學習演算法“提升為“強學習演算法”的過程,主要思想是“三個臭皮匠頂個諸葛亮”。一般來說,找到弱學習演算法要 ...
  • java克隆 為什麼需要克隆 我們在很多時候需要使用一個對象去記錄另外一個對象的當前狀態,對象中可能會有很多屬性,如果我們一個一個去設置,不僅不方便,而且效率很低,我們看一個初學者可能遇到的問題 也許有的人認為Person p2=p1這樣的方式就可以克隆一個對象,這種想法是錯誤的,這種使用等號賦值的 ...
一周排行
    -Advertisement-
    Play Games
  • C#TMS系統代碼-基礎頁面BaseCity學習 本人純新手,剛進公司跟領導報道,我說我是java全棧,他問我會不會C#,我說大學學過,他說這個TMS系統就給你來管了。外包已經把代碼給我了,這幾天先把增刪改查的代碼背一下,說不定後面就要趕鴨子上架了 Service頁面 //using => impo ...
  • 委托與事件 委托 委托的定義 委托是C#中的一種類型,用於存儲對方法的引用。它允許將方法作為參數傳遞給其他方法,實現回調、事件處理和動態調用等功能。通俗來講,就是委托包含方法的記憶體地址,方法匹配與委托相同的簽名,因此通過使用正確的參數類型來調用方法。 委托的特性 引用方法:委托允許存儲對方法的引用, ...
  • 前言 這幾天閑來沒事看看ABP vNext的文檔和源碼,關於關於依賴註入(屬性註入)這塊兒產生了興趣。 我們都知道。Volo.ABP 依賴註入容器使用了第三方組件Autofac實現的。有三種註入方式,構造函數註入和方法註入和屬性註入。 ABP的屬性註入原則參考如下: 這時候我就開始疑惑了,因為我知道 ...
  • C#TMS系統代碼-業務頁面ShippingNotice學習 學一個業務頁面,ok,領導開完會就被裁掉了,很突然啊,他收拾東西的時候我還以為他要旅游提前請假了,還在尋思為什麼回家連自己買的幾箱飲料都要叫跑腿帶走,怕被偷嗎?還好我在他開會之前拿了兩瓶芬達 感覺感覺前面的BaseCity差不太多,這邊的 ...
  • 概述:在C#中,通過`Expression`類、`AndAlso`和`OrElse`方法可組合兩個`Expression<Func<T, bool>>`,實現多條件動態查詢。通過創建表達式樹,可輕鬆構建複雜的查詢條件。 在C#中,可以使用AndAlso和OrElse方法組合兩個Expression< ...
  • 閑來無聊在我的Biwen.QuickApi中實現一下極簡的事件匯流排,其實代碼還是蠻簡單的,對於初學者可能有些幫助 就貼出來,有什麼不足的地方也歡迎板磚交流~ 首先定義一個事件約定的空介面 public interface IEvent{} 然後定義事件訂閱者介面 public interface I ...
  • 1. 案例 成某三甲醫預約系統, 該項目在2024年初進行上線測試,在正常運行了兩天後,業務系統報錯:The connection pool has been exhausted, either raise MaxPoolSize (currently 800) or Timeout (curren ...
  • 背景 我們有些工具在 Web 版中已經有了很好的實踐,而在 WPF 中重新開發也是一種費時費力的操作,那麼直接集成則是最省事省力的方法了。 思路解釋 為什麼要使用 WPF?莫問為什麼,老 C# 開發的堅持,另外因為 Windows 上已經裝了 Webview2/edge 整體打包比 electron ...
  • EDP是一套集組織架構,許可權框架【功能許可權,操作許可權,數據訪問許可權,WebApi許可權】,自動化日誌,動態Interface,WebApi管理等基礎功能於一體的,基於.net的企業應用開發框架。通過友好的編碼方式實現數據行、列許可權的管控。 ...
  • .Net8.0 Blazor Hybird 桌面端 (WPF/Winform) 實測可以完整運行在 win7sp1/win10/win11. 如果用其他工具打包,還可以運行在mac/linux下, 傳送門BlazorHybrid 發佈為無依賴包方式 安裝 WebView2Runtime 1.57 M ...