验证码使用Java代码验证登录页面

时间:2018-07-16 10:27:04

标签: java jsp captcha

我尝试使用Google Recaptcha代码在JSP和Servlet代码中使用验证码登录页面。我收到错误消息,因为“ Recaptcha V1已关闭”,如下所示:

enter image description here

用于验证码的代码:

JSP页面

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%@ page import="net.tanesha.recaptcha.ReCaptcha" %>
<%@ page import="net.tanesha.recaptcha.ReCaptchaFactory" %>

<html>
<head>
<title>CAPTCHA in Java using reCAPTCHA</title>
</head>
<body>
<h2>CAPTCHA in Java Application using reCAPTCHA</h2>
<form action="validate.jsp" method="post">
<p>Username: <input type="text" name="user"></p>
<p>Password: <input type="password" name="password"></p>
<p>
<%
    ReCaptcha c = ReCaptchaFactory.newReCaptcha(
                "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", 
                "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy", false);
    out.print(c.createRecaptchaHtml(null, null));
%>
   <input type="submit" value="submit" />
</p>        
        </form>
</body>
</html>

Validate.Jsp

  <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <%@ page import="net.tanesha.recaptcha.ReCaptchaImpl"%>
    <%@ page import="net.tanesha.recaptcha.ReCaptchaResponse"%>

    <html>
<head>
<title>CAPTCHA in Java using reCAPTCHA</title>
</head>
<body>
<h2>CAPTCHA in Java Application using reCAPTCHA</h2>
<p>
    <%
        String remoteAddr = request.getRemoteAddr();
        ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
        reCaptcha.setPrivateKey("xxxxxxxxxxxxxxxxxxxx");

        String challenge = request
                .getParameter("recaptcha_challenge_field");
        String uresponse = request.getParameter("recaptcha_response_field");
        ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(
                remoteAddr, challenge, uresponse);

        if (reCaptchaResponse.isValid()) {
            String user = request
                    .getParameter("user");
            out.print("CAPTCHA Validation Success! User "+user+" registered.");
    } else {
            out.print("CAPTCHA Validation Failed! Try Again.");
        }
    %>
</p>
<a href=".">Home</a>    
</body>
</html>

请帮助我解决问题。

1 个答案:

答案 0 :(得分:0)

该问题是由于Google不推荐使用reCaptcha v.1-您需要升级,如相应的Google documentation page中所述。这可能意味着您必须交换另一个客户端库。简短的答案是您无法继续使用现有设置。