集成 kaptcha 框架,pom.xml 中注入依赖
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
初始化 kaptcha
@Bean
public DefaultKaptcha initKaptcha(){
DefaultKaptcha kaptcha = new DefaultKaptcha();
Properties properties = new Properties();
Config config = new Config(properties);
kaptcha.setConfig(config);
return kaptcha;
}
生成图片
- 生成 Http Cache 响应头信息
- 设置 Http 内容格式
- 创建随机数
- 输出图片
完整代码
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Properties;
//验证码
@Controller
public class CaptchaControl {
@Autowired
private DefaultKaptcha kaptcha;
@RequestMapping(value = "/captcha")
public void getCaptcha(HttpServletResponse response,
HttpSession session) throws IOException {
// 设置 HTTP Cache 响应头
// Set to expire far in the past.
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");
// 设置 HTTP 内容格式
// return a jpeg
response.setContentType("image/png");
// 创建随机数
// create the text for the image.
String capText = kaptcha.createText();
// store the text in the session.
session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
// 输出图片
// create the image with the text.
BufferedImage bufferedImage = kaptcha.createImage(capText);
// write the data out.
ServletOutputStream outputStream = response.getOutputStream();
ImageIO.write(bufferedImage, "png", outputStream);
try {
outputStream.flush();
} finally {
outputStream.close();
}
}
@Bean
public DefaultKaptcha initKaptcha(){
DefaultKaptcha kaptcha = new DefaultKaptcha();
Properties properties = new Properties();
//定制验证码情况
properties.put(Constants.KAPTCHA_TEXTPRODUCER_CHAR_STRING, "123");
properties.put(Constants.KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, 6);
properties.put(Constants.KAPTCHA_IMAGE_HEIGHT, "44");//不能为小数
properties.put(Constants.KAPTCHA_IMAGE_WIDTH, "326");
properties.put(Constants.KAPTCHA_TEXTPRODUCER_FONT_SIZE, "32");
Config config = new Config(properties);
kaptcha.setConfig(config);
return kaptcha;
}
}
HTML 内容 加载图片
<a href="javascript:reloadCaptcha()">
<img id="captchImg" src="/captcha">
</a>
JS 内容
function reloadCaptcha() {
var captchImg = document.getElementById("captchImg");
captchImg.src = "/captcha?t=" + (new Date()).getTime();
}
登陆 验证码验证逻辑
@RequestMapping(value = "/login")
public ModelAndView loginSuccess(@RequestParam("captcha") String captcha) {
if (!captcha.equals(session.getAttribute(Constants.KAPTCHA_SESSION_KEY))){
return new ModelAndView("redirect:login.html");
}
}