Wiki source code of

Version 6.1 by Vincent Massol on 2010/12/07 13:36

Show last authors
1 {{include document="ExtensionCode.ExtensionSheet"/}}
2
3 image:captchaEx2.png
4
5 {{toc/}}
6
7 = Getting the captcha image =
8
9 The captcha images are served from ##yourwiki.com/xwiki/bin/imagecaptcha~/~/ ##
10 The extra trailing / is required by struts but you can put anything before or after it so:
11
12 {{code language="java"}}
13 xwiki.getURL(doc.getFullName(), "imagecaptcha")
14 {{/code}}
15
16 will output ##yourwiki.com/xwiki/bin/imagecaptcha/SomeSpace/SomePage## which will work.
17
18 = Verifying the answer =
19
20 To verify the captcha answer in Groovy, you simply get the CaptchaVerifier using Utils.getComponent
21 then call isAnswerCorrect
22
23 {{code language="java"}}
24 import com.xpn.xwiki.web.Utils;
25 import org.xwiki.captcha.CaptchaVerifier;
26 CaptchaVerifier verifier = Utils.getComponent(CaptchaVerifier.class, "image");
27 verifier.isAnswerCorrect(verifier.getUserId(request), userSuppliedAnswer);
28 {{/code}}
29
30 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.
31
32 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]].
33
34 = What about sound captchas? =
35
36 Sound captcha support will be added when JCaptcha 2.0 is officially released.
37
38 = How about implementing your own captcha type? =
39
40 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.
41
42 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 [[Extension.Component Module]].

Get Connected