直接上代码
首先配置
/**
* @Component 不需要 *
*/
public class KaptchaConfig {
// @Bean 不需要 *
public DefaultKaptcha getDefaultKaptcha() {
//随机颜色
Random random = new Random();
int r = random.nextInt(256);
int g = random.nextInt(256);
int b = random.nextInt(256);
String color =new StringBuffer().append(r).append(",").append(g).append(",").append(b).toString();
com.google.code.kaptcha.impl.DefaultKaptcha defaultKaptcha = new com.google.code.kaptcha.impl.DefaultKaptcha();
Properties properties = new Properties();
// 图片边框
properties.setProperty("kaptcha.border", "no");
// 边框颜色
properties.setProperty("kaptcha.border.color", "105,179,90");
// 字体颜色
properties.setProperty("kaptcha.textproducer.font.color", color);
// 图片宽
properties.setProperty("kaptcha.image.width", "240");
// 图片高
properties.setProperty("kaptcha.image.height", "75");
// 字体大小
properties.setProperty("kaptcha.textproducer.font.size", "45");
//设置图片样式
properties.setProperty("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.WaterRipple");
// session key
properties.setProperty("kaptcha.session.key", "code");
// 验证码长度
properties.setProperty("kaptcha.textproducer.char.length", "4");
// 字体
properties.setProperty("kaptcha.textproducer.font.names", "隶书,黑体,宋体,楷体,微软雅黑");
//干扰线颜色
properties.setProperty("kaptcha.noise.color",color);
properties.setProperty("kaptcha.textproducer.char.string","ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
properties.setProperty("kaptcha.textproducer.char.space","3");
Config config = new Config(properties);
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}
字体颜色 和 干扰线颜色 设置为 color
然后在Controller 里面实例化
@RequestMapping("/chkcode")
public void defaultKaptcha(HttpSession session, HttpServletResponse response)
throws Exception {
KaptchaConfig kaptchaConfig = new KaptchaConfig();
DefaultKaptcha defaultKaptcha = kaptchaConfig.getDefaultKaptcha();
byte[] captchaChallengeAsJpeg = null;
ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
try {
// 生产验证码字符串并保存到session中
String createText = defaultKaptcha.createText();
session.setAttribute("rightCode", createText);
// 使用生产的验证码字符串返回一个BufferedImage对象并转为byte写入到byte数组中
BufferedImage challenge = defaultKaptcha.createImage(createText);
ImageIO.write(challenge, "jpg", jpegOutputStream);
} catch (IllegalArgumentException e) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
// 定义response输出类型为image/jpeg类型,使用response输出流输出图片的byte数组
captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
response.setHeader("Cache-Control", "no-store");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
ServletOutputStream responseOutputStream = response.getOutputStream();
responseOutputStream.write(captchaChallengeAsJpeg);
responseOutputStream.flush();
responseOutputStream.close();
}
登录页面加入
<img style="cursor:pointer;height: 60px;" id="chkcode_img" src="/chkcode" onclick="javascript:this.src='/chkcode'+'?time='+Math.random()">
验证码放到 session 中 ,登录的时候 取 session中 rightCode 和用户输入的验证码比较即可