kaptcha验证码
发布日期:2021-06-30 16:29:33 浏览次数:3 分类:技术文章

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

kaptcha验证码

kaptcha 是一个很有用的验证码生成工具。有了它,你能够生成各种样式的验证码,由于它是可配置的。
kaptcha工作的原理是调用 com.google.code.kaptcha.servlet.KaptchaServlet,生成一个图片。
同时将生成的验证码字符串放到 HttpSession中。
使用kaptcha能够方便的配置:
验证码的字体
验证码字体的大小
验证码字体的字体颜色
验证码内容的范围(数字,字母,中文汉字!)
验证码图片的大小。边框,边框粗细,边框颜色
验证码的干扰线(能够自己继承com.google.code.kaptcha.NoiseProducer写一个自己定义的干扰线)
验证码的样式(鱼眼样式、3D、普通模糊……当然也能够继承com.google.code.kaptcha.GimpyEngine自己定义样式)

使用方法

1.导入jar包
kaptcha-2.3.jar   下载地址  :   http://www.java2s.com/Code/Jar/k/Downloadkaptcha23jar.htm
2.配置web.xml添加servlet
Kaptcha
com.google.code.kaptcha.servlet.KaptchaServlet
Kaptcha
/kaptcha.jpg
3.在jsp页面中
     src="kaptcha.jpg"会被定位到servlet上
4.KaptchaServlet会把验证码设置到session中。
String kaptchaExpected = (String)request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
5.假设是struts2的action以下方式获取
String kaptchaExpected = (String)ActionContext.getContext().getSession().get(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
6.点击图片更换验证码
7.SpringMVC中的使用
//java@Controller@RequestMapping("/Captcha")public class CaptchaRandomCodeController {	private Producer captchaProducer = null;	@Autowired	public void setCaptchaProducer(Producer captchaProducer) {		this.captchaProducer = captchaProducer;	}	@RequestMapping("/captcha-image")	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {		response.setDateHeader("Expires", 0);// 禁止server端缓存		// 设置标准的 HTTP/1.1 no-cache headers.		response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");		// 设置IE扩展 HTTP/1.1 no-cache headers (use addHeader).		response.addHeader("Cache-Control", "post-check=0, pre-check=0");		response.setHeader("Pragma", "no-cache");// 设置标准 HTTP/1.0 不缓存图片		response.setContentType("image/jpeg");// 返回一个 jpeg 图片,默认是text/html(输出文档的MIMI类型)		String capText = captchaProducer.createText();// 为图片创建文本				// 将文本保存在session中。这里就使用包中的静态变量吧		request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);		BufferedImage bi = captchaProducer.createImage(capText); // 创建带有文本的图片		ServletOutputStream out = response.getOutputStream();		// 图片数据输出		ImageIO.write(bi, "jpg", out);		try {			out.flush();		} finally {			out.close();		}		System.out.println("Session 验证码是:" + request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY));		return null;	}}
yes
105,179,90
160
50
code
red
35
3
5
彩云,宋体,楷体,微软雅黑
验证码:
8.验证码参数详细
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://kevin.blog.csdn.net/article/details/79239030 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:区块链是什么?如何简单易懂地介绍区块链?
下一篇:vue.js的键盘事件

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年05月02日 11时51分58秒