kaptcha 验证码生成工具在springboot中集成

今天分享一个生成验证码的工具kaptcha 。

Kaptcha 是 Google 开发的一个简单实用的验证码生成库,基于 javax.imageio 实现,配置灵活,易于集成到 Spring/Spring Boot 项目中。

maven项目中依赖

复制代码
<!-- Maven 依赖 -->
<dependency>
    <groupId>com.github.penggle</groupId>
    <artifactId>kaptcha</artifactId>
    <version>2.3.2</version>
</dependency>

springboot中集成:

首先需要定义一个DefaultKaptcha的bean,用于生成验证码的图片和文字配置。以下是两个经典的案例(实际应用中可以根据需要设置纯数字或者算式,我们项目中只是生成的纯数字比较简单,我们是生成之后输出前端,然后存入redis中生成uuid做为验证批次号。验证时候前端传回来用户输的和uuid在redis中进行匹配,这个比较简单)下面直接上代码

复制代码
@Configuration
public class KaptchaConfig {
    
    @Bean
    public DefaultKaptcha defaultKaptcha() {
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        Properties properties = new Properties();
        
        // 图片样式配置
        properties.setProperty("kaptcha.border", "yes"); // 有边框
        properties.setProperty("kaptcha.border.color", "105,179,90"); // 边框颜色
        properties.setProperty("kaptcha.image.width", "160"); // 宽度
        properties.setProperty("kaptcha.image.height", "60"); // 高度
        
        // 文本配置
        properties.setProperty("kaptcha.textproducer.font.color", "blue"); // 字体颜色
        properties.setProperty("kaptcha.textproducer.font.size", "35"); // 字体大小
        properties.setProperty("kaptcha.textproducer.char.length", "4"); // 字符个数
        properties.setProperty("kaptcha.textproducer.char.space", "3"); // 字符间距
        properties.setProperty("kaptcha.textproducer.font.names", "Arial,Courier"); // 字体
        
        // 干扰配置
        properties.setProperty("kaptcha.noise.color", "red"); // 干扰线颜色
        properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.DefaultNoise"); // 干扰实现
        
        // 背景配置
        properties.setProperty("kaptcha.background.clear.from", "white"); // 背景渐变开始
        properties.setProperty("kaptcha.background.clear.to", "white"); // 背景渐变结束
        
        Config config = new Config(properties);
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }
    
    // 数学算式验证码配置(如:1+2=?)
    @Bean
    public DefaultKaptcha mathKaptcha() {
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        Properties properties = new Properties();
        
        properties.setProperty("kaptcha.image.width", "160");
        properties.setProperty("kaptcha.image.height", "60");
        properties.setProperty("kaptcha.textproducer.font.size", "35");
        
        // 使用数学算式文本生成器
        properties.setProperty("kaptcha.textproducer.impl", 
            "com.xxx.MathTextCreator"); // 自定义类
        
        Config config = new Config(properties);
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }
}

controller实现

复制代码
@RestController
@RequestMapping("/captcha")
public class CaptchaController {
    
    @Autowired
    private DefaultKaptcha defaultKaptcha;
    
    /**
     * 生成验证码图片
     */
    @GetMapping("/image")
    public void getCaptcha(HttpServletRequest request, 
                          HttpServletResponse response) throws IOException {
        // 设置响应头
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", 
            "no-store, no-cache, must-revalidate");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
        response.setContentType("image/jpeg");
        
        // 生成验证码文本
        String captchaText = defaultKaptcha.createText();
        
        // 存储到Session(推荐使用Redis存储分布式应用)
        request.getSession().setAttribute("CAPTCHA_SESSION_KEY", captchaText);
        
        // 可选:存储到Redis(分布式场景)或者生成UUID传给前端
         String sessionId = request.getSession().getId();
        redisTemplate.opsForValue().set(
            "captcha:" + sessionId, 
           captchaText, 
            5, TimeUnit.MINUTES
        );
        
        // 生成图片并写出
        BufferedImage image = defaultKaptcha.createImage(captchaText);
        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image, "jpg", out);
        
        try {
            out.flush();
        } finally {
            out.close();
        }
    }
    
    /**
     * 验证码验证
     */
    @PostMapping("/verify")
    public ApiResult verifyCaptcha(@RequestParam String code,
                                   HttpServletRequest request) {
        // 从Session获取验证码
        String sessionCaptcha = (String) request.getSession()
            .getAttribute("CAPTCHA_SESSION_KEY");
        
        // 从Redis获取(分布式场景)
        String sessionId = request.getSession().getId();
         String redisCaptcha = redisTemplate.opsForValue()
            .get("captcha:" + sessionId);
        
        if (StringUtils.isBlank(sessionCaptcha)) {
            return ApiResult.error("验证码已失效");
        }
        
        if (StringUtils.isBlank(code)) {
            return ApiResult.error("请输入验证码");
        }
        
        if (!sessionCaptcha.equalsIgnoreCase(code.trim())) {
            return ApiResult.error("验证码错误");
        }
        
        // 验证成功后清除验证码
        request.getSession().removeAttribute("CAPTCHA_SESSION_KEY");
        redisTemplate.delete("captcha:" + sessionId);
        //处理后续登陆操作
        return ApiResult.success("验证码正确");
    }
}
相关推荐
Flynt34 分钟前
Spring AI Alibaba 真停更了?我把它的 GitHub 翻了个底朝天
java·spring·开源
ServBay3 小时前
AI 做的小工具,怎样交给同事用?
后端·aigc·ai编程
用户69371750013843 小时前
AI 越强,为什么我们反而越来越担心失业?
前端·后端·github
夜郎king3 小时前
Shapefile 一键生成 GeoServer 样式:GeoTools + Spring Boot 实现 YSLD 自动生成器
spring boot·gis·geotools·ysld
她的男孩4 小时前
Spring Boot 接 Flowable:请假单要同时走财务和行政,包容分支和默认分支怎么配
java·spring boot·工作流引擎
咖啡八杯5 小时前
LoginUser 适配器模式:适配 SysUser 到 UserDetails
java·开发语言·后端·框架·适配器模式·若依
拖孩5 小时前
又又又用 AI 写了个小程序:我站起来猛蹬 AI,使劲薅羊毛
前端·后端·微信小程序
北冥有鱼6 小时前
从 bean::method 里抠出注解:Java 方法引用元数据反查全记录
java·后端
CoderYanger6 小时前
Java EE 进阶:11. SpringBoot 统一功能处理(第四弹):@ControllerAdvice 源码分析+案例代码
java·数据库·spring boot·后端·spring·java-ee·mybatis
Sailing7 小时前
🔥🔥sql: waiting for connection 踩坑:不要一味调大 MaxOpenConns
后端·go·gin