1、在pom文件中依赖注入Kaptcha依赖关系,如下:
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
2、创建验证码的配置文件:kaptcha.properties(名字可以随便起)
##### Kaptcha Information
kaptcha.width=150
kaptcha.height=42
kaptcha.border=no
kaptcha.textproducer.font.size=40
kaptcha.textproducer.char.space=10
kaptcha.textproducer.font.names=\u4EFF\u5B8B,\u5FAE\u8F6F\u96C5\u9ED1
kaptcha.textproducer.char.string=1234567890
kaptcha.textproducer.char.length=4
kaptcha.background.clear.from=92,189,170
kaptcha.background.clear.to=255,255,255
kaptcha属性的配置可以根据自己的需要进行修改,有关具体的配置可以参考
https://blog.csdn.net/du_zilin/article/details/77367978
3、创建验证码生成工具类NumCodeUtil
package com.mmall.common.util;
import java.util.Properties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
@Component("numCodeUtil")
public class NumCodeUtil {
private static Properties props = new Properties();
@Bean
public DefaultKaptcha defaultKaptcha() throws Exception {
// 创建DefaultKaptcha对象
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
// 读取配置文件
try {
props.load(NumCodeUtil.class.getClassLoader()
.getResourceAsStream("kaptcha.properties"));
}catch (Exception e) {
e.printStackTrace();
}
// 将Properties文件设到DefaultKaptcha对象中
defaultKaptcha.setConfig(new Config(props));
return defaultKaptcha;
}
}
4、在控制层利用流的技术生成验证码图片
package com.mmall.common.base.controller;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.google.code.kaptcha.impl.DefaultKaptcha;
/**
* 获取验证码
* @author SunTao
* @since 2018-12-13
*/
@Controller("CodeController")
@RequestMapping("/code")
public class CodeController extends BaseController {
@Autowired
private DefaultKaptcha defaultKaptcha;
@RequestMapping(value="/numCode", method=RequestMethod.GET)
public void getNumCode(HttpServletResponse response) throws Exception {
// 创建字节数组用于存放图片信息
byte[] numCodeImgByte = null;
// 获得二进制输出流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
// 通过DefaultKaptcha获得随机验证码
String successCode = defaultKaptcha.createText();
// 将生成的验证码存放在session中
session.setAttribute("successCode", successCode);
// 使用生成的验证码字符串,完成图片的生成
BufferedImage bi = defaultKaptcha.createImage(successCode);
// 将图片写入到流中
ImageIO.write(bi, "jpg", baos);
} catch (IOException e) {
System.err.println("将图片写入到输入流出现错误!");
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
// 使用HttpServletResponse将图片写入到浏览器中
numCodeImgByte = baos.toByteArray();
// 通过response设定响应请求类型
// no-store用于防止重要的信息被无意的发布。在请求消息中发送将使得请求和响应消息都不使用 缓存。
response.setHeader("Cache-Control","no-store");
// no-cache指示请求或响应消息不能缓存
response.setHeader("Pragma","no-cache");
/* expires是response的一个属性,它可以设置页面在浏览器的缓存里保存的时间 ,超过设定的时 间后就过期 。过期后再次
* 浏览该页面就需要重新请求服务器发送页面数据,如果在规定的时间内再次访问次页面 就不需从服务器传送 直接从缓存中读取。
* */
response.setDateHeader("Expires", 0);
// servlet接受request请求后接受图片形式的响应
response.setContentType("image/jpeg");
//通过response获得输出流
ServletOutputStream sos = response.getOutputStream();
sos.write(numCodeImgByte);
sos.close();
}
}
到这里,验证码的基本功能就实现了,可以发出"/code/numCode"请求获取验证码,如下图: