• 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 你们可以根据情况修改 一般都是随机的

  • 相关阅读:
    Angular:升级Angular 13到Angular 14
    使用接口包装器模块简化在FPGA上实现PCIe的过程
    Python--配置文件优化
    [短的文章] Spring Boot 日志创建使用、日志级别、@Slf4j、日志持久化——Spring Boot 系列
    IMX6ULL学习笔记(3)——挂载NFS网络文件系统
    无线智慧城市业务方案建设
    Element-ui 动态tabs实战
    c语言 二分查找(迭代与递归)
    MySQL增删查改(进阶1)
    谈谈毕业后,北漂一个月的感受
  • 原文地址:https://blog.csdn.net/weixin_45966674/article/details/133019839