1、首先我们在Maven工程里面的POM.XML中加入如下依赖。
<dependency>
<groupId>com.github.axet</groupId>
<artifactId>kaptcha</artifactId>
<version>0.0.9</version>
</dependency>
2、我们要在配置目录里,建立kaptcha的配置文件类,写一个返回DefaultKaptcha的方法,该方法需要设置一个Config的参数,逆向思维,建立Config的对象,该对象需要properties的对象,最终通过Properties的对象配置验证码的参数即可,我们把这个方法加入到IOC中,记得加入注解@Bean,记得给类加入@Configuration注解,具体配置代码如下:
package com.zdc.config;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import com.sun.prism.paint.Color;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;
@Configuration
public class kaptchaConfig {
@Bean
public DefaultKaptcha defaultKaptcha(){
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
Properties properties = new Properties();
properties.setProperty("kaptcha.border","yes");
properties.setProperty("kaptcha.border.color", "144,174,203");
properties.setProperty("kaptcha.border.thickness","1");
properties.setProperty("kaptcha.textproducer.char.length","4");
properties.setProperty("kaptcha.textproducer.font.names","MicroSoft YaHei");
properties.setProperty("kaptcha.textproducer.font.size","30");
properties.setProperty("kaptcha.textproducer.font.color","blue");
properties.setProperty("kaptcha.textproducer.char.space","2");
properties.setProperty("kaptcha.image.width","100");
properties.setProperty("kaptcha.image.height","40");
Config config = new Config(properties);
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}
3、我们建立Home控制器,利用defaultKaptcha对象的createText()方法获取验证码显示的字符,把该字符放入SESSION中,其中的KEY用的是一个静态常量Constants.KAPTCHA_SESSION_KEY,最终我们输出验证码图片。
package com.zdc.controller;
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
@Controller
@RequestMapping(value = "/home")
public class Home {
@Autowired
public DefaultKaptcha defaultKaptcha;
@RequestMapping(value = "/admin/login")
public String login(){
return "admin/login";
}
@RequestMapping(value = "/admin/getKaptcha")
public void getKaptcha(HttpServletRequest request, HttpServletResponse response){
try {
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
response.setHeader("Pragma", "no-cache");
response.setContentType("image/jpeg");
String capText = defaultKaptcha.createText();
request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
BufferedImage bi = defaultKaptcha.createImage(capText);
ServletOutputStream out = response.getOutputStream();
ImageIO.write(bi, "jpg", out);
try {
out.flush();
} finally {
out.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
4、在页面上显示验证码,前端采用的是Thymeleaf模板引擎,大家也可以采用其他模板引擎,具体显示验证码代码如下所示:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>我是登录页面</h1>
<img th:src="@{/home/admin/getKaptcha}" onclick="this.src='/home/admin/getKaptcha?random='+new Date()*1">
</body>
</html>
5、显示验证码