Java后端快速生成验证码

发布于:2025-05-14 ⋅ 阅读:(12) ⋅ 点赞:(0)

Hutool是一个小而全的Java工具类库,它提供了很多实用的工具类,包括但不限于日期处理、加密解密、文件操作、反射操作、HTTP客户端等。

核心工具类:CaptchaUtil,CaptchaUtil 是 Hutool 提供的一个工具类,用于创建各种类型的验证码。它提供了静态工厂方法,帮助我们快速生成不同风格的验证码。

POM依赖

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.3</version>
</dependency>

CaptchaUtil

package cn.hutool.captcha;

/**
 * 图形验证码工具
 */
public class CaptchaUtil {

	/**
	 * 创建线干扰的验证码,默认5位验证码,150条干扰线
	 * @param width  图片宽
	 * @param height 图片高
	 */
	public static LineCaptcha createLineCaptcha(int width, int height) {
		return new LineCaptcha(width, height);
	}

	/**
	 * 创建线干扰的验证码
	 * @param width     图片宽
	 * @param height    图片高
	 * @param codeCount 字符个数
	 * @param lineCount 干扰线条数
	 */
	public static LineCaptcha createLineCaptcha(int width, int height, int codeCount, int lineCount) {
		return new LineCaptcha(width, height, codeCount, lineCount);
	}

	/**
	 * 创建圆圈干扰的验证码,默认5位验证码,15个干扰圈
	 * @param width  图片宽
	 * @param height 图片高
	 */
	public static CircleCaptcha createCircleCaptcha(int width, int height) {
		return new CircleCaptcha(width, height);
	}

	/**
	 * 创建圆圈干扰的验证码
	 * @param width       图片宽
	 * @param height      图片高
	 * @param codeCount   字符个数
	 * @param circleCount 干扰圆圈条数
	 */
	public static CircleCaptcha createCircleCaptcha(int width, int height, int codeCount, int circleCount) {
		return new CircleCaptcha(width, height, codeCount, circleCount);
	}

	/**
	 * 创建扭曲干扰的验证码,默认5位验证码
	 * @param width  图片宽
	 * @param height 图片高
	 */
	public static ShearCaptcha createShearCaptcha(int width, int height) {
		return new ShearCaptcha(width, height);
	}

	/**
	 * 创建扭曲干扰的验证码,默认5位验证码
	 * @param width     图片宽
	 * @param height    图片高
	 * @param codeCount 字符个数
	 * @param thickness 干扰线宽度
	 */
	public static ShearCaptcha createShearCaptcha(int width, int height, int codeCount, int thickness) {
		return new ShearCaptcha(width, height, codeCount, thickness);
	}

	/**
	 * 创建GIF验证码
	 * @param width 宽
	 * @param height 高
	 */
	public static GifCaptcha createGifCaptcha(int width, int height) {
		return new GifCaptcha(width, height);
	}

	/**
	 * 创建GIF验证码
	 * @param width 宽
	 * @param height 高
	 * @param codeCount 字符个数
	 */
	public static GifCaptcha createGifCaptcha(int width, int height, int codeCount) {
		return new GifCaptcha(width, height, codeCount);
	}
}

前端页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
</head>
<body>
	<form action="/login" method="post">
	    用户名:<input type="text" name="username"><br>
	    密码:<input type="password" name="password"><br>
	    验证码:<input type="text" name="verificationCode"><img src="/verification-code/getImage">
	    <input type="submit" value="登录">
	</form>
</body>
</html>

后端代码

@Controller
@RequestMapping("/verification-code")
@Slf4j
public class VerificationCodeController {
    @GetMapping("/getImage")
    public void generateCaptcha(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 1. 生成验证码图片
        // 图片宽:90 图片高:30 字符个数:4 干扰线宽度:3
        ICaptcha captcha = CaptchaUtil.createShearCaptcha(90, 30, 4, 3);
        // 2. 将生成的验证码(文本)保存到 Redis / Session 中
        // 存入Redis / Session后,在后续可通过拦截器进行校验
        request.getSession().setAttribute("verificationCode", captcha.getCode());
        // 3. 设置响应类型为 JPEG 图片
        response.setContentType("image/jpeg");
        // 4. 回写
        captcha.write(response.getOutputStream());
    }
}

验证码效果

在这里插入图片描述


网站公告

今日签到

点亮在社区的每一天
去签到