验证码功能的思路和做法

验证码登录的思路和流程

步骤

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);
        }
    }
}
相关推荐
苍何5 小时前
Coding 真有质的飞跃?实测下豆包seed 2.1 pro
后端
苍何5 小时前
试了下腾讯 Marvis,回不去了...
后端
caibixyy5 小时前
springboot+langchain4j 实战 Day14——工具嵌入多 Agent(Tool-Equipped Multi-Agent)
后端
caibixyy5 小时前
springboot+langchain4j 实战 Day13 多 Agent 协作(Router + 子 Agent 分流)
后端
飘尘5 小时前
前端转全栈(Java 后端)必须要知道的:开发中的锁机制与分布式并发控制
前端·后端·全栈
苍何5 小时前
清华团队做了个具身智能大脑,有点东西!
后端
fliter5 小时前
强类型的诅咒,还是 Rust 类型系统的生存指南
后端
用户8356290780516 小时前
Python 操作 PDF 附件:添加、查看与管理指南
后端·python