Wiki source code of Captcha Module

Version 2.1 by Caleb James DeLisle on 2010/02/03 23:23

Show last authors
1 The captcha module supplies captchas and checks answers for correctness. It uses [[JCaptcha>>http://jcaptcha.sourceforge.net/]] 2.0 to get captchas and supplies them through a [[struts>>http://struts.apache.org/]] action.
2
3 = Getting the captcha image
4
5 The captcha images are served from yourwiki.com/xwiki/bin/imagecaptcha~/~/
6 The trailing / is required by struts but you can put anything before or after it so:
7
8 {{code language=java}}
9 xwiki.getURL(doc.getFullName(), "imagecaptcha")
10 {{/code}}
11
12 will output yourwiki.com/xwiki/bin/imagecaptcha/SomeSpace/SomePage
13 which will work.
14
15 = Verifying the answer
16
17 To verify the captcha answer in Groovy, you simply get the CaptchaVerifier using Utils.getComponent
18 then call isAnswerCorrect
19
20 {{code language=java}}
21 import com.xpn.xwiki.web.Utils;
22 import org.xwiki.captcha.CaptchaVerifier;
23 CaptchaVerifier verifier = Utils.getComponent(CaptchaVerifier.class, "image");
24 verifier.isAnswerCorrect(verifier.getUserId(request), userSuppliedAnswer);
25 {{/code}}
26
27 JCaptcha uses a unique String to tell different users apart so it knows who should be solving which captcha. This String is gotten from the Request object when the user's browser loads the captcha image and it must be supplied to isAnswerCorrect in order to check the supplied answer against the right stored answer. You can get this String by calling getUserId with the Request object.
28
29 For more information, you can read the code for the interface [[CaptchaVerifier>>http://svn.xwiki.org/svnroot/xwiki/platform/core/trunk/xwiki-captcha/src/main/java/org/xwiki/captcha/CaptchaVerifier.java]].
30
31 = What about sound captchas?
32
33 Sound captcha support will be added when JCaptcha 2.0 is officially released.
34
35 = How about implementing your own captcha type?
36
37 JCaptcha supports a variety of different types of captchas, you can add a new captcha type to XWiki by copying [[DefaultImageCaptchaAction>>http://svn.xwiki.org/svnroot/xwiki/platform/core/trunk/xwiki-captcha/src/main/java/org/xwiki/captcha/internal/DefaultImageCaptchaAction.java]] but using a different JCaptcha ImageCaptchaService implementation.
38
39 You can also add a different captcha type which doesn't use JCaptcha at all, all you have to do is implement CaptchaVerifier. Remember that you will be writing a component so it's a good idea to take a look at the [[ComponentModule]].

Get Connected