Google reCAPTCHA helps stop automated abuse of your website (reminiscent of remark spam or bogus registrations) by utilizing a CAPTCHA to make sure that solely people carry out sure actions. It’s a free, safe and fashionable Net service utilized by 200,000+ web sites the world over. To combine reCAPTCHA in your web site you might want to Signup along with your google account to generate private and non-private keys. This keys are later used to google to indicate CAPTCHA in your register web site.
If you wish to add Google reCAPTCHA into your web site then go to http://www.google.com/recaptcha/whyrecaptcha
and SignUp utilizing your Google account and geenrate Public, Personal keys used to allow CAPTCHA into your web site. Now obtain reCaptcha4j.jar file from https://code.google.com/p/recaptcha/
and add into eclipse as a jar.
Create a house.jsp with following code…
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
<%@ web page import=“internet.tanesha.recaptcha.ReCaptcha” %>
<%@ web page import=“internet.tanesha.recaptcha.ReCaptchaFactory” %>
<html>
<head>
<title>Google ReCaptcha with SpringMVC</title>
kind=”textual content/css”>
physique {
font-family: verdana;
font-size: 12px;
margin: 40px;
}
h2 {
background-color: #5284DC;
shade: #fff;
border-radius:4px;
width: 360px;
padding: 5px;
peak: 30px;
}
</head>
<physique>
<h2>Google ReCaptcha with SpringMVC</h2>
<type motion=“validate” technique=“put up”>
<%
ReCaptcha c = ReCaptchaFactory.newReCaptcha(
“6LeeTusSAAAAAPwiEGXXXXXXXXXXXXXXXXXXXXXX”,
“6LeeTusSAAAAAJ-XXXXXXXXXXXXXXXXXXXXXXXXX”,
false);
out.print(c.createRecaptchaHtml(null, null));
%>
<enter kind=“submit” worth=“Validate Captcha” />
</type>
</physique>
</html>
|
Now create Spring controller to validate CAPTCHA string entered by Person.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
package deal org.techzoo.springmvc.captcha;
import javax.servlet.http.HttpServletRequest;
import internet.tanesha.recaptcha.ReCaptchaImpl;
import internet.tanesha.recaptcha.ReCaptchaResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.internet.bind.annotation.RequestMapping;
/**
* @creator Tousif Khan
*
*/
@Controller
public class GoogleReCaptchaController {
@RequestMapping(worth = “/index”)
public String index() {
return “house”;
}
/**
* Methodology used to validate CAPTCHA textual content entered by Person
**/
@RequestMapping(worth = “/validate”)
public String validateCaptcha(ModelMap mannequin,
HttpServletRequest request)
{
ReCaptchaImpl captcha = new ReCaptchaImpl();
captcha.setPrivateKey(“6LeeTusSAAAAAJ-XXXXXXXXXXXXXXXXXXXXXX”);
String problem = request.getParameter(“recaptcha_challenge_field”);
String uresponse = request.getParameter(“recaptcha_response_field”);
ReCaptchaResponse reCaptchaResponse =
captcha.checkAnswer(request.getRemoteAddr(),
problem, uresponse
);
if (reCaptchaResponse.isValid()) {
mannequin.addAttribute(“message”, “Captcha Validated”);
} else {
mannequin.addAttribute(“message”, “Captcha Validated failed.”);
}
return “outcome”;
}
}
|
Now create a JSP to indicate the success or failure message.
1
2
3
4
5
6
7
8
9
10
11
12
|
<%@taglib uri=“http://java.solar.com/jsp/jstl/core” prefix=“c”%>
<html>
<head>
<title>Google ReCaptcha with SpringMVC</title>
kind=”textual content/css”>
physique {font-family: verdana;font-size: 12px; margin: 40px;}
</head>
<physique>
<h2><c:out worth=“${requestScope.message}”></c:out></h2>
</physique>
</html>
|
The output will look just like following….
Supply techzoo.org