Java生成图形验证码

1、加依赖

复制代码
<dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.16</version>
        </dependency>

2、写接口,这块不需要登录成功才能操作的,所以写controller就行了,不涉及服务

复制代码
package com.hmblogs.backend.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.hmblogs.backend.config.CaptchaProperties;
import com.hmblogs.backend.util.JedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@RestController
@RequestMapping("/captcha")
public class CaptchaController {

    @Autowired
    private CaptchaProperties captchaProp;

    @RequestMapping("/get")
    public void getCaptcha(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        // 定义图形验证码的长和宽(配置默认值)
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(captchaProp.getWidth(), captchaProp.getHeight());
        // 细节问题,不影响程序
        // 设置返回类型
        response.setContentType("image/jpeg");
        // 静止缓存
        response.setHeader("Progma", "No-cache");
        try {
            // 图形验证码写出,可以写出到文件,也可以写出到流
            lineCaptcha.write(response.getOutputStream());
            // 这里是缓存图形验证码逻辑,也可以放库里,或者session里,但要用用户名区别,因为还没登录所以不要使用用户ID区别
            // 在相关功能(例如登录)的时候,要对应验证判断一下
            Jedis jedis = JedisUtil.getJedisConn();
            String username = request.getParameter("username");
            jedis.setex("imageToken:"+username,2*60, lineCaptcha.getCode());
            // 关流
            response.getOutputStream().close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}

3、对应的配置文件

复制代码
package com.hmblogs.backend.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Data
@Configuration
@ConfigurationProperties(prefix = "captcha")
public class CaptchaProperties {
    private Integer width;
    private Integer height;
}

4、对应的配置文件的配置

复制代码
captcha:
  width: 200
  height: 100

5、开发页面,使用的是vue的ref来控制页面图形验证码区域显示,点击图片区域则会换一个图形验证码

6、验证

页面加载时,有调用该接口

查看redis的缓存,和页面看到的一致

点击图形区域,发现又调用了该接口

查看redis缓存

验证完毕。

相关推荐
lichenyang45319 分钟前
Docker 学习笔记(一):为什么需要镜像、容器和仓库?
前端
kyriewen31 分钟前
别再对着 TypeScript 报错发呆了:我把 10 个最常见的红色波浪线翻译成了人话
前端·javascript·typescript
IT_陈寒32 分钟前
SpringBoot自动配置的坑,我的API突然就404了
前端·人工智能·后端
奇奇怪怪的1 小时前
Embedding 模型 10+ 横向评测
前端
陈广亮1 小时前
Monorepo 从 0 到 1 实操指南 2026 版:pnpm catalogs + Turborepo 2.x + changesets 全链路
前端
子兮曰1 小时前
OpenMontage 深度解剖:你的 AI 编程助手,其实是个视频工作室
前端·后端·ai编程
敲代码的鱼2 小时前
PDF 预览与签名批注写回 支持安卓 iOS 鸿蒙 UTS插件
android·前端·ios
子兮曰2 小时前
前端工具链的「Rust 化」:一场没有赢家的军备竞赛?
前端·后端·rust
Hyyy3 小时前
Function Calling / Tool Use的原理和实现模式
前端·llm·ai编程
爱勇宝3 小时前
从 Ctrl+CV 到 Enter:程序员正在失去什么
前端·后端·程序员