How To Add BotDetect CAPTCHA Protection to Spring MVC Forms

Protecting your Spring MVC forms with BotDetect Java Captcha requires a few steps, outlined on this page.

First Time Here?

Check the BotDetect JSP Captcha Quickstart for key integration steps.

You can also see how BotDetect Captcha protection has been added to various kinds of Spring MVC forms and projects by running the BotDetect Captcha Spring integration code examples coming with the BotDetect installation. You can also reuse the code example source code that fits your requirements.

CAPTCHA Integration Steps

To add BotDetect Captcha protection to a Spring MVC application:

Add BotDetect Java CAPTCHA Library Dependency

Here is how to add BotDetect Java CAPTCHA Library dependency in various dependency management scenarios:

To manually add BotDetect Captcha library to classpath, copy the following jar files from BotDetect Java download package to application's WEB-INF/lib folder:

  • botdetect-4.0.beta3.4.jar
  • botdetect-servlet-4.0.beta3.4.jar
  • botdetect-jsp20-4.0.beta3.4.jar
  • hsqldb.jar

The hsqldb.jar above is HyperSQL Database library, you can download it at its site.

To share BotDetect Captcha among multiple applications, these BotDetect jar files above should be copied into 'lib' directory of web container or application server's domain.

Register SimpleCaptchaServlet

Application web.xml configuration file setting

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

Spring Boot servlet registration

In case you are using Spring Boot servlet is registered in a following way:

@SpringBootApplication
public class SpringBootWebApplication {

    [...]

    @Bean
    ServletRegistrationBean simpleCaptchaServletRegistration () {
      ServletRegistrationBean srb = new ServletRegistrationBean();
      srb.setServlet(new SimpleCaptchaServlet());
      srb.addUrlMappings("/botdetectcaptcha");
      return srb;
    }
}

Configure Captcha options

Configure captcha options in WEB-INF/botdetect.xml configuration file. You can find a full list of available Captcha configuration options and related instructions at the Captcha configuration options page.

<?xml version="1.0" encoding="UTF-8"?>
<botdetect xmlns="https://captcha.com/schema/java" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="https://captcha.com/schema/java 
      https://captcha.com/schema/java/botdetect-4.0.beta3.4.xsd">

  <captchaStyles>
    <captchaStyle>
      <name>exampleCaptcha</name>
      <userInputID>captchaCode</userInputID>
      <codeLength>3-5</codeLength>
    </captchaStyle>
  </captchaStyles>

</botdetect>

Display Captcha Protection on the Spring MVC Form

We'll assume you already have a form which can be posted (<form method="post" ...), with other fields in place.

BotDetect custom simpleCaptcha tag is designed to add CAPTCHA protection to Spring MVC form as simple as possible.

At the top of the file put BotDetect taglib declaration:

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

To display the Captcha test on your form, you will need the following Html elements:

  • A textbox for the Captcha code user input, with a label displaying Captcha instructions
  • The Captcha markup including the image, sound and reload icons etc., which will be generated by the Captcha tag.

For example:

<botDetect:simpleCaptcha id="exampleCaptcha"/>

<div class="validationDiv">
    <input id="captchaCode" type="text" name="captchaCode"
            value="${basicExample.captchaCode}"/>
    <input type="submit" name="submit" value="Submit" />
    <span class="correct">${basicExample.captchaCorrect}</span>
    <span class="incorrect">${basicExample.captchaIncorrect}</span>
</div>

When you open your form in a browser, the above declarations should render as:

BotDetect CAPTCHA added to an JSP page

If you are adding Captcha protection to multiple Spring MVC forms in the same website, you should initialize each Captcha object with unique name (e.g. "registrationCaptcha", "commentCaptcha", ...).

Validate Captcha User Input During Spring MVC Form Submission

Since we want to ensure only real human users can perform a certain action (e.g. account registration or comment submission), we also have to add Captcha validation code which will process form submissions, and only allow certain actions if Captcha validation succeeds.

Add CAPTCHA Validation Logic to Spring MVC Controller

When the form is submitted, the Captcha validation result must be checked and the protected action (user registration, comment posting, email sending, ...) only performed if the Captcha test was passed. You just have to create SimpleCaptcha object instance with the same name as the one used on the form first. For example, this code should be part of or invoked from controller's onSubmit method:

SimpleCaptcha captcha = SimpleCaptcha.load(request, "exampleCaptcha");
boolean isHuman = captcha.validate(basicExample.getCaptchaCode());

if (isHuman) {
  // TODO: Captcha validation passed, perform protected action
} else {
  // TODO: Captcha validation failed, show error message
}

return new ModelAndView("index", "basicExample", basicExample);

This approach is shown in the BotDetect Spring MVC Basic CAPTCHA integration code example included in the BotDetect download package.


Please Note

BotDetect Java Captcha Library v4.0.Beta3.4 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.