验证码功能的思路和做法

验证码登录的思路和流程

步骤

1.导入依赖

复制代码
<dependency>
    <groupId>com.github.axet</groupId>
    <artifactId>kaptcha</artifactId>
    <version>0.0.9</version>
</dependency>

2.写一个验证码的配置类

复制代码
package com.lzy.config;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;

@Configuration
public class KaptchaConfig {
   @Bean
   public DefaultKaptcha producer() {
      //验证码长宽高
      Properties properties = new Properties();
      properties.put("kaptcha.border", "no");
      properties.put("kaptcha.textproducer.font.color", "black");
      properties.put("kaptcha.textproducer.char.space", "4");
      properties.put("kaptcha.image.height", "40");
      properties.put("kaptcha.image.width", "120");
      properties.put("kaptcha.textproducer.font.size", "30");
      Config config = new Config(properties);
      DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
      defaultKaptcha.setConfig(config);
      return defaultKaptcha;
   }
}

3.使用这个类,创建一个uuid作为key,并得到一个随机验证码,再将验证码转化为图片,再转化为base64进制,并且按上对应的前缀

复制代码
package com.lzy.controller;

import cn.hutool.json.JSONObject;
import com.lzy.common.lang.Result;
import com.lzy.util.Constants;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.UUID;

@RestController
public class CaptureController extends BaseController {

    @GetMapping("/captcha")
    public Result captcha() {
        // 生成uuid作为验证码的唯一标识
        UUID uuid = UUID.randomUUID();
        // 生成验证码
        String text = producer.createText();
        BufferedImage image = producer.createImage(text);
        String base64Image = convertImageToBase64(image);
        // 将验证码存入redis
        redisUtil.hset(Constants.CAPTURE , uuid.toString(), text, 120);
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("token", uuid.toString());
        jsonObject.set("captchaImg", base64Image);
        return Result.success(jsonObject);
    }
    private String convertImageToBase64(BufferedImage image) {
        try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
            ImageIO.write(image, "png", baos);
            byte[] imageBytes = baos.toByteArray();
            String base64Image = Base64.getEncoder().encodeToString(imageBytes);
            return "data:image/png;base64," + base64Image;
        } catch (IOException e) {
            throw new RuntimeException("Error converting image to Base64", e);
        }
    }
}
相关推荐
深入云栈7 小时前
Netty 4.2.x 源码深度解析 (十一):EventExecutor 业务线程池 —— 防止 IO 线程阻塞的并发模型
java·后端
吴声子夜歌7 小时前
Java——基本类型
java·开发语言
柠檬味拥抱7 小时前
鸿蒙 AI 对话实战:用蓝耘 MaaS 给礼物 App 装上「多轮对话选礼大脑」
后端
唐青枫7 小时前
别再把 build.zig 当配置文件:Zig 构建系统从零到实战
后端
Naylor7 小时前
只借不占:Rust 的引用与借用
后端·rust
大勇前进8 小时前
Python装饰器、生成器、上下文管理器:3个必会的"魔法"机制
后端
Go_error8 小时前
Fyne:让 Go 开发者也能玩转 GUI
后端·go
用户0795675043148 小时前
Python + OpenCV 识别 GIF 中旋转最快的图形:基于时间周期的动态识别
后端
长大19888 小时前
Python+Flask 1小时搭建个人博客:比WordPress轻量10倍
后端
闪学it8 小时前
Python测试开发进阶线上班28期
后端