Kaptcha 验证码框架使用

Kaptcha 验证码框架使用基于springboot验证码框架kaptcha使用一、统一步骤引入maven坐标<dependency><groupId>com.github.penggle</groupId><artifactId>kaptcha</artifactId>…

大家好,又见面了,我是你们的朋友全栈君。

基于springboot 验证码框架kaptcha使用

一、统一步骤引入maven坐标

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

二、配置验证码生成规则并将框架实现类导入Spring容器

	@Bean
    public DefaultKaptcha getDefaultKaptcha() { 
   
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        Properties properties = new Properties();
        // 图片边框,合法值yes,no,默认值yes
        properties.setProperty("kaptcha.border", "no");
        // 边框颜色,合法值rgb(and optional alpha)或者 white,black,blue,默认值black
        properties.setProperty("kaptcha.border.color", "105,179,90");
        // 边框厚度,合法值>0,默认值为1
        properties.setProperty("kaptcha.border.color", "2");
        // 图片宽度,默认值200
        properties.setProperty("kaptcha.image.width", "200");
        // 图片高度,默认值50
        properties.setProperty("kaptcha.image.height", "50");
        // 验证码长度,默认值为5
        properties.setProperty("kaptcha.textproducer.char.length", "4");
        // 字体,默认值Arial, Courier(如果使用中文验证码,则必须使用中文的字体,否则出现乱码)
        properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
        // 字体颜色,合法值: r,g,b 或者 white,black,blue,默认值black
        properties.setProperty("kaptcha.textproducer.font.color", "black");
        // 字体大小,默认值为40px
        properties.setProperty("kaptcha.textproducer.font.size", "40");
        // 文字间隔,默认值为2
        properties.setProperty("kaptcha.textproducer.char.space", "3");
        // 干扰 颜色,合法值: r,g,b 或者 white,black,blue,默认值black
        properties.setProperty("kaptcha.noise.color", "blue");
        // 水纹com.google.code.kaptcha.impl.WaterRipple
        // 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy
        // 阴影com.google.code.kaptcha.impl.ShadowGimpy
        properties.setProperty("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.ShadowGimpy");
        // 背景颜色渐变,开始颜色,默认值lightGray/192,193,193
        properties.setProperty("kaptcha.background.clear.from", "255,255,255");
        // 背景颜色渐变, 结束颜色,默认值white
        properties.setProperty("kaptcha.background.clear.to", "white");
        Config config = new Config(properties);
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }

三、编写controller

@Controller
public class VerifyCodeController { 
   

    @Autowired
    DefaultKaptcha defaultKaptcha;

    @RequestMapping("/verifyCode")
    public void verifyCode(HttpServletResponse httpServletResponse) throws IOException { 
   
        // 获取字符串验证码
        String verifyCodeStr = defaultKaptcha.createText();
        // 通过字符串验证码生成图片验证码
        BufferedImage verifyCodeImage = defaultKaptcha.createImage(verifyCodeStr);
        // 设置响应头
        httpServletResponse.setHeader("Cache-Control", "no-store");
        httpServletResponse.setHeader("Pragma", "no-cache");
        httpServletResponse.setContentType("image/jpeg");
        ServletOutputStream ops = httpServletResponse.getOutputStream();
        // 将验证码写出浏览器
        ImageIO.write(verifyCodeImage, "jpg", ops);
    }
}

生成的验证码效果:
在这里插入图片描述
四、以下是验证码生成规则配置表。

Constant 描述 默认值
kaptcha.border 图片边框,合法值:yes , no yes
kaptcha.border.color 边框颜色,合法值: r,g,b (and optional alpha) 或者 white,black,blue black
kaptcha.border.thickness 边框厚度,合法值:>0 1
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.ShadowGimpy com.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.key session key KAPTCHA_SESSION_KEY
kaptcha.session.date session date KAPTCHA_SESSION_DATE
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/150387.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • java集合系列——List集合之Stack介绍(五)「建议收藏」

    Stack的简介Stack 类表示后进先出(LIFO)的对象堆栈。它通过五个操作对类 Vector 进行了扩展 ,允许将向量视为堆栈。它提供了通常的 push 和 pop 操作,以及取堆栈顶点的 peek 方法、测试堆栈是否为空的 empty 方法、在堆栈中查找项并确定到堆栈顶距离的 search 方法。

    2022年2月26日
    42
  • Android 官方文档:(一)动画和图像 —— 1.5 画布和画图

    Android 官方文档:(一)动画和图像 —— 1.5 画布和画图

    2021年11月13日
    42
  • bs和cs开发架构的详细解析区别_BS与CS架构区别

    bs和cs开发架构的详细解析区别_BS与CS架构区别目前开发的基本架构。一、CS结构—-Client/Server1、开发者需要编写两个端点,一个是客户端程序,一个是服务端程序。举例:QQ、360等等。2、需要再客户机安装客户端的部分。3、弊端:客户端的维护比较麻烦,机器只要一重装,就需要重新安装该软件,同时升级也比较麻烦。后期有了一个解决方案:对于升级,可以通过网络升级的形式完成。4、好处:客户端的出现,可以

    2022年10月16日
    0
  • java整型转换成字符串_java整型转换成字符串

    java整型转换成字符串_java整型转换成字符串二、实验要求1、编写一个Java程序,在程序中进行字符串与数值型数据的转换。2、编写一个Java程序,在程序中通过键盘输入常用的数据,包括字符串、整数和……Strings=”10″;//字符串转换成数值型a=Byte.parseByte(s);b=Short.parseShort(s);//调用Short类的parseShort方法把s转换成短整型c…

    2022年10月19日
    0
  • 两个求和符号相乘_excel输入次方符号

    两个求和符号相乘_excel输入次方符号在机器学习中,经常会遇到有含有两个求和符号的公式,如,∑i=1M∑j=1N\sum^M_{i=1}\sum^N_{j=1}∑i=1M​∑j=1N​,一开始,我总是不能够理解这是一种怎样的运算,后来看到下面的解释觉得自己顿悟:有两个∑\sum∑的时候就有两个变量,是一个不变的情况下另一个从头到尾改变,然后之前那个再变一下,第二个再从头到尾变,一直到第一个变量变到最后,把这个过程中的项加起来!…

    2022年10月12日
    0
  • 如何用 python gzip解压?[通俗易懂]

    如何用 python gzip解压?[通俗易懂]#createadecompressgzipfilefunctionimportgzipimportosdefun_gzip(gzip_file):f_name=gzip_file.split(‘.’)[0]withgzip.open(gzip_file,’rb’)asf_in:withopen(f_name,’wb’)asf_out:f_out.writelines(f_in)

    2022年9月6日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号