Java开发中验证码EasyCaptcha工具

在java日常开发中,需要生成图形验证码,通常可以采用基于java本身封装的工具类。下面尝试一下EasyCaptcha插件,用于图形验证码生成,支持gif验证码,可用于基于的session的web项目和前后端分离的项目。
在这里插入图片描述
实例
1、pom依赖

<dependency>
    <groupId>com.github.whvcse</groupId>
    <artifactId>easy-captcha</artifactId>
    <version>1.6.2</version>
</dependency>

2、使用

	@RequestMapping("/hello")
    public void hello(HttpServletResponse response) throws IOException {
        // png类型
        SpecCaptcha captcha = new SpecCaptcha(130, 48);
        // 获取验证码的字符
        String text = captcha.text();
        // 获取验证码的字符数组
        char[] chars = captcha.textChar();
        System.out.println("验证码:"+text);
        System.out.println("验证码:"+chars);
        // 输出验证码
        captcha.out(response.getOutputStream());
    }

    @RequestMapping("/hello2")
    public void hello2(HttpServletResponse response) throws IOException {
        // 三个参数分别为宽、高、位数
        GifCaptcha gifCaptcha = new GifCaptcha(100, 48, 4);
        // 设置类型:字母数字混合
        gifCaptcha.setCharType(Captcha.TYPE_DEFAULT);
        //获取验证码
        String text = gifCaptcha.text();
        System.out.println("验证码为:"+text);
        // 输出验证码
        gifCaptcha.out(response.getOutputStream());
    }

    @RequestMapping("/hello3")
    public void hello3(HttpServletResponse response) throws IOException {
        // 中文类型
        ChineseCaptcha captcha = new ChineseCaptcha(130, 48);
        //获取验证码
        String text = captcha.text();
        System.out.println("验证码为:"+text);
        // 输出验证码
        captcha.out(response.getOutputStream());
    }

    @RequestMapping("/hello4")
    public void hello4(HttpServletResponse response) throws IOException {
        // 算术类型
        ArithmeticCaptcha captcha = new ArithmeticCaptcha(130, 48);
        // 几位数运算,默认是两位
        captcha.setLen(3);
        // 获取运算的公式:4-9+1=?
        captcha.getArithmeticString();
        // 获取运算的结果:-4
        String text = captcha.text();
        System.out.println("计算结果为:"+text);
        // 输出验证码
        captcha.out(response.getOutputStream());
    }

上面依次是png类型、gif类型、中文类型、算数类型
测试结果
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
感兴趣的可以尝试一下,是否可以简化开发。加油吧,少年。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java生成图形验证码工具类很多,其比较常用的是使用第三方库生成验证码图片,比如Google的kaptcha和阿里巴巴的GifCaptcha等。 以下是一个使用kaptcha生成图形验证码的示例代码: ```java import com.google.code.kaptcha.impl.DefaultKaptcha; import com.google.code.kaptcha.util.Config; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.util.Properties; public class CaptchaUtils { private static DefaultKaptcha captchaProducer; static { captchaProducer = new DefaultKaptcha(); Properties properties = new Properties(); // 设置验证码图片的宽度 properties.setProperty("kaptcha.image.width", "120"); // 设置验证码图片的高度 properties.setProperty("kaptcha.image.height", "40"); // 设置验证码字符的字体 properties.setProperty("kaptcha.textproducer.font.names", "Arial,Courier"); // 设置验证码字符个数 properties.setProperty("kaptcha.textproducer.char.length", "4"); Config config = new Config(properties); captchaProducer.setConfig(config); } public static BufferedImage generateCaptcha(String text) { return captchaProducer.createImage(text); } public static byte[] generateCaptchaBytes(String text) { try { BufferedImage image = captchaProducer.createImage(text); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ImageIO.write(image, "png", outputStream); return outputStream.toByteArray(); } catch (Exception e) { e.printStackTrace(); return null; } } } ``` 使用示例: ```java // 生成验证码图片 BufferedImage image = CaptchaUtils.generateCaptcha("abcd"); // 将验证码图片转换成字节数组 byte[] bytes = CaptchaUtils.generateCaptchaBytes("abcd"); ``` 这个工具类使用了kaptcha库,可以方便地生成图形验证码图片。可以通过修改配置文件来定制验证码图片的样式和字符个数等参数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值