基于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/KAmjF7VVrijyJbNOVj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H06/+HWA0jWyKUqhu9V/O19d0gcEMzAfF/8QzIGbVOFbN1uG0tihYOBDman6XKkPmHHatR2Lp0pUuxiNM5dLyD/4HiIBmBomHYKU13yvrJ6CgwlGtKxn0OHR93tJ2LEbC+2dT7Pd9+3jSMVik65F5AgQlueAXFSO1PakdrxbS3G8D0PzaAP3zcBL60tdFfPFqCMmnkMYd+xwEHeW11GImovFw7NMPCGMWxqtxrcxFK0IhHLENRrSGfzbqNMhAlgzN1UrFux6CdyiDAYBBYEAXHUhAWhsYJLXhOEVj24mn9+ktrgfDWl4HsGgjCm7owjExFe81BMXQdwsPhAWEkq8pt9sskZkoGpu5X/yBRrRGnnVXhgfBqbcnDiq6Rhfkvk4dJUhycimoAAAAASUVORK5CYII=

访问验证接口:

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

5.参考文献

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不染心

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值