apacherecaptchawicket-1.5

Can I use RE-Captcha with Wicket?


Can I use recaptcha with apache wicket 1.5.3? Is there some good example?


Solution

  • Have you read this?

    I have added the guide here in case page disappears.

    Usage

    We will create a panel called RecaptchaPanel. In order to use this component to your application all you'll have to do is this:

    add(new RecaptchaPanel("recaptcha"));
    

    and of course, add the component in your markup:

    <div wicket:id="recaptcha"></div>
    

    Implementation

    Implementation is simple. All you have to do, is to follow several steps:

    Add recaptcha dependency to your project

    <dependency>
     <groupid>net.tanesha.recaptcha4j</groupid>
     <artifactid>recaptcha4j</artifactid>
     <version>0.0.7</version>
    </dependency>
    

    This library hides the implementation details and expose an API for dealing with recaptcha service.

    Create associated markup (RecaptchaPanel.html)

    <wicket:panel><div wicket:id="captcha"></div></wicket:panel>
    

    Create RecaptchaPanel.java

    import net.tanesha.recaptcha.ReCaptcha;
    import net.tanesha.recaptcha.ReCaptchaFactory;
    import net.tanesha.recaptcha.ReCaptchaImpl;
    import net.tanesha.recaptcha.ReCaptchaResponse;
    
    /**
     * Displays recaptcha widget. It is configured using a pair of public/private keys which can be registered at the
     * following location:
     * 
    * https://www.google.com/recaptcha/admin/create
     * <br>
     * More details about recaptcha API: http://code.google.com/apis/recaptcha/intro.html
     *
     * @author Alex Objelean
     */
    @SuppressWarnings("serial")
    public class RecaptchaPanel extends Panel {
      private static final Logger LOG = LoggerFactory.getLogger(RecaptchaPanel.class);
      @SpringBean
      private ServiceProvider serviceProvider;
    
    
      public RecaptchaPanel(final String id) {
        super(id);
        final ReCaptcha recaptcha = ReCaptchaFactory.newReCaptcha(serviceProvider.getSettings().getRecaptchaPublicKey(),
          serviceProvider.getSettings().getRecaptchaPrivateKey(), false);
        add(new FormComponent<void>("captcha") {
          @Override
          protected void onComponentTagBody(final MarkupStream markupStream, final ComponentTag openTag) {
            replaceComponentTagBody(markupStream, openTag, recaptcha.createRecaptchaHtml(null, null));
          }
    
          @Override
          public void validate() {
            final WebRequest request = (WebRequest)RequestCycle.get().getRequest();
    
            final String remoteAddr = request.getHttpServletRequest().getRemoteAddr();
            final ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
            reCaptcha.setPrivateKey(serviceProvider.getSettings().getRecaptchaPrivateKey());
    
            final String challenge = request.getParameter("recaptcha_challenge_field");
            final String uresponse = request.getParameter("recaptcha_response_field");
            final ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr, challenge, uresponse);
    
            if (!reCaptchaResponse.isValid()) {
              LOG.debug("wrong captcha");
              error("Invalid captcha!");
            }
          }
        });
      }
    }
    </void>
    

    Things to notice: