码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Day76-Spring Boot实践,开发社区登录模块-生成二维码


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

    文章目录

    • Day76-Spring Boot实践,开发社区登录模块-生成二维码
      • 一.Kaptcha
      • 1.1导入jar包
      • 1.2编写Kaptcha配置类
      • 1.3生成随机字符,生成图片

    一.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
  • 相关阅读:
    thinkphp6 入门教程合集(更新中)
    Kali工具介绍(超详细!史上最全!)
    矩阵求导简记
    python及第三方库交叉编译
    insightface实战:画出嘴巴和眼睛的mask
    餐饮行业离职证明申请范文,共计20篇
    全链路压测的步骤及重要性
    迁移学习 - 微调
    【小程序】uni-app 页面的传参和接参
    RocketMQ快速入门:如何保证消息不丢失|保证消息可靠性(九)
  • 原文地址:https://blog.csdn.net/Youmonster/article/details/127417860
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号