【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数据库也可以(在数据量相对较小的情况下)

Spring Boot实现图片验证码可以通过以下步骤: 1. 添加依赖 在 `pom.xml` 文件中添加以下依赖: ```xml <dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency> ``` 2. 添加验证码配置 在 `application.properties` 文件中添加验证码的相关配置: ```properties # 验证码配置 kaptcha.border=yes kaptcha.border.color=105,179,90 kaptcha.textproducer.font.color=blue kaptcha.image.width=125 kaptcha.image.height=45 kaptcha.textproducer.font.size=30 kaptcha.session.key=code kaptcha.textproducer.char.length=4 kaptcha.textproducer.char.space=5 kaptcha.noise.color=black ``` 3. 创建验证码 Controller 创建一个 `CaptchaController` 类,并添加一个 `getCaptcha` 方法,用于获取验证码图片: ```java @Controller public class CaptchaController { @Autowired private Producer captchaProducer; @GetMapping("/captcha") public void getCaptcha(HttpServletRequest request, HttpServletResponse response) throws Exception { response.setHeader("Cache-Control", "no-store"); response.setHeader("Pragma", "no-cache"); response.setDateHeader("Expires", 0); response.setContentType("image/jpeg"); String captchaText = captchaProducer.createText(); request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, captchaText); BufferedImage captchaImage = captchaProducer.createImage(captchaText); ServletOutputStream outputStream = response.getOutputStream(); ImageIO.write(captchaImage, "jpg", outputStream); outputStream.close(); } } ``` 4. 在登录页面中使用验证码 在登录页面中添加一个验证码图片和一个输入框: ```html <img src="/captcha" onclick="this.src='/captcha?'+Math.random();" alt="验证码" /> <input type="text" name="captcha" placeholder="请输入验证码" required> ``` 5. 验证用户输入 在登录逻辑中,获取用户输入的验证码并与 session 中的验证码进行比较: ```java String captcha = request.getParameter("captcha"); String sessionCaptcha = (String) request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY); if (!captcha.equalsIgnoreCase(sessionCaptcha)) { // 验证码错误 } ``` 这样,就可以在 Spring Boot实现图片验证码了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

WalkerShen

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

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

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

打赏作者

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

抵扣说明:

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

余额充值