微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用开源工具制作网页验证码的方法

开发工具:eclipse、kaptcha-2.3.jar包

一、创建Web项目;

二、新建一个Jsp页面内容有,一个文本框,一个图片容器,一个提交按钮)

rush:js;"> random

三、可以看出图片验证码来源(src=“randomcode.jpg”)需配置Web.xml文件。(交给Servlet(该servlet在kaptcha-2.3.jar)处理)

rush:js;"> Kaptcha com.google.code.kaptcha.servlet.KaptchaServlet Kaptcha /randomcode.jpg

四、由于需要kaptcha-2.3.jar包,所以将下载好的jar包导入在lib中。(复制黏贴即可)

其他:

一、网页验证码的属性

(一)添加边框

rush:js;"> 图片边框,合法值:yes,no kaptcha.border yes

(二)边框颜色

rush:js;"> 边框颜色,合法值: r,g,b (and optional alpha) 或者white,black,blue. kaptcha.border.color black

(三)边框厚度

rush:js;"> 边框厚度,合法值:>大于0 kaptcha.border.thickness 1

(四)图片宽度

rush:js;"> 图片宽 200 kaptcha.image.width 200

(五)图片高度

rush:js;"> 图片高 50 kaptcha.image.height 50

(六)验证码集合

rush:js;"> 文本集合,验证码值从此集合中获取 kaptcha.textproducer.char.string 1234567890 //abcde2345678gfynmnpwx

(七)验证码长度

rush:js;"> 验证码长度 认是5 kaptcha.textproducer.char.length 2

(八)字体

rush:js;"> 字体 Arial,Courier kaptcha.textproducer.font.names Arial,Courier

(九)字体大小

rush:js;"> 字体大小 40px. kaptcha.textproducer.font.size 40

(十)字体颜色

rush:js;"> 字体颜色,合法值: r,b 或者 white,blue. kaptcha.textproducer.font.color black

(十一)每个验证码之间的间隔

rush:js;"> 文字间隔 2 kaptcha.textproducer.char.space 2

(十二)干扰实现

rush:js;"> 干扰实现类 kaptcha.noise.impl com.google.code.kaptcha.impl.DefaultNoise

(十三)干扰颜色

rush:js;"> 干扰颜色,合法值: r,blue. kaptcha.noise.color black

(十四)背景样式

rush:js;"> 图片样式: 水纹com.google.code.kaptcha.impl.WaterRipple 鱼眼com.google.code.kaptcha.impl.FisheyeGimpy 阴影com.google.code.kaptcha.impl.ShadowGimpy kaptcha.obscurificator.impl com.google.code.kaptcha.impl.WaterRipple

(十五)背景实现类

rush:js;"> 背景实现类 kaptcha.background.impl com.google.code.kaptcha.impl.DefaultBackground

(十六)背景渐变颜色

rush:js;"> 背景颜色渐变,开始颜色 kaptcha.background.clear.from green 背景颜色渐变,结束颜色 kaptcha.background.clear.to white

(十七)文字渲染器

rush:js;"> 文字渲染器 kaptcha.word.impl com.google.code.kaptcha.text.impl.DefaultWordRenderer

(十八)图片的验证码会保存在Session中,其中的值为

rush:js;"> session中存放验证码的key键 kaptcha.session.key KAPTCHA_SESSION_KEY

(十九)图片实现类别

rush:js;"> 图片实现类 kaptcha.producer.impl com.google.code.kaptcha.impl.DefaultKaptcha

(二十)文本实现类(可通过重写该类来实现验证码为中文

rush:js;"> 文本实现类 kaptcha.textproducer.impl com.google.code.kaptcha.text.impl.DefaultTextCreator

重写文本实现类,实现验证码为中文

1.创建一个类别,继承Configurable 实现TextProducer(在jar包中)

rush:js;"> import com.google.code.kaptcha.text.TextProducer; import com.google.code.kaptcha.util.Configurable; import java.util.Random; public class ChineseText extends Configurable implements TextProducer { public String getText() { int length = getConfig().getTextProducerCharLength(); String finalWord = "",firstWord = ""; int tempInt = 0; String[] array = { "0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f" }; Random rand = new Random(); for (int i = 0; i < length; i++) { switch (rand.nextInt(array.length)) { case 1: tempInt = rand.nextInt(26) + 65; firstWord = String.valueOf((char) tempInt); break; case 2: int r1,r2,r3,r4; String strH,strL;// high&low r1 = rand.nextInt(3) + 11; // 前闭后开[11,14) if (r1 == 13) { r2 = rand.nextInt(7); } else { r2 = rand.nextInt(16); } r3 = rand.nextInt(6) + 10; if (r3 == 10) { r4 = rand.nextInt(15) + 1; } else if (r3 == 15) { r4 = rand.nextInt(15); } else { r4 = rand.nextInt(16); } strH = array[r1] + array[r2]; strL = array[r3] + array[r4]; byte[] bytes = new byte[2]; bytes[0] = (byte) (Integer.parseInt(strH,16)); bytes[1] = (byte) (Integer.parseInt(strL,16)); firstWord = new String(bytes); break; default: tempInt = rand.nextInt(10) + 48; firstWord = String.valueOf((char) tempInt); break; } finalWord += firstWord; } return finalWord; } }

2.修改Web.xml配置

rush:js;"> 文本实现类 kaptcha.textproducer.impl ChineseText 五、验证码的校验(本文是利用check.jsp来校验的)保存在Session中,其中的键值为(第十八个属性) [html] view plain copy <% // 检查是否是正确的验证码 String k = (String) session.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY); String str = request.getParameter("r"); if (k.equals(str)) out.print("true"); out.print(k + "---" + str); %>

六、扩展(加法验证码的实现)

1.重写KaptchaServlet类

rush:js;"> import com.google.code.kaptcha.Producer; import com.google.code.kaptcha.util.Config; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.Enumeration; import java.util.Properties; import javax.imageio.ImageIO; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.servletexception; import javax.servlet.ServletoutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class KaptchaServlet extends HttpServlet implements Servlet { private Properties props; private Producer kaptchaProducer; private String sessionkeyvalue; public KaptchaServlet() { this.props = new Properties(); this.kaptchaProducer = null; this.sessionkeyvalue = null; } public void init(ServletConfig conf) throws servletexception { super.init(conf); ImageIO.setUseCache(false); Enumeration initParams = conf.getinitParameterNames(); while (initParams.hasMoreElements()) { String key = (String) initParams.nextElement(); String value = conf.getinitParameter(key); this.props.put(key,value); } Config config = new Config(this.props); this.kaptchaProducer = config.getProducerImpl(); this.sessionkeyvalue = config.getSessionKey(); } public void doGet(HttpServletRequest req,HttpServletResponse resp) throws servletexception,IOException { resp.setDateHeader("Expires",0L); resp.setHeader("Cache-Control","no-store,no-cache,must-revalidate"); resp.addHeader("Cache-Control","post-check=0,pre-check=0"); resp.setHeader("Pragma","no-cache"); resp.setContentType("image/jpeg"); String capText = this.kaptchaProducer.createText(); String s1 = capText.substring(0,1); String s2 = capText.substring(1,2); int r = Integer.valueOf(s1).intValue() + Integer.valueOf(s2).intValue(); req.getSession().setAttribute(this.sessionkeyvalue,String.valueOf(r)); BufferedImage bi = this.kaptchaProducer.createImage(s1+"+"+s2+"=?"); ServletoutputStream out = resp.getoutputStream(); ImageIO.write(bi,"jpg",out); try { out.flush(); } finally { out.close(); } } }

2.修改配置文件

rush:js;"> Kaptcha KaptchaServlet

以上所述是小编给大家介绍的使用开源工具制作网页验证码的方法。编程之家 jb51.cc 收集整理的教程希望能对你有所帮助,如果觉得编程之家不错,可分享给好友!感谢支持

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐