导入依赖
<!-- kaptcha 验证码开源组件 -->
<dependency><groupId>com.github.penggle</groupId><artifactId>kaptcha</artifactId><version>2.3.2</version>
</dependency>
kaptcha.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsd"><!-- Kaptcha组件配置 --><bean id="kaptchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha"><property name="config"><bean class="com.google.code.kaptcha.util.Config"><constructor-arg><props><!-- 验证码宽度 --><prop key="kaptcha.image.width">120</prop><!-- 验证码高度 --><prop key="kaptcha.image.height">50</prop><!-- 生成验证码内容范围 --><prop key="kaptcha.textproducer.char.string">0123456789AKWUEHPMRX</prop><!-- 验证码个数 --><prop key="kaptcha.textproducer.char.length">4</prop><!-- 是否有边框 --><prop key="kaptcha.border">no</prop><!-- 边框颜色 --><prop key="kaptcha.border.color">105,179,90</prop><!-- 边框厚度 --><prop key="kaptcha.border.thickness">1</prop><!-- 验证码字体颜色 --><prop key="kaptcha.textproducer.font.color">yellow</prop><!-- 验证码字体大小 --><prop key="kaptcha.textproducer.font.size">30</prop><!-- 验证码所属字体样式 --><prop key="kaptcha.textproducer.font.names">楷体</prop><!-- 干扰线颜色 --><prop key="kaptcha.noise.color">black</prop><!-- 验证码文本字符间距 --><prop key="kaptcha.textproducer.char.space">8</prop><!-- 图片样式 :阴影--><prop key="kaptcha.obscurificator.impl">com.google.code.kaptcha.impl.ShadowGimpy</prop></props></constructor-arg></bean></property></bean>
</beans>
KaptchaController
package com.xycf.springboot01.controller;import com.google.code.kaptcha.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;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;/*** 验证码* @author: ztc* @date: 2022/5/26*/
@Controller
public class KaptchaController {private Producer kaptchaProducer=null;@Autowiredpublic void setKaptchaProducer(Producer kaptchaProducer) {this.kaptchaProducer = kaptchaProducer;}@GetMapping(value = "/getVerifyCode")public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response){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 = kaptchaProducer.createText();//将验证码保存到session中request.getSession().setAttribute("verifyCode", capText);BufferedImage bi = kaptchaProducer.createImage(capText);ServletOutputStream out = null;try {out = response.getOutputStream();ImageIO.write(bi, "jpg", out);} catch (IOException e) {e.printStackTrace();}try {out.flush();} catch (IOException e) {e.printStackTrace();} finally {try {out.close();} catch (IOException e) {e.printStackTrace();}}return null;}
}
html页面
<body><img id="kaptchaImage" style="width: 120px;height: 50px" src="/getVerifyCode" onclick="change()">
</body>
<script>function change() {$('#kaptchaImage').click(function () {$(this).attr('src', '/getVerifyCode?' + Math.floor(Math.random() * 100));})}
</script>
效果图