springboot实现简单的验证码功能

我就是我 2023-01-11 01:09 309阅读 0赞

导入依赖

使用谷歌的验证码依赖

  1. <!-- google kaptcha依赖 验证码 -->
  2. <dependency>
  3. <groupId>com.github.axet</groupId>
  4. <artifactId>kaptcha</artifactId>
  5. <version>0.0.9</version>
  6. </dependency>

验证码配置

验证码的工具类

  1. @Configuration
  2. public class CaptchaConfig {
  3. @Bean
  4. public DefaultKaptcha getDefaultKaptcha(){
  5. //验证码生成器
  6. DefaultKaptcha defaultKaptcha=new DefaultKaptcha();
  7. //配置
  8. Properties properties = new Properties();
  9. //是否有边框
  10. properties.setProperty("kaptcha.border", "yes");
  11. //设置边框颜色
  12. properties.setProperty("kaptcha.border.color", "105,179,90");
  13. //边框粗细度,默认为1
  14. // properties.setProperty("kaptcha.border.thickness","1");
  15. //验证码
  16. properties.setProperty("kaptcha.session.key","code");
  17. //验证码文本字符颜色 默认为黑色
  18. properties.setProperty("kaptcha.textproducer.font.color", "blue");
  19. //设置字体样式
  20. properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
  21. //字体大小,默认40
  22. properties.setProperty("kaptcha.textproducer.font.size", "30");
  23. //验证码文本字符内容范围 默认为abced2345678gfynmnpwx
  24. // properties.setProperty("kaptcha.textproducer.char.string", "");
  25. //字符长度,默认为5
  26. properties.setProperty("kaptcha.textproducer.char.length", "4");
  27. //字符间距 默认为2
  28. properties.setProperty("kaptcha.textproducer.char.space", "4");
  29. //验证码图片宽度 默认为200
  30. properties.setProperty("kaptcha.image.width", "100");
  31. //验证码图片高度 默认为40
  32. properties.setProperty("kaptcha.image.height", "40");
  33. Config config = new Config(properties);
  34. defaultKaptcha.setConfig(config);
  35. return defaultKaptcha;
  36. }
  37. }

配置对应验证码请求

加了 produces = “image/jpeg” 可以使得在postman等工具中也能显示对应的验证码图片

  1. @Autowired
  2. private DefaultKaptcha defaultKaptcha;//需要注入验证码生成的工具
  3. @GetMapping(value = "/captcha", produces = "image/jpeg")
  4. public void captcha(HttpServletRequest request, HttpServletResponse response) {
  5. // 定义response输出类型为image/jpeg类型
  6. response.setDateHeader("Expires", 0);
  7. // Set standard HTTP/1.1 no-cache headers.
  8. response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
  9. // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
  10. response.addHeader("Cache-Control", "post-check=0, pre-check=0");
  11. // Set standard HTTP/1.0 no-cache header.
  12. response.setHeader("Pragma", "no-cache");
  13. // return a jpeg
  14. response.setContentType("image/jpeg");
  15. //-------------------生成验证码 begin --------------------------
  16. String text = defaultKaptcha.createText();
  17. System.out.println("验证码为:" + text);
  18. //将验证码放入session中
  19. request.getSession().setAttribute("captcha", text);
  20. //根据验证码创建图片
  21. BufferedImage image = defaultKaptcha.createImage(text);
  22. ServletOutputStream outputStream = null;
  23. try {
  24. outputStream= response.getOutputStream();
  25. //根据image生成jpg的图像 由outputStream输出
  26. ImageIO.write(image, "jpg", outputStream);
  27. outputStream.flush();
  28. } catch (IOException e) {
  29. if (outputStream != null) {
  30. try {
  31. outputStream.close();
  32. } catch (IOException ioException) {
  33. ioException.printStackTrace();
  34. }
  35. }
  36. }
  37. //-------------------生成验证码 end ----------------------------
  38. }

效果

在这里插入图片描述

发表评论

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

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

相关阅读

    相关 说说验证功能实现

    ![format_png][] 前言 大家好,我是 god23bin,今天说说验证码功能的实现,相信大家都经常接触到验证码的,毕竟平时上网也能遇到各种验证码,需要我们输