【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>
相关推荐
励志不掉头发的内向程序员1 分钟前
STL库——string(类函数学习)
开发语言·c++
刺客-Andy13 分钟前
React 第七十节 Router中matchRoutes的使用详解及注意事项
前端·javascript·react.js
前端工作日常28 分钟前
我对eslint的进一步学习
前端·eslint
一百天成为python专家29 分钟前
Python循环语句 从入门到精通
开发语言·人工智能·python·opencv·支持向量机·计算机视觉
Sunhen_Qiletian32 分钟前
朝花夕拾(五)--------Python 中函数、库及接口的详解
开发语言·python
hqwest41 分钟前
C#WPF实战出真汁07--【系统设置】--菜品类型设置
开发语言·c#·wpf·grid设计·stackpanel布局
禁止摆烂_才浅1 小时前
VsCode 概览尺、装订线、代码块高亮设置
前端·visual studio code
前路不黑暗@1 小时前
C语言:操作符详解(二)
c语言·开发语言·经验分享·笔记·学习·学习方法·visual studio
柳贯一(逆流河版)1 小时前
Spring 三级缓存:破解循环依赖的底层密码
java·spring·缓存·bean的循环依赖
程序员猫哥1 小时前
vue跳转页面的几种方法(推荐)
前端