Java Humanizer is an alternative to using CAPTCHA on your site. It's heavily inspired by the fantastic Humanizer project for Rails applications.
Java Humanizer works by providing a properties file containing simple questions and their answers. The questions are easily answered by humans, but provide enough of a challenge for scripts to stop unwanted form submissions.
Clone this project to your machine and run gradlew ( if you don't have gradle installed already ). Once gradle is installed, run: gradle build. This will output a jar file in build/libs that you can include in your project.
Get an instance of the Humanizer class:
Humanizer humanizer = new Humanizer();
Ask it for a random question:
Question question = humanizer.getQuestion();
Display the question in your JSP:
<input type="hidden" name="humanizerQuestionId" value="${humanizerQuestion.id}" />
<label for="humanizerAnswer">${humanizerQuestion.question}</label>
<input type="text" id="humanizerAnswer" name="humanizerAnswer" size="64" />
In your handler, check the answer:
long id = // get question id from paaramters
String answer = // get answer from parameters
boolean isHuman = Humanizer.checkAnswer(id, answer);
The jar file comes with a properties file containing a set of questions and answers embedded within the jar, but you can easily provide your own set.
The format for this file is:
question.<n>=Question goes here
answer.<n>=Answer1; Answer2
When the file is parsed, the value of <n> is used as the question's ID so we can find the correct set of answers. Multiple answers are supported for each question, which allows for variations of the answer. For example:
question.1=Two plus two?
answer.1=4; four
This allows the question to be answered with either the number 4 or the word "four".
To use a custom set of questions and answers, just provide Humanizer with a Properties object that has your custom set of questions and answers loaded.
String file = "..."; // location of custom questions and answers
Properties props = new Properties();
props.load(file);
Humanizer humanizer = new Humanizer(props);
Changes in version 1.0:
- Build was changed from maven to gradle
- JUnit tests were converted to use the Spock Framework
- Questions were modified based on user feedback from 2 years in production
- Updates to README
The original list of questions included in this project are taken directly from Humanizer by Kisko Labs. The questions have been modified slightly based on user feedback from usage in production at BiddingForGood.
Java Humanizer is licensed under the MIT License, for more details see the LICENSE file.