Spring MVC Basic CAPTCHA Code Example
First Time Here?
Check the BotDetect JSP Captcha Quickstart for key integration steps.
The Spring MVC Basic Captcha code example shows the most basic source code required to protect a Spring MVC form with BotDetect CAPTCHA and validate the user input.
This example is very similar to JSP Basic Captcha Example in terms of implementing BotDetect Captcha protection to form(s).
Downloaded Location
The Spring MVC Basic CAPTCHA Example is included in examples
folder of the download package as bdc3-springmvc-basic-captcha-example.war
file. Deploying (unpacking) the file will create a standard JSP directory tree.
- WEB-INF/jsp/index.jsp
- WEB-INF/src/botdetect/examples/springmvc/basic/controller/IndexController.java
- WEB-INF/src/botdetect/examples/springmvc/basic/model/BasicExample.java
index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@taglib prefix="botDetect" uri="botDetect"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>BotDetect CAPTCHA Spring MVC Basic Example</title> <link rel="stylesheet" href="stylesheet.css" type="text/css"/> </head> <body> <form method="post"> <h1>BotDetect CAPTCHA Spring MVC Basic Example</h1> <fieldset> <legend>CAPTCHA Validation</legend> <label for="captchaCodeTextBox" class="prompt"> Retype the code from the picture:</label> <!-- Adding BotDetect Captcha to the page --> <botDetect:captcha id="basicExampleCaptcha"/> <div class="validationDiv"> <input id="captchaCodeTextBox" type="text" name="captchaCodeTextBox" value="${basicExample.captchaCodeTextBox}"/> <input type="submit" name="submit" value="Submit" /> <span class="correct">${basicExample.captchaCodeCorrect}</span> <span class="incorrect">${basicExample.captchaCodeIncorrect}</span> </div> </fieldset> </form> </body> </html>
IndexController.java
package botdetect.examples.springmvc.basic.controller; import botdetect.examples.springmvc.basic.model.BasicExample; import botdetect.web.Captcha; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.validation.BindException; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.SimpleFormController; public class IndexController extends SimpleFormController { public IndexController(){ setCommandClass(BasicExample.class); } @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, "basicExampleCaptcha"); boolean isHuman = captcha.validate(request, basicExample.getCaptchaCodeTextBox()); if (isHuman) { basicExample.setCaptchaCodeCorrect("Correct."); basicExample.setCaptchaCodeIncorrect(""); } else { basicExample.setCaptchaCodeCorrect(""); basicExample.setCaptchaCodeIncorrect("Incorrect!"); } basicExample.setCaptchaCodeTextBox(""); return new ModelAndView("index", "basicExample", basicExample); } }
Controller's onSubmit
method is pretty straightfoward. To keep it simple we just set appropriate messages upon Captcha validaton and then return to the original form.
In your application, you would of course perfom the form action you want to protect from bots after successful Captcha validation.
BasicExample.java
package botdetect.examples.springmvc.basic.model; public class BasicExample { private String userCaptchaCode, captchaCodeCorrect, captchaCodeIncorrect; public String getCaptchaCodeTextBox() { return userCaptchaCode; } public void setCaptchaCodeTextBox(String userCaptchaCode) { this.userCaptchaCode = userCaptchaCode; } public String getCaptchaCodeCorrect() { return captchaCodeCorrect; } public void setCaptchaCodeCorrect(String captchaCodeCorrect) { this.captchaCodeCorrect = captchaCodeCorrect; } public String getCaptchaCodeIncorrect() { return captchaCodeIncorrect; } public void setCaptchaCodeIncorrect(String captchaCodeIncorrect) { this.captchaCodeIncorrect = captchaCodeIncorrect; } }
A Command
object has no BotDetect Captcha related code except boilerplate elements.
To run this example, botdetect.jar
must be in the classpath.
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.
Current BotDetect Versions
-
BotDetect ASP.NET CAPTCHA
2019-07-22v4.4.2 -
BotDetect Java CAPTCHA
2019-07-22v4.0.Beta3.7 -
BotDetect PHP CAPTCHA
2019-07-22v4.2.5