博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
kaptcha 简单方便的验证码生成工具
阅读量:3564 次
发布时间:2019-05-20

本文共 5203 字,大约阅读时间需要 17 分钟。

kaptcha是一个非常实用的验证码生成工具,有了它,你可以生成各种样式的验证码,因为它是可配置的。

kaptcha工作的原理是调用com.google.code.kaptcha.servlet.KaptchaServlet,生成一个图片。同时将生成的验证码字符串放到HttpSession中。

kaptcha可以配置一下信息:

验证码的字体

验证码字体的大小

验证码字体的字体颜色

验证码内容的范围(数字,字母,中文汉字!)

验证码图片的大小,边框,边框粗细,边框颜色

验证码的干扰线(可以自己继承com.google.code.kaptcha.NoiseProducer写一个自定义的干扰线)

验证码的样式(鱼眼样式、3D、普通模糊……当然也可以继承com.google.code.kaptcha.GimpyEngine自定义样式)

……

详细信息请看下面的web.xml文件

下面介绍一下用法:

1.首先去官网下载jar:

2.建立一个web项目,导入kaptcha-2.3.jar到环境变量中。

3.配置web.xml文件

其实就是配置com.google.code.kaptcha.servlet.KaptchaServlet

 

Kaptcha
com.google.code.kaptcha.servlet.KaptchaServlet
Border around kaptcha. Legal values are yes or no.
kaptcha.border
no
Color of the border. Legal values are r,g,b (and optional alpha) or white,black,blue.
kaptcha.border.color
red
Thickness of the border around kaptcha. Legal values are > 0.
kaptcha.border.thickness
5
Width in pixels of the kaptcha image.
kaptcha.image.width
80
Height in pixels of the kaptcha image.
kaptcha.image.height
40
The image producer.
kaptcha.producer.impl
com.google.code.kaptcha.impl.DefaultKaptcha
The text producer.
kaptcha.textproducer.impl
com.google.code.kaptcha.text.impl.DefaultTextCreator
The characters that will create the kaptcha.
kaptcha.textproducer.char.string
abcde2345678gfynmnpwx
The number of characters to display.
kaptcha.textproducer.char.length
5
A list of comma separated font names.
kaptcha.textproducer.font.names
Arial, Courier
The size of the font to use.
kaptcha.textproducer.font.size
23
The color to use for the font. Legal values are r,g,b.
kaptcha.textproducer.font.color
black
The noise producer.
kaptcha.noise.impl
com.google.code.kaptcha.impl.NoNoise
The noise color. Legal values are r,g,b.
kaptcha.noise.color
black
The obscurificator implementation.
kaptcha.obscurificator.impl
com.google.code.kaptcha.impl.ShadowGimpy
The background implementation.
kaptcha.background.impl
com.google.code.kaptcha.impl.DefaultBackground
Ending background color. Legal values are r,g,b.
kaptcha.background.clear.to
white
The word renderer implementation.
kaptcha.word.impl
com.google.code.kaptcha.text.impl.DefaultWordRenderer
The value for the kaptcha is generated and is put into the HttpSession. This is the key value for that item in the session.
kaptcha.session.key
KAPTCHA_SESSION_KEY
The date the kaptcha is generated is put into the HttpSession. This is the key value for that item in the session.
kaptcha.session.date
KAPTCHA_SESSION_DATE
Kaptcha
/Kaptcha.jpg
KaptchaExample.jsp
 

4.编写KaptchaExample.jsp

这里用到了jQuery,所以添加了jQuery的库

			<%@ page language="java" contentType="text/html; charset=UTF-8"%>		
Kaptcha Example
Enter in the Kaptcha to see if it matches what is stored in the session attributes.
单击换图片
验证码::
<% String c = (String) session .getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY); String parm = (String) request.getParameter("kaptchafield"); System.out.println(c); out.println("Parameter: " + parm + " ? Session Key: " + c + " : "); if (c != null && parm != null) { if (c.equals(parm)) { out.println("true"); } else { out.println("false"); } } %>
 

5.运行测试

 

工程下载:

转载地址:http://pthrj.baihongyu.com/

你可能感兴趣的文章
【IC1】【转 非常好】运算放大器使用的六个经验
查看>>
【IC-ADC 3】ADC的选型
查看>>
2019年03月18日 查看数据手册的注意点,极限参数、电气参数、推荐参数
查看>>
HiKey960/970用户手册;HiKey960 Development Board User Manual
查看>>
【书籍推荐】FPGA,xilinx
查看>>
N9-SQL注入(union注入)
查看>>
N10-sql注入(information_schema注入)
查看>>
N1-Kali虚拟机中SQLmap
查看>>
N11-sql注入(http头注入)
查看>>
N2-sqlmap初使用
查看>>
N12-sql盲注原理以及boolean盲注案例实现
查看>>
N13-sqli盲注 基于时间型
查看>>
N1 技术心得 2019-6-26
查看>>
N1-环境配置
查看>>
N2-审计方法与步骤
查看>>
N3-常见的INI配置
查看>>
代码审计 N4 常见危险函数和特殊函数(一)
查看>>
MySQL笔记
查看>>
计算机运算方法之(原码 补码 反码 移码)
查看>>
计算机组成原理之(二进制与十进制互相转换,数的定点表示与浮点数表示)例题:设浮点数字长16位,其中阶码5位(含有1位阶符),尾数11位(含有1位数符)
查看>>