SpringBoot项目中集成Kaptcha

1.Kaptcha简介

Kaptcha是一个流行的Java库,用于生成验证码(CAPTCHA)图片。CAPTCHA是"Completely Automated Public Turing test to tell Computers and Humans Apart"的缩写,通常用于在线表单验证以防止机器人或自动化工具的滥用,保护网站不被自动注册帐户、发送垃圾邮件等使用。

Kaptcha提供了易于使用的方法来快速集成和产生各种风格的验证码图片,它支持自定义字体、文字颜色、大小以及添加的噪声等级等,从而使得验证码看起来多种多样并且对机器具有一定的识别难度。它在Spring框架中也非常容易整合。

2.SpringBoot项目中引入Kaptcha

2.1.引入依赖
复制代码
<!-- Kaptcha 生成随机字符、生成图片(生成用于验证码)-->
<dependency>
    <groupId>com.oopsguy.kaptcha</groupId>
    <artifactId>kaptcha-spring-boot-starter</artifactId>
    <version>1.0.0-beta-2</version>
</dependency>
2.2.配置工具类
复制代码
package com.duhong.util;
import com.google.code.kaptcha.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
@Component
public class KaptchaProducerUtil {
    @Autowired
    private Producer producer;

    public  String createText(){
        return producer.createText();
    }
    public BufferedImage createImage(String text){
        // 生成初始图像
        BufferedImage captchaImage = producer.createImage(text);

        //获取图像的画笔以便添加干扰线
        Graphics2D g2d = captchaImage.createGraphics();

        //设置干扰线的颜色
        g2d.setColor(Color.PINK); // 或者你可以使用其他颜色

        //设置干扰线的笔触,使线条更粗
        g2d.setStroke(new BasicStroke(3)); // 2 是线条的宽度

        //随机生成干扰线的起点和终点坐标
        Random random = new Random();
        int x1 = random.nextInt(captchaImage.getWidth());
        int y1 = random.nextInt(captchaImage.getHeight());
        int x2 = random.nextInt(captchaImage.getWidth());
        int y2 = random.nextInt(captchaImage.getHeight());

        //在图像上绘制线条
        g2d.drawLine(x1, y1, x2, y2);

       //绘制3条线条
        for (int i = 0; i < 3; i++) {
            x1 = random.nextInt(captchaImage.getWidth());
            y1 = random.nextInt(captchaImage.getHeight());
            x2 = random.nextInt(captchaImage.getWidth());
            y2 = random.nextInt(captchaImage.getHeight());
            g2d.drawLine(x1, y1, x2, y2);
        }

        // 释放画笔资源
        g2d.dispose();
        return captchaImage;
    }

}
2.3.测试:
复制代码
@Test
void kaptchaTest(){
    String text=producer.createText();
    BufferedImage image = producer.createImage(text);


    File outputFile = new File("C:\\Users\\bumiexiguang\\OneDrive\\桌面\\毕业设计\\output.png"); // 输出文件路径和名称
    try {
        ImageIO.write(image, "png", outputFile);
        System.out.println("Image saved successfully.");
    } catch (IOException e) {
        System.out.println("Error saving image: " + e.getMessage());
    }
}
2.4.效果:
相关推荐
追随者永远是胜利者32 分钟前
(LeetCode-Hot100)253. 会议室 II
java·算法·leetcode·go
追随者永远是胜利者1 小时前
(LeetCode-Hot100)207. 课程表
java·算法·leetcode·go
yanghuashuiyue3 小时前
lambda+sealed+record
java·开发语言
山岚的运维笔记3 小时前
SQL Server笔记 -- 第73章:排序/对行进行排序
数据库·笔记·后端·sql·microsoft·sqlserver
盟接之桥3 小时前
盟接之桥EDI软件:API数据采集模块深度解析,打造企业数据协同新引擎
java·运维·服务器·网络·数据库·人工智能·制造
苍何3 小时前
豆包还能这么玩?附 13 大隐藏玩法,效率起飞(建议收藏)
后端
苍何3 小时前
Kimi 版 OpenClaw 来了,5000+ Skills 随便用,确实给力!
后端
HoneyMoose4 小时前
Spring Boot 2.4 部署你的第一个 Spring Boot 应用需要的环境
java
rfidunion4 小时前
springboot+VUE+部署(12。Nginx和前端配置遇到的问题)
前端·vue.js·spring boot
皮皮林5514 小时前
为什么 Spring 和 IDEA 都不推荐使用 @Autowired 注解??
java