• springboot中实现生成验证码和登录校验功能


    前言

    网上实现生成验证码的方式有很多,我这里只记录下使用 kaptcha 生成验证码的方式。

    实现思路

    1、整合 kaptcha ,创建 kaptcha 的工具类。
    2、编写接口,在接口中使用 kaptcha 工具类来生成验证码图片(验证码信息)并返回。
    3、登录时从 session 中获取验证码进行校验。
    4、测试获取验证码图片(验证码信息)接口。

    代码实现

    一、整合 kaptcha ,创建 kaptcha 的工具类

    1、pom 引入 kaptcha 依赖

    		<!--生成验证码-->
            <dependency>
                <groupId>com.github.penggle</groupId>
                <artifactId>kaptcha</artifactId>
                <version>2.3.2</version>
            </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2、创建 KaptchaConfig 工具类(可直接复制粘贴使用)

    package com.boc.ljh.utils.code;
    
    import com.google.code.kaptcha.impl.DefaultKaptcha;
    import com.google.code.kaptcha.util.Config;
    import org.springframework.context.annotation.Bean;
    import org.springframework.stereotype.Component;
    
    import java.util.Properties;
    
    /**
     * @Author: ljh
     * @ClassName KaptchaConfig
     * @Description TODO
     * @date 2022/11/3 20:23
     * @Version 1.0
     */
    @Component
    public class KaptchaConfig {
    
        @Bean
        public DefaultKaptcha getDefaultKaptcha(){
            com.google.code.kaptcha.impl.DefaultKaptcha defaultKaptcha = new com.google.code.kaptcha.impl.DefaultKaptcha();
            Properties properties = new Properties();
            properties.put("kaptcha.border", "no"); //是否有边框 yes有 no没有
            properties.put("kaptcha.textproducer.font.color", "blue"); //验证码字体颜色
            properties.put("kaptcha.image.width", "150"); //验证码图片的宽
            properties.put("kaptcha.image.height", "40"); //验证码图片的高
            properties.put("kaptcha.textproducer.font.size", "30"); //验证码字体大小
            properties.put("kaptcha.session.key", "verifyCode"); //存储在session中值的key
            properties.put("kaptcha.textproducer.char.length", "5"); //验证码字符个数
            Config config = new Config(properties);
            defaultKaptcha.setConfig(config);
    
            return defaultKaptcha;
        }
    
    }
    
    • 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
    • 37
    二、编写接口,在接口中使用 kaptcha 工具类来生成验证码图片(验证码信息)并返回。

    1、编写获取验证码图片或者验证码信息功能接口(二选一或者全写都可以,可直接复制粘贴使用)

    package com.boc.ljh.controller;
    
    import com.boc.ljh.utils.Result;
    import com.google.code.kaptcha.impl.DefaultKaptcha;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import javax.imageio.ImageIO;
    import javax.servlet.ServletOutputStream;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.awt.image.BufferedImage;
    import java.io.ByteArrayOutputStream;
    
    /**
     * @Author: ljh
     * @ClassName VerificationCodeController
     * @Description TODO
     * @date 2022/11/3 20:25
     * @Version 1.0
     */
    @RestController
    @Api(tags = "验证码管理")
    @RequestMapping("/code")
    public class VerificationCodeController {
    
    
        @Autowired
        private DefaultKaptcha captchaProducer;
    
    
        @ApiOperation("获取验证码图片")
        @GetMapping("/getVerificationCodePhoto")
        public void getVerificationCodePhoto(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
            byte[] captchaOutputStream = null;
            ByteArrayOutputStream imgOutputStream = new ByteArrayOutputStream();
            try {
                //生成验证码
                String verifyCode = captchaProducer.createText();
                //验证码字符串保存到session中
                httpServletRequest.getSession().setAttribute("verifyCode", verifyCode);
                BufferedImage challenge = captchaProducer.createImage(verifyCode);
                //设置写出图片的格式
                ImageIO.write(challenge, "jpg", imgOutputStream);
            } catch (IllegalArgumentException e) {
                httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
                return;
            }
            captchaOutputStream = imgOutputStream.toByteArray();
            httpServletResponse.setHeader("Cache-Control", "no-store");
            httpServletResponse.setHeader("Pragma", "no-cache");
            httpServletResponse.setDateHeader("Expires", 0);
            httpServletResponse.setContentType("image/jpeg");
            ServletOutputStream responseOutputStream = httpServletResponse.getOutputStream();
            responseOutputStream.write(captchaOutputStream);
            responseOutputStream.flush();
            responseOutputStream.close();
        }
    
    
        @ApiOperation("获取验证码")
        @GetMapping("/getVerificationCode")
        public Result getVerificationCode(HttpServletRequest request) {
            Result result = new Result();
            String verifyCode = captchaProducer.createText();
            request.getSession().setAttribute("verifyCode", verifyCode);
            result.setData(verifyCode);
            return result;
    
        }
    
    }
    
    • 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
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    三、登录时从 session 中获取验证码进行校验

    注意:从session中获取验证码时,key值一定要和前面生成验证码时存储的key保持一致。
    在这里插入图片描述

    四、测试获取验证码图片(验证码信息)接口

    测试获取验证码图片功能接口
    在这里插入图片描述
    测试获取验证码字符串功能接口
    在这里插入图片描述

  • 相关阅读:
    SMALE实验室论文成果:多标签学习CSDN源码
    DHCP服务
    centos k8s安装dapr
    基于C++的简单ANN(人工神经网络)模型
    1143. 最长公共子序列 -- 动规
    Swagger之学习使用
    分代ZGC详解
    这是什么代码你能看懂吗
    循环结构——while循环、do...while循环
    【数据结构】搜索二叉树(C++实现)
  • 原文地址:https://blog.csdn.net/weixin_45151960/article/details/127743708