一、简介
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);
}
}
```