• Day76-Spring Boot实践,开发社区登录模块-生成二维码


    Day76-Spring Boot实践,开发社区登录模块-生成二维码

    一.Kaptcha

    1.1导入jar包

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

    1.2编写Kaptcha配置类

    package com.nowcoder.community.config;
    
    import com.google.code.kaptcha.Producer;
    import com.google.code.kaptcha.impl.DefaultKaptcha;
    import com.google.code.kaptcha.util.Config;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import java.util.Properties;
    
    @Configuration
    public class KaptchaConfig{
    
        @Bean
        public Producer KaptchaProducer(){
            Properties properties = new Properties();
            properties.setProperty("Kaptcha.image.width","100");
            properties.setProperty("Kaptcha.image.height","40");
            properties.setProperty("Kaptcha.textproducer.font.size","32");
            properties.setProperty("Kaptcha.textproducer.font.color","0,0,0");
            properties.setProperty("Kaptcha.textproducer.char.string","0123456789ABCDEFGHIJKLMNOPQRSTUVWZYZ");
            properties.setProperty("Kaptcha.textproducer.char.length","4");
            properties.setProperty("Kaptcha.noise.impl","com.google.code.kaptcha.impl.NoNoise");
    
            DefaultKaptcha kaptcha = new DefaultKaptcha();
            Config config = new Config(properties);
            kaptcha.setConfig(config);
            return kaptcha;
        }
    
    
    
    }
    
    
    • 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

    1.3生成随机字符,生成图片

    @RequestMapping(path = "/kaptcha", method = RequestMethod.GET)
        public void getKaptcha(HttpServletResponse response, HttpSession session){
            //生成验证码
            String text = kpatchaProducer.createText();
            BufferedImage image = kpatchaProducer.createImage(text);
            //将验证码存入session
            session.setAttribute("kaptcha",text);
    
            //将图片输出到浏览器
            response.setContentType("image/png");
            try {
                OutputStream os = response.getOutputStream();
                ImageIO.write(image,"png",os);
            } catch (IOException e) {
                logger.error("响应验证码失败:"+e.getMessage());
            }
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
  • 相关阅读:
    PTE考试预览
    【lesson12】理解进程地址空间
    【Git】使用git上传项目到github
    BFC详解(Block Formmating Context)
    工单提交管理H5小程序开发
    【pytorch08】拼接与拆分
    [附源码]java毕业设计基于智能推荐的房屋租赁系统
    全网最细的SpringBoot3系列教程
    C++用条件变量实现线程安全的queue容器
    数据结构之二叉树
  • 原文地址:https://blog.csdn.net/Youmonster/article/details/127417860