spring boot3 验证码工具kaptcha使用
1、pom.xml文件,下载验证码工具kaptcha依赖
<!--验证码工具kaptcha的依赖-->
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
2、创建CaptchaConfig 配置工具类
package com.pn.config;
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;
/**
* 验证码工具kaptcha的配置类
*/
@Configuration
public class CaptchaConfig {
/**
* 配置Producer接口的实现类DefaultKaptcha的bean对象,该对象用于生成验证码图片;
* 并给其指定生成的验证码图片的设置项;bean对象的id引用名为captchaProducer;
*/
@Bean(name = "captchaProducer")
public DefaultKaptcha captchaProducer() {
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
//properties 属性集对象 -- 对properties文件进行封装,直接再这配置属性,也就是map
Properties properties = new Properties();
//是否有边框 默认为true 我们可以自己设置yes,no
properties.setProperty("kaptcha.border", "yes");
//边框颜色 默认为Color.BLACK
properties.setProperty("kaptcha.border.color", "105,179,90");
//验证码文本字符颜色 默认为Color.BLACK
properties.setProperty("kaptcha.textproducer.font.color", "blue");
//验证码图片宽度 默认为200
properties.setProperty("kaptcha.image.width", "120");
//验证码图片高度 默认为50
properties.setProperty("kaptcha.image.height", "40");
//验证码文本字符大小 默认为40
properties.setProperty("kaptcha.textproducer.font.size", "32");
//KAPTCHA_SESSION_KEY
properties.setProperty("kaptcha.session.key", "kaptchaCode");
//验证码文本字符间距 默认为2
properties.setProperty("kaptcha.textproducer.char.space", "4");
//验证码文本字符长度 默认为5
properties.setProperty("kaptcha.textproducer.char.length", "4");
//验证码文本字体样式 默认为new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)
properties.setProperty("kaptcha.textproducer.font.names", "Arial,Courier");
//验证码噪点颜色 默认为Color.BLACK
properties.setProperty("kaptcha.noise.color", "gray");
Config config = new Config(properties);
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}
3、创建LoginController 控制器使用
package com.pn.controller;
import com.google.code.kaptcha.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.time.Duration;
@RestController
public class LoginController {
//通过接口拿到kaptcha验证码工具类
@Resource(name = "captchaProducer")
private Producer producer;
//0库
@Autowired
@Qualifier("reactiveRedisTemplateDb0")
private ReactiveRedisTemplate<String, Object> redis;
@RequestMapping("/captcha/captchaImage")
public void captchaImage(HttpServletResponse response) throws Exception {
//ServletOutputStream 实例字节流
ServletOutputStream out = null;
try {
//producer.createText() 生成图片文件
String text = producer.createText();
//BufferedImage存放再内存中,producer.createImage() 生成图片
BufferedImage img = producer.createImage(text);
//将验证码文本保存到Reids中,设置过期时间,为什么要放key? 多个账号都需要验证码,这样查询对比就方便省事,反正放key就对了
//博主使用的时Reids异步非阻塞方式,参考博主的Reids异步非阻塞
//或者使用自己的方式
//redis.opsForValue().set(text, "",Duration.ofSeconds(20)).subscribe();
//设置响应头Content - Type 字段设置成 image/jpeg
response.setContentType("image/jpeg");
//response.getOutputStream() 开启实例字节流
out = response.getOutputStream();
//ImageIO.write()
ImageIO.write(img, "jpg", out);
//刷新
out.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
//关闭字节流
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}