SpringBoot集成Kaptcha验证码

目录

简介

实现步骤

1. 在 pom.xml 配置文件中,添加如下配置;由于国内限制了谷歌网络的访问,推荐使用下面的依赖下载

2. 在系统公共配置类中添加如下代码;当然关于 Kaptcha 的配置也可以添加到 application.properties 配置文件中

3. 在 KaptchController.class 中添加提供验证码生成的方法

4. 前端页面直接使用 img 标签引用即可

补充:Kaptcha 更多配置


简介

在开发中,验证码功能是一个常见且重要的功能,Kaptcha 是大名鼎鼎的谷歌公司提供的一款用于生成验证码的插件,支持高度可配置;本章将通过一个简单的示例展示如何实现验证码功能

实现步骤

1. 在 pom.xml 配置文件中,添加如下配置;由于国内限制了谷歌网络的访问,推荐使用下面的依赖下载

<dependency>
    <groupId>com.github.penggle</groupId>
    <artifactId>kaptcha</artifactId>
    <version>2.3.2</version>
</dependency>

2. 在系统公共配置类中添加如下代码;当然关于 Kaptcha 的配置也可以添加到 application.properties 配置文件中

@Configuration
public class AppConfigure implements WebMvcConfigurer {
    /**
     * 验证码配置
     */
    @Bean
    public DefaultKaptcha kaptcha() {
        DefaultKaptcha kaptcha = new DefaultKaptcha();
        Properties properties = new Properties();
        properties.put("kaptcha.border", "yes");
        properties.put("kaptcha.image.width", "100");
        properties.put("kaptcha.image.height", "33");
        properties.put("kaptcha.session.key", "code");
        properties.put("kaptcha.border.color", "105,179,90");
        properties.put("kaptcha.textproducer.font.size", "30");
        properties.put("kaptcha.textproducer.char.length", "4");
        properties.put("kaptcha.textproducer.font.color", "blue");
        properties.put("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
        kaptcha.setConfig(new Config(properties));
        return kaptcha;
    }
}

3. 在 KaptchController.class 中添加提供验证码生成的方法

@Controller
@RequestMapping("/kaptcha")
@Slf4j
public class KaptchaController {
    @Resource
    private DefaultKaptcha kaptcha;
    
    /**
     * 申请验证码
     */
    @GetMapping("/kaptcha")
    public void getKaptcha(HttpServletRequest request, HttpServletResponse response) {
            response.setDateHeader("Expires", 0);
            response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
            response.addHeader("Cache-Control", "post-check=0, pre-check=0");
            response.setHeader("Pragma", "no-cache");
            response.setContentType("image/jpeg");

            HttpSession session = request.getSession();
            ServletOutputStream outputStream = null;
            try {
                //生成验证码
                String kaptchaText = kaptcha.createText();
                // 将验证码保存 5 分钟
                CommonUtils.setSession(session, Properties.KAPTCHA.desc(), kaptchaText, 
                    Properties.EXPIRETIME_KAPTCHA.value());
                log.info("captcha code: " + kaptchaText);
                //向客户端输出
                BufferedImage bufferedImage = kaptcha.createImage(kaptchaText);
                outputStream = response.getOutputStream();
                ImageIO.write(bufferedImage, "jpg", outputStream);
                outputStream.flush();
            } catch (IOException e) {
                throw new BusinessException(ErrorCode.CLOSE_IO_EXCEPTION);
            } finally {
                CommonUtils.closeio(outputStream);
            }
        }
        ......
}

4. 前端页面直接使用 img 标签引用即可

<img src="/kaptcha/kaptcha" id="kaptcha-img" title="点击刷新">

补充:Kaptcha 更多配置

属性(常量)描述默认值
kaptcha.border图片边框,合法值:yes , noyes
kaptcha.border.color边框颜色,合法值: r,g,b (and optional alpha) 或者 white,black,blue.black
kaptcha.border.thickness边框厚度,合法值:>01
kaptcha.image.width图片宽200
kaptcha.image.height图片高50
kaptcha.producer.impl图片实现类com.google.code.kaptcha.impl.DefaultKaptcha  
kaptcha.textproducer.impl文本实现类com.google.code.kaptcha.text.impl.DefaultTextCreator  
kaptcha.textproducer.char.string文本集合,验证码值从此集合中获取abcde2345678gfynmnpwx
kaptcha.textproducer.char.length验证码长度5
kaptcha.textproducer.font.names字体Arial, Courier
kaptcha.textproducer.font.size字体大小40px
kaptcha.textproducer.font.color字体颜色,合法值: r,g,b  或者 white,black,blue.black
kaptcha.textproducer.char.space文字间隔2
kaptcha.noise.impl干扰实现类com.google.code.kaptcha.impl.DefaultNoise
kaptcha.noise.color干扰颜色,合法值: r,g,b 或者 white,black,blue.black
kaptcha.obscurificator.impl图片样式: 水纹com.google.code.kaptcha.impl.WaterRipple 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy 阴影com.google.code.kaptcha.impl.ShadowGimpycom.google.code.kaptcha.impl.WaterRipple
kaptcha.background.impl背景实现类com.google.code.kaptcha.impl.DefaultBackground
kaptcha.background.clear.from背景颜色渐变,开始颜色light grey
kaptcha.background.clear.to背景颜色渐变,结束颜色white
kaptcha.word.impl文字渲染器com.google.code.kaptcha.text.impl.DefaultWordRenderer  
kaptcha.session.keysession keyKAPTCHA_SESSION_KEY
kaptcha.session.datesession dateKAPTCHA_SESSION_DATE
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值