I realy like jboss seam and recaptcha project, so it was natural for me to use both technologies. It turned out that integrating them is as easy as breathing. To achieve my goal I have made following steps:
- I have downloaded small, but very usefull library Java-Recaptcha (contributed by Soren).
- I have added recaptcha4j-0.0.7.jar to class path of my seam application.
- Afterwards I have created a small and simple utility class, which integrates seam and recaptcha. The code is as follows:
package com.infosystem.server.util;
import javax.servlet.http.HttpServletRequest;
import org.jboss.seam.web.ServletContexts;
import net.tanesha.recaptcha.ReCaptcha;
import net.tanesha.recaptcha.ReCaptchaFactory;
import net.tanesha.recaptcha.ReCaptchaResponse;
public class RecaptchaUtils {
private static final String PUBLIC_KEY = "your public key";
private static final String PRIVATE_KEY = "your private key";
private static ReCaptcha reCaptcha = ReCaptchaFactory.newReCaptcha(PUBLIC_KEY,
PRIVATE_KEY, false);
public static String createRecaptchaHtml(String error) {
Properties options = new Properties();
options.setProperty("theme", "mytheme");
options.setProperty("tabindex", "1");
return reCaptcha.createRecaptchaHtml(error, options);
}
public static ReCaptchaResponse getRecaptchaRespoonse(String challenge,
String response, String remoteAddress) {
ReCaptchaResponse answer = reCaptcha.checkAnswer(remoteAddress,
challenge, response);
return answer;
}
public static ReCaptchaResponse getRecaptchaResponseFromServletContext() {
HttpServletRequest reqest = ServletContexts.instance().getRequest();
return getRecaptchaRespoonse(reqest
.getParameter("recaptcha_challenge_field"), reqest
.getParameter("recaptcha_response_field"), reqest
.getRemoteAddr());
}
}If we need more control over properties, we just need to write a bit more complex method.
- In order to add recaptcha to seam page I use "createRecaptchaHtml" method in my small bean, which is good basis to create somethign with more complex logic
- Adding it code of the site is easy
- Ready.
|
package com.infosystem.forms.session; @Stateless |
|
<h:form> .... <h:outputText value="#{captcha.html}" escape="false" /> ... </h:form> |
If something is not clear, just write to me and I will try to help.




Comments
http://www.primefaces.org/showcase/ui/captcha.jsf
just need to generate public/private keys and add them to web.xml (the only disadvantage - update to deployment)
org.primefaces.component.captcha.PRIVATE_KEY
private-key
RSS feed for comments to this post