Springboot整合hutool验证码

在 Spring Boot 中,你可以将 Hutool 生成验证码的功能集成到 RESTful API 接口中。

依赖

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.14</version> <!-- 使用最新版本 -->
</dependency>

创建验证码

package com.base.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.captcha.ShearCaptcha;
import cn.hutool.captcha.generator.MathGenerator;
import cn.hutool.captcha.generator.RandomGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/api/captcha")
@Api(tags = "验证码")
public class CaptchaController {

    @GetMapping("/image")
    @ApiOperation("线段干扰的验证码")
    public void getCaptchaImage(HttpServletResponse response) throws IOException {
        //定义图形验证码的长和宽
        LineCaptcha captcha = CaptchaUtil.createLineCaptcha(200, 100);
        System.out.println("验证码:"+captcha.getCode());

        // 设置响应类型为图片
        response.setContentType("image/png");

        // 将验证码图片写入响应
        captcha.write(response.getOutputStream());
    }
    @GetMapping("/image2")
    @ApiOperation("圆圈干扰验证码")
    public void getCaptchaImage2(HttpServletResponse response) throws IOException {
        // 创建验证码对象
        //定义图形验证码的长、宽、验证码字符数、干扰元素个数
        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 20);
        System.out.println("验证码:"+captcha.getCode());

        // 设置响应类型为图片
        response.setContentType("image/png");

        // 将验证码图片写入响应
        captcha.write(response.getOutputStream());
    }

    @GetMapping("/image3")
    @ApiOperation("扭曲干扰验证码")
    public void getCaptchaImage3(HttpServletResponse response) throws IOException {
        //定义图形验证码的长、宽、验证码字符数、干扰线宽度
        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(200, 100, 4, 4);
        System.out.println("验证码:"+captcha.getCode());

        // 设置响应类型为图片
        response.setContentType("image/png");

        // 将验证码图片写入响应
        captcha.write(response.getOutputStream());
    }

    @GetMapping("/image4")
    @ApiOperation("自定义纯数字的验证码")
    public void getCaptchaImage4(HttpServletResponse response) throws IOException {
        //定义图形验证码的长、宽、验证码字符数、干扰线宽度
        // 自定义纯数字的验证码(随机4位数字,可重复)
        RandomGenerator randomGenerator = new RandomGenerator("0123456789", 4);
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
        lineCaptcha.setGenerator(randomGenerator);
        // 重新生成code
        lineCaptcha.createCode();
        // 设置响应类型为图片
        response.setContentType("image/png");
        // 将验证码图片写入响应
        lineCaptcha.write(response.getOutputStream());
    }
    @GetMapping("/image5")
    @ApiOperation("加减乘除的验证码")
    public void getCaptchaImage5(HttpServletResponse response) throws IOException {
        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(200, 45, 4, 4);
        // 自定义验证码内容为四则运算方式
        captcha.setGenerator(new MathGenerator(1));
        // 重新生成code
        captcha.createCode();
        MathGenerator mathGenerator = new MathGenerator();

//      用户输入校验
        System.out.println("验证结果:"+mathGenerator.verify(captcha.getCode(), "1"));


        // 设置响应类型为图片
        response.setContentType("image/png");
        // 将验证码图片写入响应
        captcha.write(response.getOutputStream());
    }
}

访问验证码接口

上面提到的5种样式,效果如下:

image-20240813152106356

image-20240813152125666

image-20240813152137508

image-20240813152150645

image-20240813152253360

参考:文档

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序员-小李

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

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

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

打赏作者

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

抵扣说明:

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

余额充值