使用Kaptcha进行验证码的生成和验证

来源:互联网 发布:windows桌面文件丢失 编辑:程序博客网 时间:2024/05/19 07:08

在开始工作之前我们需要进行下载Kaptcha的jar包。

验证码:

我们在登陆和注册时都可以使用。下面我们来实现一下使用Kaptcha 包进行验证码的生成。

首先我们创建一个web工程。将jar包导入到WebRoot-->WEB_INF-->lib中,然后右键进行构建路径。

之后打开web.xml。

web.xml:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  <display-name>KaptchaTest</display-name>  <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.htm</welcome-file>    <welcome-file>index.jsp</welcome-file>    <welcome-file>default.html</welcome-file>    <welcome-file>default.htm</welcome-file>    <welcome-file>default.jsp</welcome-file>  </welcome-file-list>    <!--  <servlet>        <servlet-name>Kaptcha</servlet-name>        <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>    </servlet>    -->    <!-- 登陆验证码Kaptcha 2--><servlet><servlet-name>Kaptcha</servlet-name><servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class><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.google.code.kaptcha.text.impl.DefaultTextCreator</param-value></init-param><init-param><description>文本集合,验证码值从此集合中获取</description><param-name>kaptcha.textproducer.char.string</param-name><param-value>ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789</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 kaptcha is generated is put into theHttpSession. This is the key value for that item in thesession.</description><param-name>kaptcha.session.date</param-name><param-value>KAPTCHA_SESSION_DATE</param-value></init-param></servlet>    <servlet-mapping>        <servlet-name>Kaptcha</servlet-name>        <url-pattern>/randomcode.jpg</url-pattern>    </servlet-mapping></web-app>

然后使用一个界面来获取图片。

<body>  <form action="check.jsp">    <img src='randomcode.jpg'><input type="text" name="input">    <br>    <input type="submit" value="提交">   </form>  </body>

最后我们需要一个检验界面进行检验我们的验证码是否输入正确

check.jsp

<body>    <%    String k=(String)session.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);    String input=request.getParameter("input");    input=input.toUpperCase();    if(input.equals(k))    {    out.println("true");    }    else{    out.println("flase");    }     %>  </body>

这样我们尽可以进行验证码的设计和验证了

0 0
原创粉丝点击