基於Hutool的圖片驗證碼功能模塊實現

簡介

Hutool是一個小而全的Java工具類庫,通過靜態方法封裝,降低相關API的學習成本,提高工作效率,使Java擁有函數式語言般的優雅,讓Java語言也可以“甜甜的”。

Hutool中的工具方法來自每個用戶的精雕細琢,它涵蓋瞭Java開發底層代碼中的方方面面,它既是大型項目開發中解決小問題的利器,也是小型項目中的效率擔當;

Hutool是項目中“util”包友好的替代,它節省瞭開發人員對項目中公用類和公用工具方法的封裝時間,使開發專註於業務,同時可以最大限度的避免封裝不完善帶來的bug。

Hutool名稱的由來

Hutool = Hu + tool,是原公司項目底層代碼剝離後的開源庫,“Hu”是公司名稱的表示,tool表示工具。Hutool諧音“糊塗”,一方面簡潔易懂,一方面寓意“難得糊塗”。

基於Hutool的圖片驗證碼功能模塊實現

1.背景

為瞭提高系統的安全性,防止接口被暴力刷新,驗證碼是個好的手段,圖片驗證碼沒有短信驗證碼的費用,其是個人開發者學習的重點。

2.方案設計

2.1 方案步驟

  • 引入Hutool工具集
  • 安裝Redis
  • 接入Reids配置

2.2 Hutool工具類引入

<dependency>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
        <version>5.8.8</version>
    </dependency>
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>3.7.0</version>
    </dependency>

2.3 安裝Redis

默認端口:6379
- 下載連接 https://github.com/tporadowski/redis/releases
- 解壓
- 雙擊redis-server.exe啟動服務端
- 雙擊redis-cli.exe啟動客戶端連接服務端

2.4 接入Reids配置

在ClassPath(或者src/main/resources)的config目錄下下新建redis.setting

#-------------------------------------------------------------------------------
# Redis客戶端配置樣例
# 每一個分組代表一個Redis實例
# 無分組的Pool配置為所有分組的共用配置,如果分組自己定義Pool配置,則覆蓋共用配置
# 池配置來自於:https://www.cnblogs.com/jklk/p/7095067.html
#-------------------------------------------------------------------------------

#----- 默認(公有)配置
# 地址,默認localhost
host = localhost
# 端口,默認6379
port = 6379
# 超時,默認2000
timeout = 2000
# 連接超時,默認timeout
connectionTimeout = 2000
# 讀取超時,默認timeout
soTimeout = 2000
# 密碼,默認無
# 如果提示密碼錯誤換成:Auth = 
password = 
# 數據庫序號,默認0
database = 0
# 客戶端名,默認"Hutool"
clientName = Hutool
# SSL連接,默認false
ssl = false;

#----- 自定義分組的連接
[custom]
# 地址,默認localhost
host = localhost
# 連接耗盡時是否阻塞, false報異常,ture阻塞直到超時, 默認true
BlockWhenExhausted = true;
# 設置的逐出策略類名, 默認DefaultEvictionPolicy(當連接超過最大空閑時間,或連接數超過最大空閑連接數)
evictionPolicyClassName = org.apache.commons.pool2.impl.DefaultEvictionPolicy
# 是否啟用pool的jmx管理功能, 默認true
jmxEnabled = true;
# 是否啟用後進先出, 默認true
lifo = true;
# 最大空閑連接數, 默認8個
maxIdle = 8
# 最小空閑連接數, 默認0
minIdle = 0
# 最大連接數, 默認8個
maxTotal = 8
# 獲取連接時的最大等待毫秒數(如果設置為阻塞時BlockWhenExhausted),如果超時就拋異常, 小於零:阻塞不確定的時間,  默認-1
maxWaitMillis = -1
# 逐出連接的最小空閑時間 默認1800000毫秒(30分鐘)
minEvictableIdleTimeMillis = 1800000
# 每次逐出檢查時 逐出的最大數目 如果為負數就是 : 1/abs(n), 默認3
numTestsPerEvictionRun = 3;
# 對象空閑多久後逐出, 當空閑時間>該值 且 空閑連接>最大空閑數 時直接逐出,不再根據MinEvictableIdleTimeMillis判斷  (默認逐出策略)
SoftMinEvictableIdleTimeMillis = 1800000
# 在獲取連接的時候檢查有效性, 默認false
testOnBorrow = false
# 在空閑時檢查有效性, 默認false
testWhileIdle = false
# 逐出掃描的時間間隔(毫秒) 如果為負數,則不運行逐出線程, 默認-1
timeBetweenEvictionRunsMillis = -1

2.5 方案交互圖

client server redis uuid generate: Image code JSON)(code, time) setnx(uid, JSON) save result send result opt [send option] uid + code get uid JSON(code, time) check code true? check result opt [check option] client server redis

3.模塊編寫

VerificationImageCodeService.java

package com.example.basePro.sevice;

import com.example.basePro.bean.dto.Result;
import com.example.basePro.bean.model.ImageCode;

/**
 * @Author: 
 * @Date: 2022/10/16
 * @Description:
 */
public interface VerificationImageCodeService {

    /**
     * 生成圖片驗證碼對應的編碼
     * @param uuid 用戶id
     * @return 圖片的base64編碼
     */
    String generateImageCode(String uuid);

    /**
     * 驗證圖片驗證碼內容
     * @param uuid 用戶id
     * @param code 驗證碼內容
     * @return 驗證結果
     */
    Result<String> chechImageCode(String uuid, String code) throws Exception;
}

package com.example.basePro.sevice.Impl;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.db.nosql.redis.RedisDS;
import cn.hutool.json.JSONUtil;
import com.example.basePro.bean.dto.Result;
import com.example.basePro.bean.model.ImageCode;
import com.example.basePro.sevice.VerificationImageCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import redis.clients.jedis.Jedis;

/**
 * @Author: 
 * @Date: 2022/10/16
 * @Description:
 */

@Slf4j
@Service
public class VerificationImageCodeServiceImpl implements VerificationImageCodeService {

    /**
     * 驗證碼失效時間
     */
    @Value("${constants.expire.second: 600}")
    private Long expireSecond;


    @Override
    public String generateImageCode(String uuid) {
        String key = uuid;
        Jedis jedis = RedisDS.create().getJedis();

        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(100, 50);
        log.info("image code is {}", lineCaptcha.getCode());

        jedis.del(key);

        // 存入緩存
        ImageCode imageCode = ImageCode.builder()
                .code(lineCaptcha.getCode())
                .createTime(System.currentTimeMillis())
                .build();
        jedis.setex(key, expireSecond, JSONUtil.toJsonStr(imageCode));

        // 瀏覽器中輸入 data:image/png;base64, + base64, 即可顯示圖片
        return lineCaptcha.getImageBase64();
    }

    @Override
    public Result<String> chechImageCode(String uuid, String code) throws Exception {
        String key = uuid;

        Jedis jedis = RedisDS.create().getJedis();

        // 獲取緩存中數據
        String cache = jedis.get(key);
        ImageCode imageCode = JSONUtil.toBean(cache, ImageCode.class);

        if (StringUtils.isEmpty(cache)){
            throw new Exception("驗證碼已過期");
        }

        // 驗證碼不匹配
        if(!code.equals(imageCode.getCode())){
            throw new Exception("驗證碼錯誤");
        }
        jedis.del(key);
        return Result.success("驗證成功");
    }
}

4.接口測試

訪問獲取圖片驗證碼接口:

http://localhost:8080/captcha/getcode?uuid=123456

iVBORw0KGgoAAAANSUhEUgAAAGQAAAAyCAYAAACqNX6+AAAKQ0lEQVR42uXbC1BVxxkHcOokNWmTmj4yRE18YJIhtQ5WQ1qinaaWhNBGI42ZVFKSNolYjUgUjE181ASDGqsVZrRUI5qOoiABc1MBZURAUEAQFdErIIjIwyqC8n5u2cVd9uzZPWfPuYeH7Tfznztz7547cH73291zLjgBkxX5zmIUXrl71YL/tYr22GzZe303cgxK3HOrUOhyMvOGY171R3ky/KAmjF7VVrijyJbNOVj13BaPZBQrau62lYrwUKyEwUXD6IK4/PktEhEMjB7MBf+X+vUTTMM0LonvFyCM5AhK8LOXFbnw5o8VMIY6hMZhgWRgIMpAwViF0t9TGAvk5Mib8XDYrnGkW+xFeZqvv2Mbp5v+RrECZvP20SRO/fkDi2BivEajmEXZNLLI1FR2Lyz4/QqiVzIwoi6RRfEd+wLKUKs3vYNRBh3kiOcMFCMwjnYJDSMCuhXgpshAw8D87N3hg9chPBi6xsaGo8e8iokOd4kVQAOBBFGcBrt1MQyNs7o8CgWiwGAU/DwbK2qoTG2DDiLTNRjm/6E0QSb62qXfaOPFGId/mJYVGSSiaWwowMS8997gdQiLUlF0C+wJzSZhUWA8h8UqAqutthlcs9mBfVMmyA9KAqeXJKDHovAsUJVYBNrrW6Rw4PRlFYy7bwOJoyiJD+hPd6UF/wE+P9yE4tCUhVHaWjrAV2GnhSAsDK5352wD2QHfgLzFh4SBQPYFy6VOxijX36CwKPPvd0YZCBARigim6U4bWOgeaRzk/ZoAFB5Kpq1EgSECoWH2HYjXhGBTlVQsfUIgCq8wjB6OoyAYRQ+mtbkDrHglmmCY6hAW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K3XGxW3UOA3gTcyykFnU3vvzcCeR/svN6Pn6b8+EX2nLvrUH5w23VCH2I5FogwmiOLai4JRXKnLoshseydvb0AoCTs/MvRXJ9dTy0xNQyyK2SkLY9HpbxAaRnVzUQZF9o8cMMyatQUgNzBBG+ODf4PU2K2G1oXwsC8sB6HrvqZxJHoV/L2pKDcbQlAs/T7ECEhOQJnueFvECTR9rfvkLIhbng6yApN6gZbawFm/feBr34Ug4uXe7WhiySoU2Q4RoZgFufzYcO5YGkcPiIbBUCzIcpd/KeIQiOor3x4UPRiIAsN2DwyusF//iEQLhi2MwltLRPVQ5BGC4eGn3AFCFBEMC6RXLAzGYQvDmAKpdZrPDQszZYOLFAyvWBwZFLpb9Kqzs69DpvrdRkA4RmDo+vbT30LRgsEVMN/DuinLSMdAFEdgaBxZGF6dOj5ckcLLXQTkt4saVd1D45iFEeGwIMHXzpH06/+HWA0jWyKUqhu9V/O19d0gcEMzAfF/8QzIGbVOFbN1uG0tihYOBDman6XKkPmHHatR2Lp0pUuxiNM5dLyD/4HiIBmBomHYKU13yvrJ6CgwlGtKxn0OHR93tJ2LEbC+2dT7Pd9+3jSMVik65F5AgQlueAXFSO1PakdrxbS3G8D0PzaAP3zcBL60tdFfPFqCMmnkMYd+xwEHeW11GImovFw7NMPCGMWxqtxrcxFK0IhHLENRrSGfzbqNMhAlgzN1UrFux6CdyiDAYBBYEAXHUhAWhsYJLXhOEVj24mn9+ktrgfDWl4HsGgjCm7owjExFe81BMXQdwsPhAWEkq8pt9sskZkoGpu5X/yBRrRGnnVXhgfBqbcnDiq6Rhfkvk4dJUhycimoAAAAASUVORK5CYII=

訪問驗證接口:

http://localhost:8080/captcha/check?uuid=123456&code=****

5.參考文獻

Redis客戶端封裝-RedisDS

到此這篇關於基於Hutool的圖片驗證碼功能模塊實現的文章就介紹到這瞭,更多相關Hutool圖片驗證碼內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀:

    None Found