JCaptcha tries to strictly respect the Inversion of Control pattern, to ease creation of components for concrete applications.
On the other side the Spring Framework allows to use the power of IOC combined with XML declarations.
So that, every single component of jCaptcha can be declared in XML, and instanced through Spring.
An application using jCaptcha should only manipulate a CaptchaService instance.
The simplest configuration possible is to declare the following in your Spring context
Showing how to change the default engine, for getting diferent captchas:
Now if you want to customize your engine configuration, you'll need to define your own implementation
Setting a captcha custom service and engine configuration in Spring
In this section we will describe how to set components to create a full CaptchaEngine.
We will start with what we want, the CaptchaEngine, for each component we provide a working and common example, but every component has several implementations, see the Java doc for more details. The construction of each component in XML must respect a constructor of this component.
There are several Engines pre-configured, but as we want to control configuration, we have to use the GenericCaptchaEngine, which is built with a list of captcha factories (factories are the ?real? producer of captchas)
Then, a CaptchaFactory needs:
- A word generator, to create the text to read.
- A wordToImage, to generate the captcha from the text.
A WordGenerator creates a text to be read, it can be random, be a common implementation take words from a list, and can make composition to create a text easier to read for a human being. In the example the WordGenerator needs a Dictionnary to get real words from.
A Dictionary provides words, this one reads words from the one provided by default, with almost 6000 english words.
After to other important part to create a factory, is the WordToImage component, which is mainly created with three others components:
- A font generator
- A background generator
- A Text paster
This example is a bit more complex one; it takes the usual main three components, but also three deformations, to increase the power of captchas. All three are set to none, a component which creates no deformation, see below, and Examples to have more examples of deformations.
A FontGenerator provide Fonts to a WordToImage, differents fonts increase the difficulties for cracking software using a learning process. This one generates random fonts from a list, and the first two arguments are the minimum size and the maximum size of the font.
A font is declared like this :
The BackgrountGenerator component can be very simple like in the example, single color, or more complex with real picture, or fancy computed shapes. The first two arguments are always, the size (length and height) of the resulting image.
The TextPaster, according to his name, pastes the text on the background. This can be done in a simple way, (see example below), or another implementation can paste each character randomly (but still readably), or can double the text to make computers more confused. TextPaster can be even decorated to put perturbations around the text, a component, TextDecorator, is designed for this purpose, see Annexes for some examples. Commons arguments for TextPaster are:
1. Minimal length of the text
2. Maximal length of the text
3. A color generator component to create the text color, see Annexes.
And a color definition:
Now we are ready to setup the CaptchaService singleton...
Setting the CaptchaService in Spring
This component has a special section because it is the top level component, which is actually manipulated within your application. As for the engine, there is many already configured CaptchaService, but the one we need is a custom one, which takes an CaptchaEngine, as you can see, the BufferedEngineContairner, is in fact CaptchaEngine.
The arg index1 is the captcha session expering time, in seconds. Next arg especifies the maximun storage size.
Other links, resources:
Please refers to External resources