SpringBoot kaptcha验证码

本文详细介绍了使用Kaptcha库在Spring Boot应用中生成图形验证码的方法,包括配置属性以定制验证码的样式,如边框颜色、字体大小等,并展示了如何将验证码图片输出到HTTP响应及验证用户输入的过程。

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

		<!--验证码-->
        <dependency>
            <groupId>com.github.penggle</groupId>
            <artifactId>kaptcha</artifactId>
            <version>2.3.2</version>
        </dependency>
package com.zichan360.config;

import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

@Configuration
public class KaptchaConfig {

    @Bean
    Producer getCaptchaProducer(){
        DefaultKaptcha producer = new DefaultKaptcha();
        Properties properties = new Properties();
        properties.put("kaptcha.border", "yes");
		properties.put("kaptcha.border.color", "105,179,90");
		properties.put("kaptcha.textproducer.font.color", "blue");
		properties.put("kaptcha.image.width", "125");
		properties.put("kaptcha.image.height", "45");
		properties.put("kaptcha.textproducer.font.size", "40");
		properties.put("kaptcha.session.key", "code");
		properties.put("kaptcha.textproducer.char.length", "4");
		properties.put("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
        producer.setConfig(new Config(properties));
        return producer;
    }
}

 /**
     * @Author zhaohp
     * @Date 2018/10/24 15:52
     * @Param []
     * @Return com.zichan360.common.result.Result
     * @Description: 获取图形验证码 将验证码存入session中
     */
    @GetMapping(value = "/captcha")
    public Result getCaptcha() throws Exception {

        HttpSession session = httpServletRequest.getSession();
        httpServletResponse.setDateHeader("Expires", 0);
        httpServletResponse.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        httpServletResponse.addHeader("Cache-Control", "post-check=0, pre-check=0");
        httpServletResponse.setHeader("Pragma", "no-cache");
        httpServletResponse.setContentType("image/jpeg");

        String capText = captchaProducer.createText();
        session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
        BufferedImage bi = captchaProducer.createImage(capText); // 如何能改用byte[]输出呢?
        ServletOutputStream out = httpServletResponse.getOutputStream();
        ImageIO.write(bi, "jpg", out);
        byte[] result = new byte[]{};
        out.write(result);
        try {
            out.flush();
        } finally {
            out.close();
        }
        return null;
    }
        if (!captcha.equals(session.getAttribute(Constants.KAPTCHA_SESSION_KEY))) {
            return ResultUtil.sendErrorMessage("图形验证码有误!");
        }
       session.removeAttribute(Constants.KAPTCHA_SESSION_KEY);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值