• Spring Boot2.7生成用于登录的图片验证码


    先在 pom.xml 注入依赖

    <dependency>
        <groupId>com.github.pengglegroupId>
        <artifactId>kaptchaartifactId>
        <version>2.3.2version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    然后 需要在配置文件中声明一下DefaultKaptcha 的 bean对象
    在这里插入图片描述
    然后 我们随便找个目录创建一个类 叫CaptchaGenerator.java
    专门处理生成验证码的逻辑

    import com.google.code.kaptcha.impl.DefaultKaptcha;
    import com.google.code.kaptcha.util.Config;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    import javax.imageio.ImageIO;
    import java.awt.image.BufferedImage;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.util.Properties;
    
    @Component
    public class CaptchaGenerator {
    
        private DefaultKaptcha defaultKaptcha;
    
        @Autowired
        public CaptchaGenerator(DefaultKaptcha defaultKaptcha) {
            this.defaultKaptcha = defaultKaptcha;
        }
    
        public byte[] generateCaptchaImage(String captchaText) throws IOException {
            Properties properties = new Properties();
            properties.setProperty("kaptcha.textproducer.char.string", "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
            properties.setProperty("kaptcha.textproducer.char.length", "4");
    
            Config config = new Config(properties);
            defaultKaptcha.setConfig(config);
    
            BufferedImage captchaImage = defaultKaptcha.createImage(captchaText);
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            ImageIO.write(captchaImage, "jpg", outputStream);
    
            return outputStream.toByteArray();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    然后 在写接口的类中 声明一下CaptchaGenerator 的对象
    注意 这里要用构造方法赋值
    否则东西会拿不到
    在这里插入图片描述

    private CaptchaGenerator captchaGenerator;
    
    @Autowired
    public BookController(CaptchaGenerator captchaGenerator) {
        this.captchaGenerator = captchaGenerator;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    最后 编写函数

    @GetMapping(value = "/captcha", produces = MediaType.IMAGE_JPEG_VALUE)
    public void getCaptchaImage(HttpServletResponse response) throws IOException {
        String captchaText = "3922";// 生成验证码文本
        byte[] captchaImage = captchaGenerator.generateCaptchaImage(captchaText);
    
        response.setContentType(MediaType.IMAGE_JPEG_VALUE);
        response.getOutputStream().write(captchaImage);
        response.getOutputStream().flush();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这里插入图片描述

    然后 我们直接访问 端口+接口前缀/captcha 即可访问到验证码
    在这里插入图片描述
    这里 我设置的验证码内容是 3922 你们可以根据情况修改 一般都是随机的

  • 相关阅读:
    使用 shell 脚本二进制部署 k8s 环境 [支持 docker 和 containerd]
    杭电多校 7170 Package Delivery
    day14【代码随想录】有效字母的异位词、赎金信、两个数组的交集、两个数组的交集||
    将 Spring Boot 应用程序部署为 WAR
    Manjaro linux 安装svn 并在文件管理器里显示相关图标
    Redis(七)优化建议
    SpringBoot接口 - 如何优雅的写Controller并统一异常处理?
    unittest前置依赖用例测试失败时跳过当前用例
    杭电oj--C语言合法标识符判定
    【云原生Kubernetes系列第二篇】kubeadm v1.20 部署K8S 集群架构【admin部署】
  • 原文地址:https://blog.csdn.net/weixin_45966674/article/details/133019839