Kaptcha如何使用(翻译)
Introduction介绍
Basic use of kaptcha in your webapp is quite easy. 在您的网络应用中对验证码的基本使用非常简单。All you need to do is add the jar to your project, make a reference to the kaptcha servlet in your web.xml and then check the servlet session for the generated kaptcha value and compare it to what the user submits on your form.您所需要做的就是将jar添加到您的项目中,在web.xml中引用kaptcha servlet,然后检查servlet会话中生成的kaptcha值,并将其与用户在表单上提交的值进行比较。
For those of you looking for an audio kaptcha solution, this product probably won't ever support that unless someone else wants to work on it. 对于那些正在寻找音频kaptcha解决方案的人来说,除非其他人想对其进行处理,否则此产品可能永远不会支持。Sorry.抱歉。
Note: If you are currently using JCaptcha in the same .war file as Kaptcha, you will have a conflict with the image generation library. 注意:如果当前在与Kaptcha相同的.war文件中使用JCaptcha,则与图像生成库会有冲突。You must remove JCaptcha. 您必须删除JCaptcha。See this issue for details.有关详细信息,请参见此问题。
Example例
Included in the distribution is an example .war file for you to play with. 该发行版中包括一个供您使用的示例.war文件。Note that this .war file will only work with Java 1.5 and higher. 请注意,此.war文件仅适用于Java 1.5及更高版本。For easiest setup, download Tomcat. 为了最简单的安装,请下载Tomcat。I use 5.5.x and I download the 'Core' distribution.我使用5.5.x,然后下载“ Core”发行版。
Then, copy the kaptcha.war file into the webapps directory. 然后,将kaptcha.war文件复制到webapps目录中。Make sure it is named 'kaptcha.war'. 确保将其命名为“ kaptcha.war”。Then, start up Tomcat. 然后,启动Tomcat。I type on Unix: 我在Unix上输入:./bin/catalina.sh run
or on Windows: ./bin/catalina.sh运行或在Windows上:bin/catalina.bat run
.bin / catalina.bat运行。
Now, visit: 现在,访问:http://localhost:8080/kaptcha/KaptchaExample.jsp
If you edit the web.xml file in the exploded kaptcha.war folder you can test changes to the ConfigParameters quite easily.如果您在展开的captcha.war文件夹中编辑web.xml文件,则可以很容易地测试对Config Parameters的更改。
Details细节
Here are the details of how to integrate Kaptcha into your own application.以下是如何将Kaptcha集成到您自己的应用程序中的详细信息。
- Put the appropriate .jar file (depending on which JDK you are using) into the WEB-INF/lib directory of your .war file.将适当的.jar文件(取决于您使用的JDK)放入.war文件的WEB-INF / lib目录中。
- Put the image tag into your page (checking that the path to the .jpg matches up with what is defined in your web.xml below for the url-pattern):将image标签放入您的页面中(检查.jpg的路径是否与下面的web.xml中为url模式定义的内容匹配):
``````
``````
- Put the reference in your web.xml (checking that the url-pattern path matches up with what you put in your html fragment above):将引用放入您的web.xml中(检查url-pattern路径是否与您在上面的html片段中放置的内容匹配):
<servlet>
<servlet-name>Kaptcha</servlet-name>
<servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class> </servlet>
<servlet-mapping>
<servlet-name>Kaptcha</servlet-name>
<url-pattern>/kaptcha.jpg</url-pattern>
</servlet-mapping>
- In your code that manages the submit action:在管理提交操作的代码中:
String kaptchaExpected = (String)request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
String kaptchaReceived = request.getParameter("kaptcha");
if (kaptchaReceived == null || !kaptchaReceived.equalsIgnoreCase(kaptchaExpected)) { setError("kaptcha", "Invalid validation code."); }
- Make sure to start your JDK with -Djava.awt.headless=true确保使用-Djava.awt.headless = true启动您的JDK
That is it!这就对了!!
Extra额外
If you love JQuery like I do, you can use it to easily solve the problem that happens when someone can't read the image that was generated for them. 如果像我一样喜欢JQuery,则可以使用它轻松解决当某人无法读取为其生成的图像时发生的问题。What this does is bind a function to the onclick handler for the img element so that when the image is clicked on, it will get reloaded. 这样做是将函数绑定到img元素的onclick处理函数,以便在单击图像时将重新加载该图像。Note the use of the query string to append a random number to the end. 请注意使用查询字符串将随机数附加到末尾。This is because the browser has already cached the image and the query string makes it look like a new request.这是因为浏览器已经缓存了图像,并且查询字符串使它看起来像一个新请求。
$(function(){ $('#kaptchaImage').click(function () { $(this).attr('src', '/kaptcha.jpg?' + Math.floor(Math.random()*100) ); }) });
Can't read the image? Click it to get a new one. 无法读取图像?单击它以获取一个新的。```
If you want to get fancy, you can add an extra visual effect when refreshing the image. 如果想花哨的话,可以在刷新图像时添加额外的视觉效果。This example uses the fadeIn() effect when you click on it:当您单击该示例时,将使用fadeIn()效果:
Customization客制化
There are many web.xml parameters to customize the output of the kaptcha.有许多web.xml参数可用于定制kaptcha的输出。