1.pom.xml文件配置
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
2.新建config
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import java.util.Properties;
@Componentpublic class KaptchaConfig {
@Bean public DefaultKaptcha getDDefaultKaptcha() { DefaultKaptcha dk = new DefaultKaptcha();
Properties properties = new Properties();
// 图片边框 properties.setProperty("kaptcha.border", "yes");
// 边框颜色 properties.setProperty("kaptcha.border.color", "105,179,90");
// 字体颜色 properties.setProperty("kaptcha.textproducer.font.color", "red");
// 图片宽 properties.setProperty("kaptcha.image.width", "110");
// 图片高 properties.setProperty("kaptcha.image.height", "40");
// 字体大小 properties.setProperty("kaptcha.textproducer.font.size", "30");
// session key properties.setProperty("kaptcha.session.key", "code");
// 验证码长度 properties.setProperty("kaptcha.textproducer.char.length", "4");
// 字体 properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
Config config = new Config(properties);
dk.setConfig(config);
return dk;
}}
3.创建关于kaptcha验证码插件controller层
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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
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.ByteArrayOutputStream;
@Controller
@RequestMapping("/admin/kaptcha")
public class KaptchaController {
@Autowired
@Resource
private DefaultKaptcha captchaProducer;
/**
* 获取验证码
* @param httpServletRequest
* @param httpServletResponse
* @throws Exception
*/
@GetMapping("/getKaptcha")
public void getKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
// 接受验证码图片输出流
ByteArrayOutputStream imgOutputStream = new ByteArrayOutputStream();
try {
// 生产验证码字符串并保存到session中
String code = captchaProducer.createText();
System.out.println(code);
// 该key值必须和 kaptcha.session.key 配置的相同
httpServletRequest.getSession().setAttribute("code", code);
BufferedImage challenge = captchaProducer.createImage(code);
ImageIO.write(challenge, "jpg", imgOutputStream);
} catch (Exception e) {
// 如果出错,返回404
httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
// 设置返回值
httpServletResponse.setHeader("Cache-Control", "no-store");
httpServletResponse.setDateHeader("Expires", 0);
httpServletResponse.setContentType("image/jpeg");
// 将图片刷进response
ServletOutputStream responseOutputStream = httpServletResponse.getOutputStream();
responseOutputStream.write(imgOutputStream.toByteArray());
responseOutputStream.flush();
responseOutputStream.close();
}
}
4.前端样式
用的两个style,一个设置左浮动,一个右浮动
<div class="hr15" style="margin: auto;">
<input name="yanCode" style="width:60%;float:left;" lay-verify="required" placeholder="验证码" type="text" class="layui-input">
<img style="width:35%;float:right;" title="看不清,换一张" class="pointer" th:src="@{/admin/kaptcha/getKaptcha}"
onclick="this.src='/admin/kaptcha/getKaptcha?d='+Math.floor(Math.random()*100)">
</div>
5.关于获取验证码传值
String scode = (String)request.getSession().getAttribute(“code”);