[英]recaptcha integration with Spring MVC
我正在嘗試將recaptcha集成到我的spring mvc應用程序中。 下面是代碼。
@RequestMapping(value="home.htm", method=RequestMethod.POST)
public String processEmailLogin(
@ModelAttribute("examLoginForm") ExamLoginDetails examLoginDetails,
BindingResult result,
Model model,
@RequestParam("recaptcha_challenge_field") String challangeField,
@RequestParam("recaptcha_response_field") String responseField,
HttpServletRequest request, SessionStatus sessionStatus) {
ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
reCaptcha.setPrivateKey("6LcWOOsSAAAAAM48WFr4PfE0Y1LdTJHKC_BxILwl");
/*String challenge = request.getParameter("recaptcha_challenge_field");
String response = request.getParameter("recaptcha_response_field");*/
String remoteAddr = request.getRemoteAddr();
logger.info("Remote machine IP address is : "+remoteAddr);
String emailIdForm = request.getParameter("email");
ReCaptchaResponse reCaptchaResponse = reCaptchaService.checkAnswer(remoteAddr, challangeField, responseField);
logger.info("Shown captcha is : "+challangeField);
logger.info("Entered captcha is : "+responseField);
logger.info("The validated recaptcha response is : "+reCaptchaResponse.isValid());
boolean correctAnswer = false;
correctAnswer = challangeField.equalsIgnoreCase(responseField);
logger.info("Correct answer : "+correctAnswer);
logger.info(":::::: LOADED Login Form CONTROLLER ::::::");
if(!reCaptchaResponse.isValid()) {
model.addAttribute("message", "wrong captcha");
logger.info("Incorrect captcha");
return "login/login";
}
else {
logger.info("::::: Checking captcha response to validate :::::");
model.addAttribute("message", "correct captcha");
LoginService loginService = (LoginService) ctx.getBean("loginService");
examLoginDetails = loginService.performLogin(emailIdForm);
logger.info("Email id is ::::::::::: "+examLoginDetails.getEmailId());
return "home/home";
}
}
我的JSP表格如下:
<%@ page import="net.tanesha.recaptcha.ReCaptcha" %>
<%@ page import="net.tanesha.recaptcha.ReCaptchaFactory" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="./jsp/scripts/jquery-1.9.1.js"></script>
<script type="text/javascript" src="./jsp/scripts/jquery-ui-1.10.3.custom.min.js"></script>
<link rel="stylesheet" type="text/css" href="./jsp/styles/jquery-ui-1.10.3.custom.min.css" />
<link rel="stylesheet" type="text/css" href="./jsp/styles/page-style.css" />
<link rel="stylesheet" type="text/css" href="./jsp/styles/tooltip.css" />
</head>
<body>
<form action="home.htm" method="post" name="examLoginForm">
<fieldset class="ui-widget ui-widget-content ui-corner-all">
<legend class="ui-widget-header ui-corner-all">Online Examination</legend>
<table class="ui-widget ui-helper-clearfix">
<tr>
<td><label for="email">Please enter your Email Id : </label></td>
<td><input class="ui-widget-content ui-corner-all" type="text" id="emailId" name="email" title="Please provide the email id you used to register with us to take the exam." ></td>
</tr>
<tr>
<td></td>
<td>
<script type="text/javascript" src="http://www.google.com/recaptcha/api/challenge?k=PUBLIC_KEY"></script>
<noscript>
<iframe src="http://www.google.com/recaptcha/api/noscript?k=PUBLIC_KEY" height="300" width="500" frameborder="0"></iframe><br>
<textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
<input type="hidden" name="recaptcha_response_field" value="manual_challenge">
</noscript>
<%-- <%
ReCaptcha c = ReCaptchaFactory.newReCaptcha("PUBLIC_KEY","PRIVATE_KEY", false);
out.print(c.createRecaptchaHtml(null, null));
%> --%>
</td>
<td></td>
</tr>
<tr>
<td></td>
<td><span id="LoginBtn"><input class="ui-helper-clearfix ui-corner-all ui-button ui-button-text ui-state-default ui-state-focus ui-state-active ui-state-hover" type="submit" id="login" value="Take Exam" /></span></td>
<td></td>
</tr>
</table>
</fieldset>
</form>
<div id="dialog" title="Confirm Your Email ID">
<p><span class="ui-widget ui-widget-content ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>Are you sure that this is the Email Id you have used to register yourself with us?</p>
</div>
</body>
</html>
Spring應用程序上下文中的Bean配置為:
<bean id="reCaptchaService" class="net.tanesha.recaptcha.ReCaptchaImpl">
<property name="privateKey" value="PRIVATE_KEY" />
</bean>
我在控制台上看到的輸出是:
13:12:56,875 INFO LoginFormController:58 - Remote machine IP address is : 10.129.75.57
13:12:57,078 INFO LoginFormController:63 - Shown captcha is : 03AHJ_VuuVrDXTjd0tBEkuONNedE6Bk214GxuOj7DT0o927e5HNgS_IKX7Efuc79liUvsH8VbKbZ7aZ8iElpJog6YqjsvThFu3BuULzPGEPHZKiIk_hnix6N_IXC3aDQaEcBDTPgooOuqs_CwriWe8PuxrzfbzDS2QdnhLuiWqIVqlX2KnZT9BZYo
13:12:57,078 INFO LoginFormController:64 - Entered captcha is : 44642526 2154
13:12:57,079 INFO LoginFormController:65 - The validated recaptcha response is : false
13:12:57,079 INFO LoginFormController:70 - Correct answer : false
13:12:57,079 INFO LoginFormController:71 - :::::: LOADED Login Form CONTROLLER ::::::
13:12:57,079 INFO LoginFormController:79 - ::::: Checking captcha response to validate :::::
我從控制台輸出中看到的是recaptcha_challange_field包含某種加密形式的驗證碼問題,而recaptcha_response_field包含純文本形式的驗證碼問題的響應。 那么,難道是Recaptcha checkAnswer(remoteAddr,挑戰,響應)方法始終將其評估為false的原因嗎? 請幫助我,因為我很長一段時間以來一直困擾於此問題。
對我而言,recaptcha-spring-boot-starter非常有用,它可以大大減少您的代碼:
https://github.com/mkopylec/recaptcha-spring-boot-starter-samples
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.