zoukankan      html  css  js  c++  java
  • 验证码

    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:http://code.google.com/p/kaptcha/

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

    3.配置web.xml文件

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

      1 <?xml version="1.0" encoding="UTF-8"?>  
      2 <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
      3     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"  
      4     version="2.4">  
      5     <servlet>  
      6         <servlet-name>Kaptcha</servlet-name>  
      7         <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>  
      8         <init-param>  
      9             <description> Border around kaptcha. Legal values are yes or no. </description>  
     10             <param-name>kaptcha.border</param-name>  
     11             <param-value>no</param-value>  
     12         </init-param>  
     13         <init-param>  
     14             <description>Color of the border. Legal values are r,g,b (and optional alpha) or white,black,blue. </description>  
     15             <param-name>kaptcha.border.color</param-name>  
     16             <param-value>red</param-value>  
     17         </init-param>  
     18         <init-param>  
     19             <description>Thickness of the border around kaptcha. Legal values are > 0. </description>  
     20             <param-name>kaptcha.border.thickness</param-name>  
     21             <param-value>5</param-value>  
     22         </init-param>  
     23         <init-param>  
     24             <description>Width in pixels of the kaptcha image. </description>  
     25             <param-name>kaptcha.image.width</param-name>  
     26             <param-value>80</param-value>  
     27         </init-param>  
     28         <init-param>  
     29             <description>Height in pixels of the kaptcha image. </description>  
     30             <param-name>kaptcha.image.height</param-name>  
     31             <param-value>40</param-value>  
     32         </init-param>  
     33         <init-param>  
     34             <description>The image producer. </description>  
     35             <param-name>kaptcha.producer.impl</param-name>  
     36             <param-value>com.google.code.kaptcha.impl.DefaultKaptcha </param-value>  
     37         </init-param>  
     38         <init-param>  
     39             <description>The text producer. </description>  
     40             <param-name>kaptcha.textproducer.impl</param-name>  
     41             <param-value>com.google.code.kaptcha.text.impl.DefaultTextCreator</param-value>  
     42         </init-param>  
     43         <init-param>  
     44             <description>The characters that will create the kaptcha. </description>  
     45             <param-name>kaptcha.textproducer.char.string</param-name>  
     46             <param-value>abcde2345678gfynmnpwx </param-value>  
     47         </init-param>  
     48         <init-param>  
     49             <description>The number of characters to display. </description>  
     50             <param-name>kaptcha.textproducer.char.length</param-name>  
     51             <param-value>5</param-value>  
     52         </init-param>  
     53         <init-param>  
     54             <description>A list of comma separated font names.</description>  
     55             <param-name>kaptcha.textproducer.font.names</param-name>  
     56             <param-value>Arial, Courier</param-value>  
     57         </init-param>  
     58         <init-param>  
     59             <description>The size of the font to use. </description>  
     60             <param-name>kaptcha.textproducer.font.size</param-name>  
     61             <param-value>23</param-value>  
     62         </init-param>  
     63         <init-param>  
     64             <description>The color to use for the font. Legal values are r,g,b. </description>  
     65             <param-name>kaptcha.textproducer.font.color</param-name>  
     66             <param-value>black</param-value>  
     67         </init-param>  
     68         <init-param>  
     69             <description>The noise producer. </description>  
     70             <param-name>kaptcha.noise.impl</param-name>  
     71             <param-value>com.google.code.kaptcha.impl.NoNoise </param-value>  
     72         </init-param>  
     73         <init-param>  
     74             <description>The noise color. Legal values are r,g,b. </description>  
     75             <param-name>kaptcha.noise.color</param-name>  
     76             <param-value>black</param-value>  
     77         </init-param>  
     78         <init-param>  
     79             <description>The obscurificator implementation. </description>  
     80             <param-name>kaptcha.obscurificator.impl</param-name>  
     81             <param-value>com.google.code.kaptcha.impl.ShadowGimpy</param-value>  
     82         </init-param>  
     83         <init-param>  
     84             <description>The background implementation. </description>  
     85             <param-name>kaptcha.background.impl</param-name>  
     86             <param-value>com.google.code.kaptcha.impl.DefaultBackground</param-value>  
     87         </init-param>  
     88         <init-param>  
     89             <description>Ending background color. Legal values are r,g,b. </description>  
     90             <param-name>kaptcha.background.clear.to</param-name>  
     91             <param-value>white</param-value>  
     92         </init-param>  
     93         <init-param>  
     94             <description>The word renderer implementation. </description>  
     95             <param-name>kaptcha.word.impl</param-name>  
     96             <param-value>com.google.code.kaptcha.text.impl.DefaultWordRenderer</param-value>  
     97         </init-param>  
     98         <init-param>  
     99             <description>The value for the kaptcha is generated and is put into the HttpSession. This is the key value for that item in the session. </description>  
    100             <param-name>kaptcha.session.key</param-name>  
    101             <param-value>KAPTCHA_SESSION_KEY</param-value>  
    102         </init-param>  
    103         <init-param>  
    104             <description>The date the kaptcha is generated is put into the HttpSession. This is the key value for that item in the session. </description>  
    105             <param-name>kaptcha.session.date</param-name>  
    106             <param-value>KAPTCHA_SESSION_DATE</param-value>  
    107         </init-param>  
    108     </servlet>  
    109     <servlet-mapping>  
    110         <servlet-name>Kaptcha</servlet-name>  
    111         <url-pattern>/Kaptcha.jpg</url-pattern>  
    112     </servlet-mapping>  
    113     <welcome-file-list>  
    114         <welcome-file>KaptchaExample.jsp</welcome-file>  
    115     </welcome-file-list>  
    116 </web-app>  
    View Code

    4.编写KaptchaExample.jsp

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

     1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
     2 <html>  
     3     <head>  
     4         <%@ page language="java" contentType="text/html; charset=UTF-8"%>  
     5         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
     6         <title>Kaptcha Example</title>  
     7         <mce:script type="text/javascript" src="js/jquery-1.3.2.js" mce_src="js/jquery-1.3.2.js"></mce:script>  
     8     </head>  
     9     <body>  
    10         Enter in the  
    11         <a href="http://code.google.com/p/kaptcha/" mce_href="http://code.google.com/p/kaptcha/">Kaptcha</a> to see if it  
    12         matches what is stored in the session attributes.  
    13         <table>  
    14             <tr>  
    15                 <td>  
    16                     <img src="Kaptcha.jpg" mce_src="Kaptcha.jpg" id="kaptchaImage" />  
    17                     <mce:script type="text/javascript"><!--  
    18 $('#kaptchaImage').click(  
    19         function() {  
    20             $(this).hide().attr('src',  
    21                     'Kaptcha.jpg?' + Math.floor(Math.random() * 100)).fadeIn();  
    22         })  
    23 // --></mce:script>  
    24                     <br />  
    25                     单击换图片  
    26                 </td>  
    27                 <td valign="top">  
    28                     <form method="POST">  
    29                         <br>  
    30                         验证码::  
    31                         <input type="text" name="kaptchafield">  
    32                         <br />  
    33                         <input type="submit" name="submit">  
    34                     </form>  
    35                 </td>  
    36             </tr>  
    37         </table>  
    38         <br />  
    39         <br />  
    40         <br />  
    41         <br />  
    42         <%  
    43             String c = (String) session  
    44                     .getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);  
    45             String parm = (String) request.getParameter("kaptchafield");  
    46               
    47             System.out.println(c);  
    48             out.println("Parameter: " + parm + " ? Session Key: " + c + " : ");  
    49             if (c != null && parm != null)  
    50             {  
    51                 if (c.equals(parm))  
    52                 {  
    53                     out.println("<b>true</b>");  
    54                 } else  
    55                 {  
    56                     out.println("<b>false</b>");  
    57                 }  
    58             }  
    59         %>  
    60     </body>  
    61 </html>  
    View Code

    参考文献:

    http://blog.csdn.net/ol_beta/article/details/5877630

  • 相关阅读:
    Spring Boot的每个模块包详解
    spring框架所有包说明
    TCP三次握手和四次挥手
    线程池实现原理
    AVL树与红黑树
    去哪儿网面经
    什么是缓存一致性问题?如何解决?
    Redis的应用场景和优缺点
    线程池
    手写快排
  • 原文地址:https://www.cnblogs.com/dobestself-994395/p/4294426.html
Copyright © 2011-2022 走看看