spring boot3 验证码工具kaptcha使用 [复制链接]
发表于 2025-10-12 16:11:07 | 显示全部楼层 |阅读模式
1、pom.xml文件,下载验证码工具kaptcha依靠
官网搭建的spring boot项目 
  1. <!--验证码工具kaptcha的依赖-->
  2. <dependency>
  3.     <groupId>com.github.penggle</groupId>
  4.     <artifactId>kaptcha</artifactId>
  5. </dependency>
复制代码
阿里云搭建的spring boot项目  https://start.aliyun.com
  1. <!--验证码工具kaptcha的依赖-->
  2. <dependency>
  3.     <groupId>com.github.penggle</groupId>
  4.     <artifactId>kaptcha</artifactId>
  5.     <version>2.3.2</version>
  6. </dependency>
复制代码

2、创建CaptchaConfig 设置工具类

  1. package com.pn.config;
  2. import com.google.code.kaptcha.impl.DefaultKaptcha;
  3. import com.google.code.kaptcha.util.Config;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import java.util.Properties;
  7. /**
  8. * 验证码工具kaptcha的配置
  9. */
  10. @Configuration
  11. public class CaptchaConfig {
  12.     /**
  13.      * 配置Producer接口的实现类DefaultKaptcha的bean对象,该对象用于生成验证码图片;
  14.      * 并给其指定生成的验证码图片的设置项;bean对象的id引用名为captchaProducer;
  15.      */
  16.     @Bean(name = "captchaProducer")
  17.     public DefaultKaptcha captchaProducer() {
  18.         DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
  19.         //properties 属性集对象  -- 对properties文件进行封装,直接再这配置属性,也就是map
  20.         Properties properties = new Properties();
  21.         //是否有边框 默认为true 我们可以自己设置yes,no
  22.         properties.setProperty("kaptcha.border", "yes");
  23.         //边框颜色 默认为Color.BLACK
  24.         properties.setProperty("kaptcha.border.color", "105,179,90");
  25.         //验证码文本字符颜色 默认为Color.BLACK
  26.         properties.setProperty("kaptcha.textproducer.font.color", "blue");
  27.         //验证码图片宽度 默认为200
  28.         properties.setProperty("kaptcha.image.width", "120");
  29.         //验证码图片高度 默认为50
  30.         properties.setProperty("kaptcha.image.height", "40");
  31.         //验证码文本字符大小 默认为40
  32.         properties.setProperty("kaptcha.textproducer.font.size", "32");
  33.         //KAPTCHA_SESSION_KEY
  34.         properties.setProperty("kaptcha.session.key", "kaptchaCode");
  35.         //验证码文本字符间距 默认为2
  36.         properties.setProperty("kaptcha.textproducer.char.space", "4");
  37.         //验证码文本字符长度 默认为5
  38.         properties.setProperty("kaptcha.textproducer.char.length", "4");
  39.         //验证码文本字体样式 默认为new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)
  40.         properties.setProperty("kaptcha.textproducer.font.names", "Arial,Courier");
  41.         //验证码噪点颜色 默认为Color.BLACK
  42.         properties.setProperty("kaptcha.noise.color", "gray");
  43.         Config config = new Config(properties);
  44.         defaultKaptcha.setConfig(config);
  45.         return defaultKaptcha;
  46.     }
  47. }
复制代码

3、创建LoginController 控制器使用

  1. package com.pn.controller;
  2. import com.google.code.kaptcha.Producer;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Qualifier;
  5. import org.springframework.data.redis.core.ReactiveRedisTemplate;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8. import javax.annotation.Resource;
  9. import javax.imageio.ImageIO;
  10. import javax.servlet.ServletOutputStream;
  11. import javax.servlet.http.HttpServletResponse;
  12. import java.awt.image.BufferedImage;
  13. import java.io.IOException;
  14. import java.time.Duration;
  15. @RestController
  16. public class LoginController {
  17.     //通过接口拿到kaptcha验证码工具类
  18.     @Resource(name = "captchaProducer")
  19.     private Producer producer;
  20.     //0库
  21.     //@Autowired
  22.     //@Qualifier("reactiveRedisTemplateDb0")
  23.     //private ReactiveRedisTemplate<String, Object> redis;
  24.     @RequestMapping("/captcha/captchaImage")
  25.     public void captchaImage(HttpServletResponse response) throws Exception {
  26.         //ServletOutputStream 实例字节流
  27.         ServletOutputStream out = null;
  28.         try {
  29.             //producer.createText() 生成图片文件
  30.             String text = producer.createText();
  31.             //BufferedImage存放再内存中,producer.createImage() 生成图片
  32.             BufferedImage img = producer.createImage(text);
  33.             //将验证码文本保存到Reids中,设置过期时间,为什么要放key? 多个账号都需要验证码,这样查询对比就方便省事,反正放key就对了
  34.             //博主使用的时Reids异步非阻塞方式,参考博主的Reids异步非阻塞
  35.             //或者使用自己的方式
  36.             //redis.opsForValue().set(text, "",Duration.ofSeconds(20)).subscribe();
  37.             //设置响应头Content - Type 字段设置成 image/jpeg
  38.             response.setContentType("image/jpeg");
  39.             //response.getOutputStream() 开启实例字节流
  40.             out = response.getOutputStream();
  41.             //ImageIO.write()
  42.             ImageIO.write(img, "jpg", out);
  43.             //刷新
  44.             out.flush();
  45.         } catch (IOException e) {
  46.             throw new RuntimeException(e);
  47.         }finally {
  48.             //关闭字节流
  49.             if (out != null) {
  50.                 try {
  51.                     out.close();
  52.                 } catch (IOException e) {
  53.                     e.printStackTrace();
  54.                 }
  55.             }
  56.         }
  57.     }
  58. }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表