kaptcha——谷歌验证码工具

本文档介绍了如何在Java项目中使用Kaptcha工具生成谷歌验证码,包括引入依赖、配置参数以及生成验证码图片的方法。通过配置字体、颜色、干扰线等属性,可以定制化的创建各种样式的验证码,简化了开发流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

kaptcha——谷歌验证码工具

一、说在开始

小伙伴,你好! 这是本人第一次分享,记录一下自己在java中的学习过程。因为设计需求,在登录的时候需要进行图片验证登录。虽然也可以单独使用代码实现但是,当知道有更好的工具可以用的时候谁不想简单点呢?

二、工具简介

  1. 验证码的字体
  2. 验证码字体的字体颜色
  3. 验证码内容的范围(数字,字母,中文汉字!)
  4. 验证码图片的大小,边框,边框粗细,边框颜色
  5. 验证码的干扰线
  6. 验证码的样式(鱼眼样式、3D、普通模糊、…)

三、用法

1、引入jar包
可以去官网http://code.google.com/p/kaptcha/下载jar,或者在pom.xml中导入

<dependency>
    <groupId>com.github.penggle</groupId>
    <artifactId>kaptcha</artifactId>
    <version>2.3.2</version>
</dependency>

2、编写配置文件 KaptChaConfig.class

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;

import java.util.Properties;

/**
 * @author dk
 * @version 1.0.0
 * @ClassName KaptChaConfig.java
 * @Description TODO
 * @createTime 2021年11月20日 15:11:00
 */
@Component
public class KaptChaConfig {
    @Bean
    public DefaultKaptcha getDefaultKaptcha() {
        com.google.code.kaptcha.impl.DefaultKaptcha defaultKaptcha = new com.google.code.kaptcha.impl.DefaultKaptcha();
        Properties properties = new Properties();
        // 图片边框
        properties.setProperty("kaptcha.border", "no");
        // 边框颜色
        properties.setProperty("kaptcha.border.color", "black");
        //边框厚度
        properties.setProperty("kaptcha.border.thickness", "1");
        // 图片宽
        properties.setProperty("kaptcha.image.width", "120");
        // 图片高
        properties.setProperty("kaptcha.image.height", "60");
        //图片实现类
        properties.setProperty("kaptcha.producer.impl", "com.google.code.kaptcha.impl.DefaultKaptcha");
        //文本实现类
        properties.setProperty("kaptcha.textproducer.impl", "com.google.code.kaptcha.text.impl.DefaultTextCreator");
        //文本集合,验证码值从此集合中获取
        properties.setProperty("kaptcha.textproducer.char.string", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
        //验证码长度
        properties.setProperty("kaptcha.textproducer.char.length", "4");
        //字体
        properties.setProperty("kaptcha.textproducer.font.names", "宋体");
        //字体颜色
        properties.setProperty("kaptcha.textproducer.font.color", "black");
        //文字间隔
        properties.setProperty("kaptcha.textproducer.char.space", "4");
        //干扰实现类
        properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.DefaultNoise");
        //干扰颜色
        properties.setProperty("kaptcha.noise.color", "blue");
        //干扰图片样式
        properties.setProperty("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.WaterRipple");
        //背景实现类
        properties.setProperty("kaptcha.background.impl", "com.google.code.kaptcha.impl.DefaultBackground");
        //背景颜色渐变,结束颜色
        properties.setProperty("kaptcha.background.clear.to", "white");
        //文字渲染器
        properties.setProperty("kaptcha.word.impl", "com.google.code.kaptcha.text.impl.DefaultWordRenderer");
        Config config = new Config(properties);
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }
}

3、生成图片的方法 YanzhengmaUtil.class

import java.awt.image.BufferedImage;
        import java.io.File;
        import javax.imageio.ImageIO;
        import javax.servlet.http.HttpServletRequest;
        import javax.servlet.http.HttpServletResponse;
        import org.apache.commons.io.FileUtils;
        import org.springframework.util.FastByteArrayOutputStream;
        import com.google.code.kaptcha.impl.DefaultKaptcha;
public class YanzhengmaUtil {

    /**
     * 生成验证码图片
     * @param request 设置session
     * @param response 转成图片
     * @param captchaProducer 生成图片方法类
     * @param validateSessionKey session名称
     * @throws Exception
     */

    public static void validateCode(HttpServletRequest request, HttpServletResponse response, DefaultKaptcha captchaProducer, String validateSessionKey) throws Exception{
//        response.setDateHeader("Expires", 0);
//        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
//        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
//        response.setHeader("Pragma", "no-cache");
//        response.setContentType("image/jpeg");

        // create the text for the image
        String capText = captchaProducer.createText();

        // store the text in the session
        request.getSession().setAttribute(validateSessionKey, capText);

        System.out.println(request.getSession().getAttribute(validateSessionKey));
        System.out.println(validateSessionKey+":"+capText);
        // create the image with the text
        BufferedImage bi = captchaProducer.createImage(capText);

        //  OutputStream out = response.getOutputStream();
        FastByteArrayOutputStream os = new FastByteArrayOutputStream();

        // write the data out
        ImageIO.write(bi, "jpg", os);

        FileUtils.writeByteArrayToFile(new File("D:\\ice\\code\\ice_vue_test\\src\\pages\\login\\vercode.jpg"), os.toByteArray());
        try {
            os.flush();
        } finally {
            os.close();
        }
    }
}

D:\ice\code\ice_vue_test\src\pages\login\vercode.jpg
这个是设置你保存的本地地址

4、调用接口

这里就不用再多说了,直接调用方法就是。值得注意的就是 当某一个地方使用了springboot 的相关注解的时候,再另外一个地方如果想要使用该注解下的方法或者类,这是也需要加上Springboot与之相关的注解,不然会报错误。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

邪恶小白凸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值