当前位置: 代码网 > it编程>编程语言>Java > SpringBoot如何集成Kaptcha验证码

SpringBoot如何集成Kaptcha验证码

2025年01月07日 Java 我要评论
springboot集成kaptcha验证码简介在开发中,验证码功能是一个常见且重要的功能,kaptcha 是大名鼎鼎的谷歌公司提供的一款用于生成验证码的插件,支持高度可配置;本章将通过一个简单的示例

springboot集成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)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com