1、依赖
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
2、控制类
@Controller
@RequestMapping("/kaptcha")
public class KaptchaController {
@Autowired
private Producer producer;
@RequestMapping("")
public void index(HttpServletRequest request, HttpServletResponse response) {
System.out.println("========kaptcha========");
response.setDateHeader("Expires", 0);
// Set standard HTTP/1.1 no-cache headers.
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
// Set IE extended HTTP/1.1 no-cache headers (use addHeader).
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
// Set standard HTTP/1.0 no-cache header.
response.setHeader("Pragma", "no-cache");
// return a jpeg
response.setContentType("image/jpeg");
// create the text for the image
String capText = producer.createText();
System.out.println("---------capText:"+capText);
// create the image with the text
BufferedImage bi = producer.createImage(capText);
ServletOutputStream out = null;
try {
out = response.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
// write the data out
try {
ImageIO.write(bi, "jpg", out);
} catch (IOException e) {
e.printStackTrace();
}
try {
try {
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3 编写WebConfig
不知道你有没有发现个问题:使用springboot 后就不用采用xml方式注入bean了,那问题是KaptchaController类中的producer
怎么注入呢?
@Autowired
private Producer producer
现在上菜:
import java.util.Properties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter{
/**
* 验证码生成相关
*/
@Bean
public DefaultKaptcha kaptcha() {
Properties properties = new Properties();
properties.put("kaptcha.border", "no");
properties.put("kaptcha.border.color", "105,179,90");
properties.put("kaptcha.textproducer.font.color", "green");
properties.put("kaptcha.image.width", "125");
properties.put("kaptcha.image.height", "45");
properties.put("kaptcha.textproducer.font.size", "45");
properties.put("kaptcha.session.key", "code");
properties.put("kaptcha.textproducer.char.length", "6");
properties.put("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
Config config = new Config(properties);
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}
就是通过上面的配置类注入Producer接口的实现类 :com.google.code.kaptcha.impl.DefaultKaptcha
kaptcha生成验证的核心接口类有:
1、com.google.code.kaptcha.Producer,该接口目前只有一个默认实现类:com.google.code.kaptcha.impl.DefaultKaptcha
2、com.google.code.kaptcha.text.TextProducer,此接口目前只有一个默认实现类:com.google.code.kaptcha.text.impl.DefaultTextCreator
其中Producer是用来处理生成图片的,TextProducer是用来处理生成验证码问题的。
更多关于验证码生成的配置参数可以参考这篇文章: