简体   繁体   English

与Spring MVC重新集成

[英]recaptcha integration with Spring MVC

I am trying to integrate recaptcha into my spring mvc application. 我正在尝试将recaptcha集成到我的spring mvc应用程序中。 Below is the code. 下面是代码。

@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";
    }
}

My JSP form is as follows: 我的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>

The bean configuration in the spring application context is : Spring应用程序上下文中的Bean配置为:

<bean id="reCaptchaService" class="net.tanesha.recaptcha.ReCaptchaImpl">  
    <property name="privateKey" value="PRIVATE_KEY" />  
</bean>

The output i see on my console is : 我在控制台上看到的输出是:

    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 :::::

What i see from the console output is that the recaptcha_challange_field contains the captcha question in some encripted form and the recaptcha_response_field contains the response for the captcha question in plain text form. 我从控制台输出中看到的是recaptcha_challange_field包含某种加密形式的验证码问题,而recaptcha_response_field包含纯文本形式的验证码问题的响应。 So, is it the reason that the recaptcha checkAnswer(remoteAddr, challange, response) method is always evaluating to false?. 那么,难道是Recaptcha checkAnswer(remoteAddr,挑战,响应)方法始终将其评估为false的原因吗? Please help me as i am stuck with this problem from a long time now. 请帮助我,因为我很长一段时间以来一直困扰于此问题。

For me recaptcha-spring-boot-starter was very helpful and reduces your code a lot: 对我而言,recaptcha-spring-boot-starter非常有用,它可以大大减少您的代码:

https://github.com/mkopylec/recaptcha-spring-boot-starter-samples https://github.com/mkopylec/recaptcha-spring-boot-starter-samples

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM