使用easy-captcha工具获得验证码

EasyCaptcha的简单教程-gitee.com

一、简介

Java图形验证码,支持gif、中文、算术等类型,可用于Java Web、JavaSE等项目。

二、引入依赖

        <!--        引入验证码依赖-->
        <dependency>
            <groupId>com.github.whvcse</groupId>
            <artifactId>easy-captcha</artifactId>
            <version>1.6.2</version>
        </dependency>

三、生成几种类型验证码

1. 算术型验证码

controller

package com.zhuang.captcha.controller;

import com.wf.captcha.ArithmeticCaptcha;
import com.wf.captcha.utils.CaptchaUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;

import java.io.FileOutputStream;
import java.io.IOException;

import static java.awt.Font.*;

/**
 * @program: leanings
 * @description: 验证码
 * @author: mrzhuang
 * @create: 2022-08-11 21:09
 **/

@Slf4j
@RestController
@RequestMapping("/captcha")
public class EasyCaptchaController {

    @RequestMapping("/getCaparithmetictcha")
    public void   arithmetic(HttpServletResponse response) throws IOException {

        //创建算术类型的验证码
        ArithmeticCaptcha arithmeticCaptcha = new ArithmeticCaptcha(130,80,2);

        int height = arithmeticCaptcha.getHeight();
        int width = arithmeticCaptcha.getWidth();
        int len = arithmeticCaptcha.getLen();
        log.info("算术验证码的宽{},高{},算术个数{}", height, width, len);

        //获得算术验证码的结果(如:15)
        String text = arithmeticCaptcha.text();
        log.info("验证码的结果为:{}", text);

        //将获得的验证码的结果转化为字符(如:'1','6')
        char[] chars = arithmeticCaptcha.textChar();
        System.out.print("chars[]: " + "\t");
        for (int i = 0; i < chars.length; i++) {
            System.out.print(chars[i] + "\t");
        }
        System.out.println();
        String toBase64 = arithmeticCaptcha.toBase64();

        //获取算术型验证码字符串(如:"9+6=?")
        String arithmeticString = arithmeticCaptcha.getArithmeticString();
        log.info("chars.length:{},toBase64:{},arithmeticString:{}", chars.length, toBase64, arithmeticString);

        FileOutputStream fileOutputStream = new FileOutputStream("/Users/mrzhuang/Downloads/captcha.png");
        boolean out = arithmeticCaptcha.out(fileOutputStream);
        if (out){
            log.info("验证码写入图片图片成功!");
        }
        //将算术性型验证码写入到response中
        boolean out1 = arithmeticCaptcha.out(response.getOutputStream());
        if (out1){
            log.info("验证码写入response成功!");
        }

    }

}

CaptchaUtil类

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.wf.captcha.utils;

import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import java.awt.Font;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CaptchaUtil {
    private static final String SESSION_KEY = "captcha";
    private static final int DEFAULT_LEN = 4;
    private static final int DEFAULT_WIDTH = 130;
    private static final int DEFAULT_HEIGHT = 48;

    public CaptchaUtil() {
    }

    public static void out(HttpServletRequest request, HttpServletResponse response) throws IOException {
        out(4, request, response);
    }

    public static void out(int len, HttpServletRequest request, HttpServletResponse response) throws IOException {
        out(130, 48, len, request, response);
    }

    public static void out(int width, int height, int len, HttpServletRequest request, HttpServletResponse response) throws IOException {
        out(width, height, len, (Font)null, request, response);
    }

    public static void out(Font font, HttpServletRequest request, HttpServletResponse response) throws IOException {
        out(130, 48, 4, font, request, response);
    }

    public static void out(int len, Font font, HttpServletRequest request, HttpServletResponse response) throws IOException {
        out(130, 48, len, font, request, response);
    }

    public static void out(int width, int height, int len, Font font, HttpServletRequest request, HttpServletResponse response) throws IOException {
        SpecCaptcha specCaptcha = new SpecCaptcha(width, height, len);
        if (font != null) {
            specCaptcha.setFont(font);
        }

        out((Captcha)specCaptcha, request, response);
    }

// 将验证码以"captcha"为key,以验证码的值为value,放入请求的session中
    public static void out(Captcha captcha, HttpServletRequest request, HttpServletResponse response) throws IOException {
        setHeader(response);
        request.getSession().setAttribute("captcha", captcha.text().toLowerCase());
        captcha.out(response.getOutputStream());
    }
// 取出请求的session中的验证码,与页面填入的值进行验证
    public static boolean ver(String code, HttpServletRequest request) {
        if (code != null) {
            String captcha = (String)request.getSession().getAttribute("captcha");
            return code.trim().toLowerCase().equals(captcha);
        } else {
            return false;
        }
    }

    public static void clear(HttpServletRequest request) {
        request.getSession().removeAttribute("captcha");
    }

    public static void setHeader(HttpServletResponse response) {
        response.setContentType("image/gif");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0L);
    }
}

    ```

1.1 结果

方式一:写入response体
方式二:写入png文件
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值