验证码

验证码

一、导入依赖

  • 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.yang</groupId>
    <artifactId>verification-code</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!-- 强制排出文件  -->
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

    <!--  控制 版本    -->
    <properties>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!--  编译jdk版本  -->
        <jdk.version>1.8</jdk.version>

        <!--  依赖版本  -->
        <!--  spring 版本      -->
        <spring.version>4.1.2.RELEASE</spring.version>
        <!--  sql 版本      -->
        <mysql.version>8.0.20</mysql.version>
        <poi.version>3.17</poi.version>
        <xerces.version>2.11.0</xerces.version>
    </properties>

    <!-- 导入依赖   -->
    <dependencies>
        <!-- spring start -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
        </dependency>
        <!-- spring end -->

        <!-- 其他开始-->
        <dependency>
            <!--  lombok 依赖 偷懒神器 @Data 自动生成 get set toString 无参构造方法 equals hashCode -->
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <!--  c3po 依赖              -->
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>


        <!--  hutool 工具类      -->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>4.5.15</version>
        </dependency>


        <dependency>
            <!-- fastJson  -->
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.68</version>
        </dependency>


        <dependency>
            <!--  MySQL  驱动         -->
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>

        <dependency>
            <!-- dom 解析           -->
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>

        <!-- web 开始  -->
        <!--        <dependency>-->
        <!--            <groupId>javax</groupId>-->
        <!--            <artifactId>javaee-api</artifactId>-->
        <!--            <version>6.0</version>-->
        <!--            <scope>provided</scope>-->
        <!--        </dependency>-->

        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>javax.websocket</groupId>
            <artifactId>javax.websocket-api</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>javax.transaction-api</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- web 结束  -->


        <dependency>
            <!--  cglib  依赖        -->
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.2.2</version>
        </dependency>


        <!--   mybatis 依赖      -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.2</version>
        </dependency>
        <!-- 分页 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.10</version>
        </dependency>


        <!-- 测试     -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>


        <dependency>
            <!--  commons-io         -->
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.2</version>
        </dependency>


        <dependency>
            <!--  文件上传  -->
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>


        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>${poi.version}</version>
        </dependency>
        <dependency>
            <groupId>xerces</groupId>
            <artifactId>xercesImpl</artifactId>
            <version>${xerces.version}</version>
        </dependency>

        <dependency>
            <groupId>xml-apis</groupId>
            <artifactId>xml-apis</artifactId>
            <version>1.4.01</version>
        </dependency>


        <dependency>
            <!-- oracle 依赖           -->
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.3.0</version>
        </dependency>

        <!-- 其他结束-->
    </dependencies>


    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-framework-bom</artifactId>
                <version>${spring.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

二、使用 GUI SWING

package code;

import lombok.SneakyThrows;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;

/**
 * 验证码测试1
 * <p>
 * 验证码:随机四个字符
 * 判断验证码的流程:  正确的验证码在request域中
 * 验证码生成需要gui:::swing
 *
 * @author yang
 * @version 1.0.0
 * @date 2020-09-13 21:31
 */
public class Test01 {
    @SneakyThrows
    public static void main(String[] args) {
        // 1 准备画板
        final BufferedImage bin = new BufferedImage(170, 40, BufferedImage.TYPE_INT_RGB);
        // 2 准备画笔
        final Graphics2D g = (Graphics2D) bin.getGraphics();
        // 3 设置背景颜色,白色
        g.setColor(Color.WHITE);
        // 4 画背景
        g.fillRect(1, 1, 168, 38);
        // 5 选择字体颜色
        g.setColor(Color.BLACK);
        // 6 创建一个 font ,指定字体样式
        final Font font = new Font(null, Font.BOLD, 30);
        g.setFont(font);
        // 7 选中字
        String hanZi = "爱我中华";
        for (int i = 0; i < hanZi.length(); i++) {
            final String c = hanZi.charAt(i) + "";
            g.drawString(c, 10 + i * 40, 30);
            g.drawLine(0, 0, 200, 40);
        }
        final FileOutputStream out = new FileOutputStream("/Users/yang/Documents/git/java/oracle/src/main/resources/verification-code/hanZi.jpg");
        ImageIO.write(bin, "JPEG", out);
    }
}

三、使用 hutool

package code;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.captcha.generator.RandomGenerator;
import cn.hutool.core.lang.Console;

import java.awt.*;

/**
 * hutool 工具类验证码
 *
 * @author yang
 * @version 1.0.0
 * @date 2020-09-13 21:43
 */
public class Test02 {
    public static void main(String[] args) {
        // 创建要随机的字符串,和个数
        final RandomGenerator hanZi = new RandomGenerator("爱我", 0);
        // 创建一个图形验证码,定义 长、宽、验证码字符数、干扰元素个数
        final CircleCaptcha lineCaptcha = CaptchaUtil.createCircleCaptcha(400, 400, 4, 4);
        // 设置字体
        lineCaptcha.setFont(new Font(null, Font.BOLD, 100));
        // 设置 设置验证码生成器
        lineCaptcha.setGenerator(hanZi);
        // 创建 创建验证码,实现类需同时生成随机验证码字符串和验证码图片
        lineCaptcha.createCode();
        // 打印 code
        Console.log(lineCaptcha.getCode());
        // 写入文件
        lineCaptcha.write("/Users/yang/Documents/git/java/oracle/src/main/resources/verification-code/hanZi1.jpg");
        // 验证
        System.out.println(lineCaptcha.verify("爱"));

    }
}

posted @ 2020-09-16 15:36  javaYanglei  阅读(126)  评论(0编辑  收藏  举报