SpringBoot项目整合生成验证码(kaptcha)功能。

前言

一、kaptcha简介

验证码(CAPTCHA)是“Completely Automated Public Turing test to tell Computers and Humans Apart”(全自动区分计算机和人类的图灵测试)的缩写,是一种区分用户是计算机还是人的公共全自动程序。
这里介绍的是github上的一个开源项目kaptcha - A kaptcha generation engine.
原项目地址点此跳转!!!
当然你也可以用别的,方法和原理都大同小异。

二、使用步骤

1.引入kaptcha依赖

<!-- https://mvnrepository.com/artifact/com.github.penggle/kaptcha -->
<dependency>
    <groupId>com.github.penggle</groupId>
    <artifactId>kaptcha</artifactId>
    <version>2.3.2</version>
</dependency>

2.编写生成验证码的配置类

编写kaptcha 的相关配置,创建 bean 并注册,springboot 有两种注册方式,xml 配置和java 代码注解配置,这里采用 java 代码注解配置。

@Configuration
public class KaptchaConfig {
   

    @Bean
    public Producer kaptchaProducer() {
   
        Properties properties = new Properties();
        //图片宽
        properties.setProperty("kaptcha.image.width", "100");
        //图片高
        properties.setProperty("kaptcha.image.height", "40");
        //字体大小
        properties.setProperty("kaptcha.textproducer.font.size", "32");
        //字体颜色
        properties.setProperty("kaptcha.textproducer.font.color", "0,0,0");
        //文本集合,验证码值从此集合中获取 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYAZ" 表示从这些字符里选
        properties.setProperty("kaptcha.textproducer.char.string", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYAZ");
        //验证码长度
        properties.setProperty("kaptcha.textproducer.char.length", "4");
        //干扰实现类
        properties.setProperty("kaptcha.noise.impl"
  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值