参考地址:https://gitee.com/ele-admin/EasyCaptcha
Java图形验证码,支持gif、中文、算术等类型,可用于Java Web、JavaSE等项目。
效果展示
验证码字符类型
类型 | 描述 |
---|---|
TYPE_DEFAULT | 数字和字母混合 |
TYPE_ONLY_NUMBER | 纯数字 |
TYPE_ONLY_CHAR | 纯字母 |
TYPE_ONLY_UPPER | 纯大写字母 |
TYPE_ONLY_LOWER | 纯小写字母 |
TYPE_NUM_AND_UPPER | 数字和大写字母 |
使用方法,例:
SpecCaptcha captcha3 = new SpecCaptcha(130, 48);
captcha3.setCharType(Captcha.TYPE_ONLY_NUMBER); // 纯数字
字体设置
字体 | 效果 |
---|---|
Captcha.FONT_1 | |
Captcha.FONT_2 | |
Captcha.FONT_3 | |
Captcha.FONT_4 | |
Captcha.FONT_5 | |
Captcha.FONT_6 | |
Captcha.FONT_7 | |
Captcha.FONT_8 | |
Captcha.FONT_9 | |
Captcha.FONT_10 | |
使用方式,例:
GifCaptcha captcha4 = new GifCaptcha(130, 48);// 设置内置字体captcha4.setFont(Captcha.FONT_10);// 设置系统字体captcha4.setFont(new Font("楷体", Font.PLAIN, 28));
使用方式
maven坐标:
<dependency><groupId>com.github.whvcse</groupId><artifactId>easy-captcha</artifactId><version>1.6.2</version>
</dependency>
测试代码:
package com.example.demo02;import com.wf.captcha.*;
import com.wf.captcha.base.Captcha;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;import java.awt.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;@SpringBootTest
class Demo02ApplicationTests {@Testvoid contextLoads() throws IOException, FontFormatException {//中文验证码Captcha captcha = new ChineseCaptcha(150,60);//指定图片的高度和宽度//获取本次生成的验证码String text = captcha.text();System.out.println(text);captcha.out(new FileOutputStream(new File(("d:\\test.png"))));//算术验证码ArithmeticCaptcha captcha1 = new ArithmeticCaptcha();captcha1.getArithmeticString(); // 获取运算的公式:3+2=?//获取本次生成的验证码String text1 = captcha1.text();System.out.println(text1);captcha1.out(new FileOutputStream(new File(("d:\\test1.png"))));// 中文gif类型ChineseGifCaptcha captcha2 = new ChineseGifCaptcha(130, 48);String text2 = captcha2.text();System.out.println(text2);captcha2.out(new FileOutputStream(new File(("d:\\test2.png"))));// png类型SpecCaptcha captcha3 = new SpecCaptcha(130, 48);captcha3.setCharType(Captcha.TYPE_ONLY_NUMBER); // 纯数字String text3 = captcha3.text();// 获取验证码的字符captcha3.textChar(); // 获取验证码的字符数组System.out.println(text3);captcha3.out(new FileOutputStream(new File("d:\\test3.png")));// gif类型GifCaptcha captcha4 = new GifCaptcha(130, 48);// 设置内置字体captcha4.setFont(Captcha.FONT_10);captcha3.setCharType(Captcha.TYPE_ONLY_CHAR); // 纯字母String text4 = captcha4.text();System.out.println(text4);captcha4.out(new FileOutputStream(new File("d:\\test4.png")));}
}
easycaptcha_demo入门案例
创建spring-boot工程easycaptcha_demo并配置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.2.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>easycaptcha_demo</artifactId><version>0.0.1-SNAPSHOT</version><name>demo02</name><description>Demo project for Spring Boot</description><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.github.whvcse</groupId><artifactId>easy-captcha</artifactId><version>1.6.2</version></dependency></dependencies>
</project>
创建CaptchaController类
package com.example.controller;import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;/*** 验证码案例*/
@RestController
@RequestMapping("/easycaptcha")
public class CaptchaController {/*** 生成验证码*/@RequestMapping("/captcha")public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {// 设置请求头为输出图片类型response.setContentType("image/gif");response.setHeader("Pragma", "No-cache");response.setHeader("Cache-Control", "no-cache");response.setDateHeader("Expires", 0);// 三个参数分别为宽、高、位数SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5);// 设置字体specCaptcha.setFont(new Font("Verdana", Font.PLAIN, 32)); // 有默认字体,可以不用设置// 设置类型,纯数字、纯字母、字母数字混合specCaptcha.setCharType(Captcha.TYPE_ONLY_NUMBER);// 验证码存入sessionrequest.getSession().setAttribute("captcha", specCaptcha.text().toLowerCase());// 输出图片流specCaptcha.out(response.getOutputStream());}/*** 校验验证码* @param username 用户名* @param password 密码* @param verCode 验证码*/@PostMapping("/login")public String login(HttpServletRequest request,String username,String password,String verCode){// 获取session中的验证码String sessionCode = (String) request.getSession().getAttribute("captcha");// 判断验证码if (verCode==null || !sessionCode.equals(verCode.trim().toLowerCase())) {return "验证码不正确";}return "登录成功";}
}
启动项目,效果:
正确:
错误: