SpringBoot kaptcha 验证码
验证码生成使用的是 Google 的 kaptcha 框架,固定的代码,只需要自己配置一些参数即可,下面的代码是主要的代码,像项目的前端代码、验证生成的验证码之类的代码没有给出。

pom

需要引入 kaptcha 的 Maven 依赖

  1. <!-- https://mvnrepository.com/artifact/com.github.penggle/kaptcha -->
  2. <dependency>
  3. <groupId>com.github.penggle</groupId>
  4. <artifactId>kaptcha</artifactId>
  5. <version>2.3.2</version>
  6. </dependency>

KaptchaConfig

即 kaptcha 的配置文件

  1. import java.util.Properties;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.stereotype.Component;
  4. import com.google.code.kaptcha.impl.DefaultKaptcha;
  5. import com.google.code.kaptcha.util.Config;
  6. @Component
  7. public class KaptchaConfig {
  8. @Bean
  9. public DefaultKaptcha getDefaultKaptcha() {
  10. com.google.code.kaptcha.impl.DefaultKaptcha defaultKaptcha = new com.google.code.kaptcha.impl.DefaultKaptcha();
  11. Properties properties = new Properties();
  12. // 图片边框
  13. properties.setProperty("kaptcha.border", "no");
  14. // 边框颜色
  15. properties.setProperty("kaptcha.border.color", "105,179,90");
  16. // 字体颜色
  17. properties.setProperty("kaptcha.textproducer.font.color", "black");
  18. // 图片宽
  19. properties.setProperty("kaptcha.image.width", "120");
  20. // 图片高
  21. properties.setProperty("kaptcha.image.height", "50");
  22. // 字体大小
  23. properties.setProperty("kaptcha.textproducer.font.size", "30");
  24. // session key
  25. properties.setProperty("kaptcha.session.key", "code");
  26. // 验证码长度
  27. properties.setProperty("kaptcha.textproducer.char.length", "5");
  28. // 字体
  29. // properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
  30. Config config = new Config(properties);
  31. defaultKaptcha.setConfig(config);
  32. return defaultKaptcha;
  33. }
  34. }

KaptchaController

验证码控制层

  1. package com.cun.controller;
  2. import java.awt.image.BufferedImage;
  3. import java.io.ByteArrayOutputStream;
  4. import javax.imageio.ImageIO;
  5. import javax.servlet.ServletOutputStream;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.stereotype.Controller;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import org.springframework.web.servlet.ModelAndView;
  12. import com.google.code.kaptcha.impl.DefaultKaptcha;
  13. @Controller
  14. public class KaptchaController {
  15. //验证码工具
  16. @Autowired
  17. DefaultKaptcha defaultKaptcha;
  18. /**
  19. * 2、生成验证码
  20. * @param httpServletRequest
  21. * @param httpServletResponse
  22. * @throws Exception
  23. */
  24. @RequestMapping("/defaultKaptcha")
  25. public void defaultKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
  26. throws Exception {
  27. byte[] captchaChallengeAsJpeg = null;
  28. ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
  29. try {
  30. // 生产验证码字符串并保存到session中
  31. String createText = defaultKaptcha.createText();
  32. httpServletRequest.getSession().setAttribute("rightCode", createText);
  33. // 使用生产的验证码字符串返回一个BufferedImage对象并转为byte写入到byte数组中
  34. BufferedImage challenge = defaultKaptcha.createImage(createText);
  35. ImageIO.write(challenge, "jpg", jpegOutputStream);
  36. } catch (IllegalArgumentException e) {
  37. httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
  38. return;
  39. }
  40. // 定义response输出类型为image/jpeg类型,使用response输出流输出图片的byte数组
  41. captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
  42. httpServletResponse.setHeader("Cache-Control", "no-store");
  43. httpServletResponse.setHeader("Pragma", "no-cache");
  44. httpServletResponse.setDateHeader("Expires", 0);
  45. httpServletResponse.setContentType("image/jpeg");
  46. ServletOutputStream responseOutputStream = httpServletResponse.getOutputStream();
  47. responseOutputStream.write(captchaChallengeAsJpeg);
  48. responseOutputStream.flush();
  49. responseOutputStream.close();
  50. }
  51. }

效果

访问localhost:8080//defaultKaptcha(不同的端口不同访问链接)