一、背景
1.因为最近项目中要更换验证码,用的是kaptch与springboot,操作起来也是比较方便的,也趟了不少弯路,特意记下这个方法,以供大家学习参考,下面我们就来讲解下。
二、项目结构
三、详细的配置
1.pom.xml
<!-- kaptcha -->
<dependency>
<groupId>com.google.code</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
2.config
2.1.KaptchaConfig.java
@Component
public class KaptchaConfig {
@Bean
public DefaultKaptcha getDefaultKaptcha(){
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
Properties properties = new Properties();
properties.setProperty("kaptcha.border", "no");
properties.setProperty("kaptcha.border.color", "blue");
properties.setProperty("kaptcha.textproducer.font.color", "black");
properties.setProperty("kaptcha.image.width", "160");
properties.setProperty("kaptcha.image.height", "50");
properties.setProperty("kaptcha.textproducer.font.size", "40");
properties.setProperty("kaptcha.session.key", "code");
properties.setProperty("kaptcha.noise.color", "white");
properties.setProperty("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.ShadowGimpy");
properties.setProperty("kaptcha.textproducer.char.length", "4");
properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
Config config = new Config(properties);
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}
3.controller
3.1.生成验证码的接口(CreateCode.java)
@Controller
@RequestMapping(value = "/createCode")
public class CreateCode {
@Autowired
DefaultKaptcha defaultKaptcha;
@RequestMapping(value="/testdemo", method= RequestMethod.GET)
public void createCode(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception{
byte[] captchaChallengeAsJpeg = null;
ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
try {
//生产验证码字符串并保存到session中去
String createText = defaultKaptcha.createText();
//给定一个唯一标识,防止验证码重复使用
httpServletRequest.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, createText);
//redisCache.set(RedisCache.prefix, coldFlag, createText);
//使用生产的验证码字符串返回一个BufferedImage对象并转为byte写入到byte数组中
BufferedImage challenge = defaultKaptcha.createImage(createText);
ImageIO.write(challenge, "jpg", jpegOutputStream);
} catch (IllegalArgumentException e) {
httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
//定义response输出类型为image/jpeg类型,使用response输出流输出图片的byte数组
captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
httpServletResponse.setHeader("Cache-Control", "no-store");
httpServletResponse.setHeader("Pragma", "no-cache");
httpServletResponse.setDateHeader("Expires", 0);
httpServletResponse.setContentType("image/jpeg");
ServletOutputStream responseOutputStream = httpServletResponse.getOutputStream();
responseOutputStream.write(captchaChallengeAsJpeg);
responseOutputStream.flush();
responseOutputStream.close();
}
}
3.2.postman测试生成验证码
3.3.模拟前端校验验证码(PreCheckController)
@Controller
@RequestMapping(value = "/preCheckCode")
@Slf4j
public class PreCheckController {
//验证码验证
@PostMapping("/checkCode")
@ResponseBody
public String imgvrifyControllerDefaultKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,@RequestBody JSONObject data){
String captchaId = (String) httpServletRequest.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
String code = data.getString("code");
log.info("Session vrifyCode ---->"+captchaId+"---- form code --->"+code);
if (!captchaId.equals(code)) {
log.info("错误的验证码");
return "fail";
} else {
return "success";
}
}
}
3.4.模拟前端postman测试
四、看后端日志
1.日志,前端输入的和我们保存在session中的是一致的
五、总结
1.这样就完成了验证码的生成以及校验,希望能帮助到你们,共勉!!!