SpringBoot使用kaptcha生成验证码

ゝ一纸荒年。 2022-05-10 02:16 352阅读 0赞

1.在pom文件中添加依赖

  1. <dependency>
  2. <groupId>com.github.penggle</groupId>
  3. <artifactId>kaptcha</artifactId>
  4. <version>2.3.2</version>
  5. </dependency>

2.新建KaptchaConfig.java,在其中配置验证码相关的属性

  1. /**
  2. * Created By Seven.wk
  3. * Description: 验证码配置类
  4. * Created At 2018/08/07
  5. */
  6. @Component
  7. public class KaptchaConfig {
  8. @Bean
  9. public DefaultKaptcha getDefaultKaptcha() {
  10. DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
  11. Properties properties = new Properties();
  12. properties.setProperty("kaptcha.border", "yes");
  13. properties.setProperty("kaptcha.border.color", "105,179,90");
  14. properties.setProperty("kaptcha.textproducer.font.color", "blue");
  15. properties.setProperty("kaptcha.image.width", "110");
  16. properties.setProperty("kaptcha.image.height", "40");
  17. properties.setProperty("kaptcha.textproducer.font.size", "30");
  18. properties.setProperty("kaptcha.session.key", "code");
  19. properties.setProperty("kaptcha.textproducer.char.length", "4");
  20. properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
  21. Config config = new Config(properties);
  22. defaultKaptcha.setConfig(config);
  23. return defaultKaptcha;
  24. }
  25. }

3.在控制器中生成验证码并保存为图片

  1. @RestController
  2. @RequestMapping("/api")
  3. public class ILoginController {
  4. @Autowired
  5. private DefaultKaptcha defaultKaptcha;
  6. /**
  7. * 生成验证码
  8. * @param response
  9. * @param session
  10. * @throws IOException
  11. */
  12. @GetMapping("/captcha")
  13. public void captcha(HttpServletResponse response,
  14. HttpSession session) throws IOException {
  15. byte[] captchaChallengeAsJpeg = null;
  16. ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
  17. try {
  18. //生产验证码字符串并保存到session中
  19. String createText = defaultKaptcha.createText();
  20. session.setAttribute(Const.CAPTCHA_CODE, createText);
  21. //使用生产的验证码字符串返回一个BufferedImage对象并转为byte写入到byte数组中
  22. BufferedImage challenge = defaultKaptcha.createImage(createText);
  23. ImageIO.write(challenge, "jpg", jpegOutputStream);
  24. } catch (IllegalArgumentException e) {
  25. response.sendError(HttpServletResponse.SC_NOT_FOUND);
  26. return;
  27. }
  28. //定义response输出类型为image/jpeg类型,使用response输出流输出图片的byte数组
  29. captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
  30. response.setHeader("Cache-Control", "no-store");
  31. response.setHeader("Pragma", "no-cache");
  32. response.setDateHeader("Expires", 0);
  33. response.setContentType("image/jpeg");
  34. ServletOutputStream responseOutputStream = response.getOutputStream();
  35. responseOutputStream.write(captchaChallengeAsJpeg);
  36. responseOutputStream.flush();
  37. responseOutputStream.close();
  38. }
  39. }

4.现在我们可以通过访问/api/captcha,就可以看到验证码了

5.前端使用方法如下:

  1. <img src="/api/captcha" title="验证码" alt="验证码" class="captchaCode" onclick = "this.src='/api/captcha?d='+new Date()*1"/>

这样每次点击图片时就可以更换验证码了。

发表评论

表情:
评论列表 (有 0 条评论,352人围观)

还没有评论,来说两句吧...

相关阅读