@Controller
public class KaptchaController {
@Resource
private Producer kaptchaProducer;
@GetMapping("/verify_code")
public void createVerifyCode(HttpServletRequest request , HttpServletResponse response) throws IOException {
//响应立即过期
response.setDateHeader("Expires",0);
//不缓存任何图片数据
response.setHeader("Cache-Control" , "no-store,no-cache,must-revalidate");
response.setHeader("Cache-Control" , "post-check=0,pre-check=0");
response.setHeader("Pragma" , "no-cache");
response.setContentType("image/png");
//生成验证码字符文本
String verifyCode = kaptchaProducer.createText();
request.getSession().setAttribute("kaptchaVerifyCode",verifyCode);
System.out.println(request.getSession().getAttribute("kaptchaVerifyCode"));
BufferedImage image = kaptchaProducer.createImage(verifyCode);//创建验证码图片
ServletOutputStream out = response.getOutputStream();
ImageIO.write(image, "png", out);//输出图片流
out.flush();
out.close();
}