BotDetect Simple CAPTCHA Options: Server Side Persistence in Redis Database Code Example
The Java Simple Captcha options: Server side persistence in Redis database code example shows how to use Redis to store persist Captcha data instead of using default BotDetect HyperSQL Database persistence provider.
First Time Here?
Check the BotDetect Developer Crash Course for key integration steps.
As you may know Redis is an open source, in-memory data structure store used to improve data performance. Therefore Captcha data storage via Redis is neccessary in real world. This combination makes your application have better performance.
To use Redis to store persist Captcha data, BotDetect Java Simple Captcha provides you a simple way by declaring some Redis configuration info in WEB-INF/botdetect.xml
file, such as: redis host, port, password, ssl.
BotDetect Java Simple Captcha uses Jedis as Redis java client, so you need to ensure Jedis jar library is in the classpath.
Download the BotDetect Java CAPTCHA Library and run this exampleExample Location
The Java Captcha options: Application config settings code example is included in the examples/simple-api/bdc4-simple-api-captcha-server-side-persistence-in-redis-database-example.war
file of the download package. Deploying (unpacking) the file will create a standard JSP directory tree
Running Example
This example's war file (in BotDetect download package) has already embedded all dependencies.
In case you are making this example from scratch in your IDE, you need to be sure that botdetect.jar
, botdetect-servlet.jar
, jedis.jar
, commons-pool2.jar
are in the classpath.
index.jsp
<%@page import="com.captcha.botdetect.web.servlet.SimpleCaptcha"%> <%@page trimDirectiveWhitespaces="true"%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>BotDetect Java Simple CAPTCHA Options: Server Side Persistence in Redis Database Code Example</title> <link type="text/css" rel="stylesheet" href="stylesheet.css" /> </head> <body> <form method="post" action="" class="column" id="form1"> <h1>BotDetect Java Simple CAPTCHA Options: <br /> Server Side Persistence in Redis Database Code Example</h1> <fieldset> <legend>Java CAPTCHA validation</legend> <label for="captchaCode">Retype the characters from the picture:</label> <% // Adding BotDetect Captcha to the page SimpleCaptcha captcha = SimpleCaptcha.load(request, "redisCaptchaExample"); String captchaHtml = captcha.getHtml(); out.write(captchaHtml); %> <div class="validationDiv"> <input name="captchaCode" type="text" id="captchaCode" /> <input type="submit" name="ValidateCaptchaButton" id="validateCaptchaButton" value="Validate" /> <% // when the form is submitted if ("POST".equalsIgnoreCase(request.getMethod())) { // validate the Captcha to check we're not dealing with a bot boolean isHuman = captcha.validate(request.getParameter("captchaCode")); if (isHuman) { // Captcha validation passed, perform protected action out.print("<span class=\"correct\">Correct code</span>"); } else { // Captcha validation failed, show error message out.print("<span class=\"incorrect\">Incorrect code</span>"); } } %> </div> </fieldset> </form> </body> </html>
After including the BotDetect Captcha library, adding Captcha protection to the form is as simple as creating a SimpleCaptcha
object instance and printing the getHtml(ServletContext context)
method result. The same object instance provides easy Captcha validation using the validate(String userInput)
method.
botdetect.xml
<?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.5.xsd"> <captchaStyles> <captchaStyle> <name>redisCaptchaExample</name> <userInputID>captchaCode</userInputID> </captchaStyle> </captchaStyles> <captchaPersistence> <persistenceType>REDIS</persistenceType> <host>localhost</host> <port>6379</port> <connectionTimeout>2000</connectionTimeout> <password></password> <ssl>false</ssl> </captchaPersistence> </botdetect>
In WEB-INF/botdetect.xml
configuration file, we tell BotDetect to use Redis as the BotDetect persistence provider instead of the default one (HyperSQL Database). You can find the detailed explanation about this at Captcha Redis persistence provider documentation.
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <session-config> <session-timeout> 30 </session-timeout> </session-config> <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> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
The example WEB-INF/web.xml
file we register SimpleCaptchaServlet
used for BotDetect Captcha requests..
Please Note
BotDetect Java Captcha Library v4.0.Beta3.5 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
2018-11-06v4.4.0
2015-12-03v3.0.18 -
BotDetect Java CAPTCHA
2018-09-20v4.0.Beta3.5
2015-12-03v3.0.Alpha3 -
BotDetect PHP CAPTCHA
2018-09-20v4.2.3
2015-12-03v3.0.4 -
BotDetect ASP Classic CAPTCHA
(Classic ASP port is discontinued)
2016-07-25v4.1.0
2015-12-03v3.0.18