Recaptcha module for Play!
Integrate challenge-response reCaptcha tests in 5 minutes.
What is a Captcha?
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.
Features
- 5 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
Prerequisites
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
Installation
There are two ways to deploy this module.
- You can download the zip and unpack it in your play/module folder.
- or, You can simply run 'play install recaptcha-0.9' and let Play do all the job for you!
How to Use
Well it’s kind of easy.
First, add this line in your conf/application.conf
module.recaptcha=${play.path}/modules/recaptcha-0.9
Now the module offers two integration points: one to display the captcha, the other to validate it.
Generating the captcha
Play being an MVC framework, we need to define a action in the controller and its associated page.
Edit file app/controller/Application.java as follows:
public class Application extends Controller {
public static void register() {
render();
}
}
Create a file `app/view/Application/register.html`
#{extends ‘main.html’ /}
#{set title:‘Test reCaptcha’ /}
#{form @save()}
your-other-form-stuff
#{crionics.recaptcha publicKey:“YOUR_PUBLIC_KEY”, privateKey:“YOUR_PRIVATE_KEY”, theme:“red”, tabindex:“2”/}
#{/form}
Validating the captcha
Edit file app/controller/Application.java and add the following:
import com.crionics.recaptcha.Recaptcha;public class Application extends Controller {
public static void save() {// ... Do some other form validation...
if (status) { System.out.println(“Captcha answer was entered correctly!”); } else { System.out.println(“Captcha answer is wrong”); register(); } } }
boolean status = Recaptcha.checkAnswer(“YOUR_PRIVATE_K”, request, params);
Testing the captcha
Start play and launch a browser to http://localhost:9000/Application/register
Done!
Tag parameters
The crionics.recaptcha tag takes the following parameters
publicKey: Mandatory field
privateKey: Mandatory field
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.
Credits
This module is based on the Recaptcha java library