【Java Web】使用Kaptcha生成验证码

1. 导入jar包

java 复制代码
<dependency>
	<groupId>com.github.penggle</groupId>
	<artifactId>kaptcha</artifactId>
	<version>2.3.2</version>
</dependency>

2. Kaptcha配置类

java 复制代码
package com.nowcoder.community.config;

import com.google.code.kaptcha.Producer;
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 Producer kaptchaProducer(){
        Properties properties = new Properties();
        properties.setProperty("kaptcha.image.width", "100");  // 宽
        properties.setProperty("kaptcha.image.height", "40");  // 高
        properties.setProperty("kaptcha.textproducer.font.size", "32");  // 字号
        properties.setProperty("kaptcha.textproducer.char.color", "black");  //颜色
        properties.setProperty("kaptcha.textproducer.char.string", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"); // 字符
        properties.setProperty("kaptcha.textproducer.char.length", "4"); // 长度
        properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.NoNoise");  // 干扰方式

        DefaultKaptcha kaptcha = new DefaultKaptcha();
        Config config = new Config(properties);
        kaptcha.setConfig(config);
        return kaptcha;
    }
}

3. Controller

java 复制代码
@Autowired
private Producer kaptchaProducer;

@RequestMapping(path="/kaptcha", method = RequestMethod.GET)
public void getKaptcha(HttpServletResponse response, HttpSession session){
    // 生成验证码
    String text = kaptchaProducer.createText();
    BufferedImage image = kaptchaProducer.createImage(text);

    // 验证码是敏感信息,需将验证码存入session
    session.setAttribute("kaptcha", text);

    // 将图片输出给浏览器
    response.setContentType("image/png");
    try{
        OutputStream os = response.getOutputStream();
        ImageIO.write(image, "png", os);
    } catch (IOException e) {
        logger.error("响应验证码失败:"+e.getMessage());
    }
}

4. 前端刷新验证码

java 复制代码
<div class="col-sm-4">
	<img th:src="@{/kaptcha}" id="kaptcha" style="width:100px;height:40px;" class="mr-2"/>
	<a href="javascript:refresh_kaptcha();" class="font-size-12 align-bottom">刷新验证码</a>
</div>


<script>
	function refresh_kaptcha(){
		var path = CONTEXT_PATH + "/kaptcha?p=" + Math.random();
		$("#kaptcha").attr("src",path);
	}
</script>
相关推荐
SunnyDays10111 分钟前
使用 Java 高效删除 Excel 空白行与空白列
java·删除 excel 空白行·删除 excel 空白列
笨手笨脚の2 分钟前
Java 性能优化
java·jvm·数据库·性能优化·分布式锁·分布式事务·并发容器
木斯佳2 分钟前
前端八股文面经大全:腾讯前端暑期提前批一、二、三面面经(上)(2026-03-04)·面经深度解析
前端
l软件定制开发工作室6 分钟前
Spring开发系列教程(32)——Spring Boot开发
java·spring boot·后端·spring
亚马逊云开发者8 分钟前
MCP 协议实战:用 Amazon Bedrock 让 AI Agent 安全调用云服务的完整方案
开发语言·qt·安全
嘉琪0018 分钟前
Day4 完整学习包(this 指向)——2026 0313
前端·javascript·学习
前端小菜鸟也有人起8 分钟前
Vue3父子组件通信方法总结
前端·javascript·vue.js
co_wait8 分钟前
【C++ STL】排序算法
开发语言·c++·排序算法
DolphinScheduler社区9 分钟前
Apache DolphinScheduler 3.4.1 发布,新增任务分发超时检测
java·数据库·开源·apache·海豚调度·大数据工作流调度