BotDetect Spring 2 CAPTCHA Integration Quickstart

Install BotDetect Java CAPTCHA dependencies

The free version Maven artifacts are available from our public repository; while the enterprise version jars are available in the root folder of the enterprise version's archive.

To reference the BotDetect dependency from our public repository, the repository itself has to be declared first -- add the highlighted lines to your app's pom.xml file:

<repository>
  <id>captcha</id>
  <name>BotDetect Captcha Repository</name>
  <url>https://git.captcha.com/botdetect-java-captcha.git/blob_plain/HEAD:/</url>
</repository>

Then, in the same file, declare the BotDetect dependency, too:

<dependency>
  <groupId>com.captcha</groupId>
  <artifactId>botdetect-jsp20</artifactId>
  <version>4.0.beta3.7</version>
</dependency>

2. Show a CAPTCHA Challenge on the Form

On the very top of the source file for the web form you want to protect against bots, put BotDetect taglib declaration:

<%@taglib prefix="botDetect" uri="https://captcha.com/java/jsp"%>

To render Captcha in the form, add the following within <form> you want to protect against bots:

<!-- Adding BotDetect Captcha to the page -->
<botDetect:captcha id="exampleCaptcha" userInputID="captchaCode"/>

<input type="text" name="captchaCode" id="captchaCode"/>
<input type="submit" name="submit" label="Submit" id="submit"/>

3. Check User Input During Form Submission

When the form is submitted, the Captcha validation result must be checked in Spring Controller:

[...]
import com.captcha.botdetect.web.servlet.Captcha;

public class ExampleController extends SimpleFormController {

  [...]

  @Override
  protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response,
      Object command, BindException errors) {
    BasicExample basicExample = (BasicExample) command;
    
    // validate the Captcha to check we're not dealing with a bot
    Captcha captcha = Captcha.load(request, "exampleCaptcha");
    boolean isHuman = captcha.validate(basicExample.getCaptchaCode());
    
    if (isHuman) {
      basicExample.setCaptchaCorrect("Correct code");
      basicExample.setCaptchaIncorrect("");
    } else {
      basicExample.setCaptchaCorrect("");
      basicExample.setCaptchaIncorrect("Incorrect code");
    }
    
    basicExample.setCaptchaCode("");

    return new ModelAndView("example", "basicExample", basicExample);
  }
}

4. Configure your Application

Update your application configuration (web.xml) file.

<servlet>
  <servlet-name>BotDetect Captcha</servlet-name>
  <servlet-class>com.captcha.botdetect.web.servlet.CaptchaServlet</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>BotDetect Captcha</servlet-name>
  <url-pattern>/botdetectcaptcha</url-pattern>
</servlet-mapping>

In-Depth Spring MVC 2 CAPTCHA Instructions and Explanations

Detailed Spring MVC 2 Captcha instructions and explanations can be found in the Spring MVC 2 Captcha integration how to guide.


Please Note

BotDetect Java Captcha Library v4.0.Beta3.7 is an in-progress port of BotDetect 4 Captcha, and we need you to guide our efforts towards a polished product. Please let us know if you encounter any bugs, implementation issues, or a usage scenario you would like to discuss.