zoukankan      html  css  js  c++  java
  • springboot+captcha生成验证码

    原文地址:https://www.toutiao.com/i6876730754873688587/

    前言:验证码登录是很多项目中会涉及到的,为了数据安全和缓存验证码数据考虑,一般会要求后端来生成验证码,并存储在redis中,设置有效期,点击可刷新。本文,讲述springboot+captcha生成验证码的全过程,并写一个简单的demo

    创建springboot项目,点击Next 

    填写必要信息,点击Next

     

    选择必要依赖,点击Next

     

    确认项目信息,点击Finish

     

    生成初始项目如下

     

    添加工具依赖,这里我们使用hutool,版本选最新的即可,文章末尾会介绍

     

    新建控制器CaptchaCtl

     

    增加一个生成验证码的接口

     

    调用CaptchaUtil里边的具体方法

     

    三种方法主要区别

    • createLineCaptcha:生成线段干扰验证码

        

       

    • createCircleCaptcha:生成圆圈干扰验证码

        

    • createShearCaptcha:生成扭曲干扰验证码

        

    选择一种喜欢的,完善生成方法,其中code就是图片对应的真实文字,用来和用户输入值进行比较

     运行项目,请求接口,前端获取到了图片流,后端打印出了验证码的真实值:tdxua

     拷贝前端的图片流,去专业网站验证图片生成情况

     

     拷贝前端的图片流,去专业网站验证图片生成情况

     图、文一致,至此springboot+captcha生成验证码的功能就完成了。

     贴上完整代码

    • pom.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.3.4.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>top.baohaipeng</groupId>
        <artifactId>captcha-boot</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>captcha-boot</name>
        <description>springboot生成验证码</description>
        <properties>
            <java.version>1.8</java.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>cn.hutool</groupId>
                <artifactId>hutool-all</artifactId>
                <version>5.4.3</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>
    • CaptchaCtl.java
    package top.baohaipeng.captchaboot.controller;
    import cn.hutool.captcha.CaptchaUtil;
    import cn.hutool.captcha.ShearCaptcha;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    /**
     * @ClassName CaptchaCtl
     * @Description TODO
     * @Author 头条号:北海派bhp
     * @Date 2020/9/25 21:42
     * @Version 1.0
     **/
    @RestController
    @RequestMapping
    public class CaptchaCtl {
        @GetMapping("captcha")
        public String getCaptcha(){
            ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(111, 36);
            // 验证码对应的字符串
            String code = captcha.getCode();
            System.out.println("真实验证码是:"+code);
            // 返回图片流
            return captcha.getImageBase64();
        }
    }

    相关资料

     

    Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。

    Hutool中的工具方法来自于每个用户的精雕细琢,它涵盖了Java开发底层代码中的方方面面,它既是大型项目开发中解决小问题的利器,也是小型项目中的效率担当。

    Hutool是项目中“util”包友好的替代,它节省了开发人员对项目中公用类和公用工具方法的封装时间,使开发专注于业务,同时可以最大限度的避免封装不完善带来的bug。

  • 相关阅读:
    Swift-自定义类的构造函数
    Swift-存储属性,计算属性,类属性
    iOS-UICollectionViewController协议及回调
    Swift-数组
    Swift-switch使用注意点
    Swift-setValuesForKeysWithDictionary
    解决IDEA Struts2 web.xml问题
    枚举类
    增强for循环 -- foreach循环
    静态导入
  • 原文地址:https://www.cnblogs.com/java-bhp/p/13737126.html
Copyright © 2011-2022 走看看