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文件

使用easy-captcha工具获得验证码相关推荐

  1. Python + selenium自动化工具 + 滑块验证码+点选验证码,实现模拟登录“中国铁路网12306”

    文章目录 一.模拟登录"中国铁路网12306 1.引入库 2.初始化 3.将点选验证码图片,通过人工打码,返回目标像素位置(json格式). 4.点选验证码位置得到后,需要鼠标左击进行模拟人 ...

  2. 基于captcha的图形验证码实现

    本案例基于SpringBoot构建,原理为后台生成base64的图片资源,前端HTML进行渲染 为简化代码偷懒,用到lombok ^_^ 1.captcha版本选择,pom.xml中配置captcha ...

  3. django使用captcha完成图片验证码

    使用captcha 1:需要下载包: pip install django-simple-captcha 2: 然后在settings的INSTALLED_APPS添加: 'captcha' 3: 需 ...

  4. 模拟1688(跨境产品开发工具)滑块验证码案例

    模拟阿里1688(跨境产品开发工具以图搜图)滑块验证码的案例演示: 网址如下,需要登陆后才能使用以图搜图的功能: https://kj.1688.com/pdt_tongkuan.html?spm=a ...

  5. python验证码生成器_Python captcha模块生成验证码

    需要的方法 img=ImageCaptcha(width=160, height=60, fonts=None, font_sizes=None) #实例化ImageCaptcha类 image=im ...

  6. android 好用的自定义倒计时工具(验证码发送倒计时等等)

    可用于短信发送倒计时,请求倒计时等各个地方,使用方便,与组件解耦. import android.os.CountDownTimer; import android.view.View; /*** 倒 ...

  7. java验证码工具_java 验证码工具

    importjavax.imageio.ImageIO;import java.awt.*;importjava.awt.image.BufferedImage;importjava.io.IOExc ...

  8. linux profiling 工具,高性能:LEP (LINUX EASY PROFILING) 工具介绍

    LEP工具入门 给大家推荐个宋宝华老师出品的小工具 相关文档: LEP的介绍,大家直接看上面的文档即可. 下面是我记录的安装部署过程 环境:CentOS7.7 IP: 10.10.11.11 安装le ...

  9. 在TP5使用 captcha 验证码验证码功能

    在TP5使用 captcha 验证码验证码功能 使用composer,安装captcha 1.如果你的电脑没有安装 composer 则先要安装,安装步骤及下载地址 TP5项目中下载,安装compos ...

最新文章

  1. 困扰数学家25年的“切苹果”难题,被一位华人统计学博士解决了
  2. 团队nabcd(校园大事件)
  3. 中欧谋定原产地保护-农业大健康·万祥军:战略格局地理标志
  4. 13.SpringMVC和Spring集成(一) 14.SpringMVC和Spring集成(二)
  5. 收不回来的value
  6. 王爽 汇编语言第三版 第10章 call 和 ret 指令 以及 子程序设计
  7. 通用sqlserver分页存储过程
  8. ubuntu16.04下ROS操作系统学习笔记(七 )机器语音-语音听写-科大讯飞SDK调用
  9. Java过滤器和拦截器的区别
  10. 告别奇虎360、依图科技,再谋他途!依图CTO颜水成被曝离职!
  11. QT C++ Luhn算法验证银行卡号合法性
  12. 爬虫学习笔记(用python爬取东方财富网实验)
  13. SpringSecurity退出登录logout报错404
  14. python中type(12.34)_Python typing.TYPE_CHECKING属性代码示例
  15. 基于C语言实现了PASCAL编译器
  16. 酒浓码浓 - node之http
  17. 夏新Q32s 蓝牙耳机 使用说明
  18. Node模块--anser
  19. Canvas 教程:如何绘制带箭头的曲线
  20. 安装python环境变量配置没问题啊_这是什么,Python安装好了,环境变量也配置没,就是报下面错误,指点!...

热门文章

  1. MobileNet论文翻译
  2. 局域网计算机修改网络用户,windows10怎么修改局域网网络名称
  3. Python-自动化测试之接口基础
  4. leetcode-977
  5. 显示和隐藏mac文件
  6. 7 Distributed System notes:A feature of distributed system - Transparency (分布式系统的透明性特征)
  7. 用Cadence Virtuoso IC617仿真V-I特性曲线
  8. Entry键值对对象
  9. Gartner2021新兴技术和趋势影响力雷达图:四项颇具影响力的技术
  10. 避开“恐怖谷”,机器人的眼睛应该这样开发…