【springboot系列】springboot实现图片验证码captcha

使用

1、导入依赖

在maven仓库中查找,发现只有这个依赖,所以直接复制这个即可

<!--easy-captcha 验证码-->
        <dependency>
            <groupId>com.github.whvcse</groupId>
            <artifactId>easy-captcha</artifactId>
            <version>1.6.2</version>
        </dependency>

2、使用

总共有这么多种验证类型
image.png

3、测试

package com.walker.third.easyCaptcha;

import com.wf.captcha.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
public class CaptchaController {


    /**
    * 数字验证码
    */
    @GetMapping("/arithmeticCaptcha")
    public void captcha(HttpServletResponse response) throws IOException {
        ArithmeticCaptcha captcha = new ArithmeticCaptcha(138, 54);
        captcha.setLen(3);
        String arithmeticString = captcha.getArithmeticString();
        //获取算数过程的字符串
        System.out.println(arithmeticString);
        //获取算数结果
        System.out.println(captcha.text());

        response.setContentType("image/gif");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);

        captcha.out(response.getOutputStream());
    }


    /**
    * 中文验证码
    */
    @GetMapping("chineseCaptcha")
    public void chineseCaptcha(HttpServletResponse response) throws IOException {

        ChineseCaptcha chineseCaptcha = new ChineseCaptcha();
        chineseCaptcha.setLen(5);

        chineseCaptcha.out(response.getOutputStream());
    }

    /**
    * 字母闪烁图片
    */
    @GetMapping("gifCaptcha")
    public void gifCaptcha(HttpServletResponse response) throws IOException {
        GifCaptcha gifCaptcha = new GifCaptcha();
        gifCaptcha.out(response.getOutputStream());
    }

    /**
     * 中文闪烁图片
     */
    @GetMapping("chineseGifCaptcha")
    public void chineseGifCaptcha(HttpServletResponse response) throws IOException {
        ChineseGifCaptcha chineseGifCaptcha = new ChineseGifCaptcha();
        chineseGifCaptcha.out(response.getOutputStream());
    }


    /**
    * 字母图片
    */
    @GetMapping("specCaptcha")
    public void spec(HttpServletResponse response) throws IOException {
        SpecCaptcha specCaptcha = new SpecCaptcha();
        specCaptcha.out(response.getOutputStream());

    }
}

具体结果如下:
算数ArithmeticCaptcha:
image.png
中文ChineseCaptcha:
image.png

GifCaptcha
不断闪烁
image.png

SpecCaptcha:英文字母
image.png
中文gif:中文不断闪烁
image.png

4、最佳实践

可以将其与uuid和验证码结果保存在缓存中,如果是本地,可以保存在guava中,也可以保存在redis中,或者mysql数据库也可以(在数据量相对较小的情况下)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

WalkerShen

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

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

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

打赏作者

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

抵扣说明:

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

余额充值