Kaptcha验证码生成

2024-04-11 03:08
文章标签 生成 验证码 kaptcha

本文主要是介绍Kaptcha验证码生成,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

集成Spring框架

1.配置bean

<beanname="producer"class="com.google.code.kaptcha.impl.DefaultKaptcha"scope="singleton">

    <property name="config">

        <beanclass="com.google.code.kaptcha.util.Config">

            <constructor-arg>

                <props>

                    <propkey="kaptcha.border">no</prop>

                    <propkey="kaptcha.textproducer.font.color">black</prop>

                    <propkey="kaptcha.textproducer.char.space">5</prop>

                </props>

            </constructor-arg>

        </bean>

    </property>

</bean>

 

生成验证码

 

2.注入Producer

@Autowired

private Producer producer;

 

 

//1.设置响应格式

response.setHeader("Cache-Control","no-store,no-cache");

response.setContentType("image/jpeg");

//2.随机生成验证码文字

Stringtext=producer.createText();

//3.创建一个缓存图片

BufferedImageimage=producer.createImage(text);

//4.输出图片

OutputStreamout=response.getOutputStream();

ImageIO.write(image,"jpg",out);

 

属性配置

 <init-param>

                     <description>图片边框,合法值:yes , no</description>

                     <param-name>kaptcha.border</param-name>

                     <param-value>yes</param-value>

              </init-param>

              <init-param>

                     <description>

                           边框颜色,合法值: r,g,b (and optional alpha) 或者

                           white,black,blue.

                     </description>

                     <param-name>kaptcha.border.color</param-name>

                     <param-value>black</param-value>

              </init-param>

              <init-param>

                     <description>边框厚度,合法值:>0</description>

                     <param-name>kaptcha.border.thickness</param-name>

                     <param-value>1</param-value>

              </init-param>

              <init-param>

                     <description>图片宽 200</description>

                     <param-name>kaptcha.image.width</param-name>

                     <param-value>200</param-value>

              </init-param>

              <init-param>

                     <description>图片高 50</description>

                     <param-name>kaptcha.image.height</param-name>

                     <param-value>50</param-value>

              </init-param>

              <init-param>

                     <description>图片实现类</description>

                     <param-name>kaptcha.producer.impl</param-name>

                     <param-value>

                           com.google.code.kaptcha.impl.DefaultKaptcha

                     </param-value>

              </init-param>

              <init-param>

                     <description>文本实现类</description>

                     <param-name>kaptcha.textproducer.impl</param-name>

                     <param-value>

                           <!-- 自己实现的中文实现类

                           com.th.mykaptcha.ChineseText

                            -->

                           <!-- 默认的实现类 -->

                           com.google.code.kaptcha.text.impl.DefaultTextCreator

                     </param-value>

              </init-param>

              <init-param>

                   <description>文本集合,验证码值从此集合中获取</description>

                   <param-name>kaptcha.textproducer.char.string</param-name>

                     <param-value>1234567890</param-value>

                     <!--<param-value>abcde2345678gfynmnpwx</param-value>-->

                  <!--<param-value>慕课网教程验证码实例</param-value> -->

              </init-param>

              <init-param>

                     <description>验证码长度 5</description>

                     <param-name>kaptcha.textproducer.char.length</param-name>

                     <param-value>4</param-value>

              </init-param>

              <init-param>

                     <description>字体Arial, Courier</description>

                     <param-name>kaptcha.textproducer.font.names</param-name>

                     <param-value>Arial, Courier</param-value>

              </init-param>

              <init-param>

                     <description>字体大小 40px.</description>

                     <param-name>kaptcha.textproducer.font.size</param-name>

                     <param-value>40</param-value>

              </init-param>

              <init-param>

                     <description>

                           字体颜色,合法值: r,g,b 或者white,black,blue.

                     </description>

                   <param-name>kaptcha.textproducer.font.color</param-name>

                     <param-value>black</param-value>

              </init-param>

              <init-param>

                     <description>文字间隔 2</description>

                   <param-name>kaptcha.textproducer.char.space</param-name>

                     <param-value>2</param-value>

              </init-param>

              <init-param>

                     <description>干扰实现类</description>

                     <param-name>kaptcha.noise.impl</param-name>

                     <param-value>

                           <!-- com.google.code.kaptcha.impl.NoNoise -->

                           com.google.code.kaptcha.impl.DefaultNoise

                     </param-value>

              </init-param>

              <init-param>

                     <description>

                           干扰颜色,合法值: r,g,b 或者white,black,blue.

                     </description>

                     <param-name>kaptcha.noise.color</param-name>

                     <param-value>black</param-value>

              </init-param>

              <init-param>

                     <description>

                           图片样式: 水纹com.google.code.kaptcha.impl.WaterRipple

                           鱼眼com.google.code.kaptcha.impl.FishEyeGimpy

                           阴影com.google.code.kaptcha.impl.ShadowGimpy

                     </description>

                     <param-name>kaptcha.obscurificator.impl</param-name>

                     <param-value>

                           com.google.code.kaptcha.impl.WaterRipple

                     </param-value>

              </init-param>

              <init-param>

                     <description>背景实现类</description>

                     <param-name>kaptcha.background.impl</param-name>

                     <param-value>

                           com.google.code.kaptcha.impl.DefaultBackground

                     </param-value>

              </init-param>

              <init-param>

                     <description>背景颜色渐变,开始颜色</description>

                     <param-name>kaptcha.background.clear.from</param-name>

                     <param-value>green</param-value>

              </init-param>

              <init-param>

                     <description>背景颜色渐变,结束颜色</description>

                     <param-name>kaptcha.background.clear.to</param-name>

                     <param-value>white</param-value>

              </init-param>

              <init-param>

                     <description>文字渲染器</description>

                     <param-name>kaptcha.word.impl</param-name>

                     <param-value>

                           com.google.code.kaptcha.text.impl.DefaultWordRenderer

                     </param-value>

              </init-param>

              <init-param>

                     <description>

                           session中存放验证码的key键

                     </description>

                     <param-name>kaptcha.session.key</param-name>

                     <param-value>KAPTCHA_SESSION_KEY</param-value>

              </init-param>

              <init-param>

                     <description>

                           The date the kaptchais generated is put intothe

                           HttpSession. This isthe key value for that item in the

                           session.

                     </description>

                     <param-name>kaptcha.session.date</param-name>

                     <param-value>KAPTCHA_SESSION_DATE</param-value>

              </init-param>

 

这篇关于Kaptcha验证码生成的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/892915

相关文章

Python从Word文档中提取图片并生成PPT的操作代码

《Python从Word文档中提取图片并生成PPT的操作代码》在日常办公场景中,我们经常需要从Word文档中提取图片,并将这些图片整理到PowerPoint幻灯片中,手动完成这一任务既耗时又容易出错,... 目录引言背景与需求解决方案概述代码解析代码核心逻辑说明总结引言在日常办公场景中,我们经常需要从 W

C#使用Spire.XLS快速生成多表格Excel文件

《C#使用Spire.XLS快速生成多表格Excel文件》在日常开发中,我们经常需要将业务数据导出为结构清晰的Excel文件,本文将手把手教你使用Spire.XLS这个强大的.NET组件,只需几行C#... 目录一、Spire.XLS核心优势清单1.1 性能碾压:从3秒到0.5秒的质变1.2 批量操作的优雅

Python使用python-pptx自动化操作和生成PPT

《Python使用python-pptx自动化操作和生成PPT》这篇文章主要为大家详细介绍了如何使用python-pptx库实现PPT自动化,并提供实用的代码示例和应用场景,感兴趣的小伙伴可以跟随小编... 目录使用python-pptx操作PPT文档安装python-pptx基础概念创建新的PPT文档查看

在ASP.NET项目中如何使用C#生成二维码

《在ASP.NET项目中如何使用C#生成二维码》二维码(QRCode)已广泛应用于网址分享,支付链接等场景,本文将以ASP.NET为示例,演示如何实现输入文本/URL,生成二维码,在线显示与下载的完整... 目录创建前端页面(Index.cshtml)后端二维码生成逻辑(Index.cshtml.cs)总结

Python实现数据可视化图表生成(适合新手入门)

《Python实现数据可视化图表生成(适合新手入门)》在数据科学和数据分析的新时代,高效、直观的数据可视化工具显得尤为重要,下面:本文主要介绍Python实现数据可视化图表生成的相关资料,文中通过... 目录前言为什么需要数据可视化准备工作基本图表绘制折线图柱状图散点图使用Seaborn创建高级图表箱线图热

SQLServer中生成雪花ID(Snowflake ID)的实现方法

《SQLServer中生成雪花ID(SnowflakeID)的实现方法》:本文主要介绍在SQLServer中生成雪花ID(SnowflakeID)的实现方法,文中通过示例代码介绍的非常详细,... 目录前言认识雪花ID雪花ID的核心特点雪花ID的结构(64位)雪花ID的优势雪花ID的局限性雪花ID的应用场景

Django HTTPResponse响应体中返回openpyxl生成的文件过程

《DjangoHTTPResponse响应体中返回openpyxl生成的文件过程》Django返回文件流时需通过Content-Disposition头指定编码后的文件名,使用openpyxl的sa... 目录Django返回文件流时使用指定文件名Django HTTPResponse响应体中返回openp

Django开发时如何避免频繁发送短信验证码(python图文代码)

《Django开发时如何避免频繁发送短信验证码(python图文代码)》Django开发时,为防止频繁发送验证码,后端需用Redis限制请求频率,结合管道技术提升效率,通过生产者消费者模式解耦业务逻辑... 目录避免频繁发送 验证码1. www.chinasem.cn避免频繁发送 验证码逻辑分析2. 避免频繁

python生成随机唯一id的几种实现方法

《python生成随机唯一id的几种实现方法》在Python中生成随机唯一ID有多种方法,根据不同的需求场景可以选择最适合的方案,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习... 目录方法 1:使用 UUID 模块(推荐)方法 2:使用 Secrets 模块(安全敏感场景)方法

Python实现自动化Word文档样式复制与内容生成

《Python实现自动化Word文档样式复制与内容生成》在办公自动化领域,高效处理Word文档的样式和内容复制是一个常见需求,本文将展示如何利用Python的python-docx库实现... 目录一、为什么需要自动化 Word 文档处理二、核心功能实现:样式与表格的深度复制1. 表格复制(含样式与内容)2