依赖
<!-- https://mvnrepository.com/artifact/com.github.axet/kaptcha -->
<dependency>
<groupId>com.github.axet</groupId>
<artifactId>kaptcha</artifactId>
<version>0.0.9</version>
</dependency>
servlet-context.xml图形验证码bean配置
<!--
图片验证码bean 对象配置
-->
<bean id="captchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha">
<property name="config">
<bean class="com.google.code.kaptcha.util.Config">
<constructor-arg>
<props>
<prop key="kaptcha.border">yes</prop><!--是否有边框 -->
<prop key="kaptcha.border.color">105,179,90</prop><!--设置边框颜色 -->
<prop key="kaptcha.textproducer.font.color">green</prop><!--验证码文本字符颜色 默认为Color.BLACK -->
<prop key="kaptcha.session.key">code</prop><!--验证码 -->
<prop key="kaptcha.textproducer.font.names">宋体,楷体,微软雅黑</prop><!--设置字体样式 -->
<prop key="kaptcha.border.thickness"></prop><!--边框粗细度 默认为1 -->
<prop key="kaptcha.producer.impl"></prop><!--验证码生成器 默认为DefaultKaptcha -->
<prop key="kaptcha.textproducer.impl"></prop><!-- 验证码文本生成器 默认为DefaultTextCreator -->
<prop key="kaptcha.textproducer.char.string"></prop><!--验证码文本字符内容范围 默认为abcde2345678gfynmnpwx -->
<prop key="kaptcha.textproducer.char.length">4</prop><!-- 验证码文本字符长度 默认为5 -->
<prop key="kaptcha.textproducer.font.size">40</prop><!--验证码文本字符大小 默认为40 -->
<prop key="kaptcha.textproducer.char.space">6</prop> <!--验证码文本字符间距 默认为2 -->
<prop key="kaptcha.image.width">200</prop> <!--验证码图片宽度 默认为200 -->
<prop key="kaptcha.image.height">50</prop> <!--验证码图片高度 默认为40 -->
</props>
</constructor-arg>
</bean>
</property>
</bean>
controller接口
package com.credi.xmjf.web.controller;
import com.google.code.kaptcha.Producer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
/**
* Created by Administrator on 2019/6/23.
*/
@Controller
public class ImageController {
//图形验证码接口注入
@Resource
private Producer producer;
@RequestMapping("image")
public void getImage(HttpServletResponse response, HttpServletRequest request){
// 响应头信息设置
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");
// return a jpeg
response.setContentType("image/jpeg");
// 验证码文本数字
String code=producer.createText();
// 将验证码存入session
request.getSession().setAttribute("image",code);
// 验证码图片对象
BufferedImage bi=producer.createImage(code);
// 输出流
OutputStream os=null;
try {
os=response.getOutputStream();
ImageIO.write(bi,"jpg",os); // bi:图片对象 , "jpg":图片格式 , os:输出流
os.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(null !=os){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
前端
<!--访问接口 , 得到验证码图片-->
[外链图片转存失败(img-VX64MbtS-1562055061097)(https://mp.csdn.net/mdeditor/$%7Bctx%7D/image)]
// 点击验证码,生成新的验证码
$(function () {
$(".validImg").click(function () {
$(this).attr("src",ctx+"/image?time="+new Date());
})
});