1.添加依赖
<!--验证码工具kaptcha的依赖-->
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
2.添加配置类
个人觉得可以理解为设置验证码图片的格式
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 getKaptchaBean() {
//创建DefaultKaptcha对象
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.实现接口
根据captchaProducer生成验证码文本,再通过文本生成验证码图片,通过redis设置验证码过期时间
//@ResponseBody+@Controller
@RestController
public class LoginController {
//两种注入方式:1.@Autowired,类型注入;2.@Resource,名称注入
//注入kaptcha的验证码生成器
@Resource(name = "captchaProducer")
private Producer producer;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Qualifier("redisTemplate")
@Autowired
private RedisTemplate redisTemplate;
/*
* 生成验证码图片的url接口/captcha/captchaImage
*/
@RequestMapping("/captcha/captchaImage")
public void captchaImage(HttpServletResponse response){
ServletOutputStream out = null;
try {
//生成验证码图片的文件
String text = producer.createText();
//使用text生成验证码图片(BufferedImage对象代表生成的验证码图片保存在内存中)
BufferedImage image = producer.createImage(text);
//将验证码文本作为键保存在redis中,设置10分钟过期
redisTemplate.opsForValue().set(text,"",60*10, TimeUnit.SECONDS);
//设置响应头为图片类型
response.setContentType("image/jpeg");
//将图片以流的形式响应给前端
out = response.getOutputStream();
ImageIO.write(image,"jpg",out);//使用响应对象字节输出流写出图片
//刷新输出流
out.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭输出流
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}