Integrate challenge-response reCaptcha tests in 2 minutes.
A Captcha is a type of challenge-response test used in computing to ensure that the response is not generated by a computer. Captcha are typically used to fight spam and validate that a human is using the system.
- 2 minutes integration guaranteed
- Industry standard Captcha engine [reCaptcha](http://www.google.com/recaptcha)
- Automatically detects language preference from the Play Framework
- Customizable theme, tabindex and language
The modules requires the recapctha public/private key for your domain.
- Please go to http://www.google.com/recaptcha
- Register and create a site for your domain
- Write down the generated public and private keys
- run ‘play install recaptcha’ and let Play! install the module for you
- open your application conf/application.conf and add
- module.recaptcha=${play.path}/modules/recaptcha-{version}
- ugot.recaptcha.privateKey=YOUR_RECAPTCHA_PRIVATE_KEY
- ugot.recaptcha.publicKey=YOUR_RECAPTCHA_PUBLIC_KEY
Make sure to replace {version} and KEYS with their appropriate values
Well it’s kind of easy, the module offers two integration points:
- a tag to render the captcha: #{ugot.recaptcha /}
- an annotation to validate the result: @Recaptcha
Play being an MVC framework, we need to define an action in the controller and its associated page.
Edit file app/controller/Application.java as follows:
bc.
public class Application extends Controller {
public static void index() {
render();
}
}
Create a file `app/view/Application/index.html`
bc.
#{extends ‘main.html’ /}
#{set title:‘Test reCaptcha’ /}
#{form @save()} My sample captcha!
#{ugot.recaptcha theme:“red”, tabindex:1/}
#{ifError ’recaptcha’}
#{error ‘recaptcha’/}
#{/ifError}
#{/form}
Edit file app/controller/Application.java and add the following:
import ugot.recaptcha.Recaptcha; public class Application extends Controller { public static void save(@Recaptcha String recaptcha) { if(validation.hasErrors()) { System.out.println("ERRORS found - invalid captcha"); validation.keep(); } else { System.out.println("There are no errors, the catcha was validated"); } // no matter what, redisplay the same page index(); } }
Start play and launch a browser to http://localhost:9000/
Done!
You may find the sample app bundled with the module
The ugot.recaptcha tag takes the following parameters
theme: The theme to use. Possible values are:red (default), white, blackglass, clean
tabindex: Sets a tabindex for the reCAPTCHA text box. If other elements in the form use a tabindex, this should be set so that navigation is easier for the user
lang: A two character country code. By default the module will ask Play to provide the negotiated language. possible values are: en,nl,fr,de,pt,ru,es,tr
See the reCaptcha documentation for more details.
This module is based on the Recaptcha java library.
Author: Olivier Refalo