Wednesday, March 9, 2011

JCaptcha & Spring MVC

Have you ever heard about Captcha.
Is this familiar?

Now that you remember what I'm talking about, let's begin.
I chose JCaptcha, because I used before with Struts back in Mexico for one of my projects when I was working in Government (SICOSIEM) and it was really useful (but I used a servlet)
Now the idea will be implementing the same project on an existing Spring MVC App, which is really simple :)
First things first!. Get JCaptcha and install it in your Local MVN Repo (version 1.0-all), because if you add as dependency version 1.0, it will not work, due to the lack of some jars on Maven Central Repo.
My Controller which will behave like my old servlet (I just have to create an IMG tag with src="/myAppContext/captcha/captcha.htm")
This is how you have to use it (on my previous jsp I have an input text called "captchaText" which holds the value entered by the user)
This is a minimal configuration (I have the bad habit of splitting all my configurations to make my life easier, on this case I'll call this configuration file "jcaptcha.xml"), if you want to dig deeper, you can explore the official web site and play around with other classes (just look into this package: com.octo.captcha.engine.image.gimpy)
FYI: This is the captcha service I'm injecting in my Controllers
Don't forget adding your new configuration file for JCaptcha in your Spring App Context (this is my web.xml)
If you want to see a more complex ("customized") configuration, you can use see this one (It took me a while to create it - but I used as reference what is on JCaptcha WebSite)

No comments:

Post a Comment