码迷,mamicode.com
首页 > 其他好文 > 详细

jcaptcha sample 制作验证码

时间:2016-10-06 12:33:01      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:

Skip to end of metadata

    Created by marc antoine garrigue, last modified by Jeremy Waters on Feb 23, 2012

Go to start of metadata
Description

This integration module provides the shortest way to integrate JCaptcha to your web application.
This module is available since jcaptcha 2.0

This integration module provides the new default captcha configuration :

All you need to do is add the jcaptcha jar to your project, make a reference to the SimpleImageCaptchaServlet in your web.xml and then use the servlet session id to validate what the user submits on your form against the captcha service.
Set up

    Add the jcaptcha jar file into the WEB-INF/lib directory of your .war file
    If your app or server do not already have commons-logging and commons-collection into the WEB-INF/lib directory of your .war

    Put the reference in your web.xml (checking that the url-pattern path matches up with what you put in your html fragment above):

<servlet>
        <servlet-name>jcaptcha</servlet-name>
        <servlet-class>com.octo.captcha.module.servlet.image.SimpleImageCaptchaServlet</servlet-class>
</servlet>
<servlet-mapping>
        <servlet-name>jcaptcha</servlet-name>
        <url-pattern>/jcaptcha.jpg</url-pattern>
</servlet-mapping>

    Add the folowing image tag to the form you want to protect, this call will asks the SimpleImageCaptchaServlet to generate a fresh new captcha

<form action="submit.action">
...
    <img src="jcaptcha.jpg" /> <input type="text" name="jcaptcha" value="" />
...
</form>

    In your code that manages the submit action add the following code fragment to validate the user input

String userCaptchaResponse = request.getParameter("jcaptcha");
boolean captchaPassed = SimpleImageCaptchaServlet.validateResponse(request, userCaptchaResponse);
if(captchaPassed){
// proceed to submit action
}else{
// return error to user
}

And that‘s it!
Sample Web App

    see the attached sample web app (warning, this war contains commons-logging and commons-collections)

Jcaptcha 2.0 jars

    jcaptcha-api-1.0.jar
    jcaptcha-2.0-alpha-1-SNAPSHOT.jar
    jcaptcha-integration-simple-servlet-2.0-alpha-1-SNAPSHOT.jar
    filters-2.0.235.jar

按照上面说的做,然后把相关的jar放进去,注意还有 commons-logging and commons-collection 这两个.

其他网友说很简单,但是第一次做也不觉的简单,做完了才觉得简单.这个是网上的教程,由于我这里的网络不好所以就记录下来,和spring整合也可以这样做,先简单的整合起来,后面再优化

教程原网址 jcaptcha

说一个可疑的bug

技术分享

当注册的时候出现用户名重复的问题之后,修改用户名,此时会报验证码错误,仔细看看验证码没有错啊,但是程序却提示错了,这个类似的问题我遇到了,当用这个插件时,如果后台出现验证失败,而且验证码(猜测是放在第一个验证的)已经被验证码,那么就不能被第二次验证,我本地的后台时报了异常的.所以在每次经过后台验证出错之后都要刷新验证码,就是在ajax的success方法中加入刷新页面的js 方法.

 

jcaptcha sample 制作验证码

标签:

原文地址:http://www.cnblogs.com/rocky-AGE-24/p/5933520.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!