首页 > 解决方案 > 使用 Java 代码验证登录页面的验证码

问题描述

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

在此处输入图像描述

用于验证码的代码:

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>

验证.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>

请帮我解决问题。

标签: javajspcaptcha

解决方案


该问题是由于 Google 已弃用 reCaptcha v.1 - 您需要升级,如相应的Google 文档页面所述。这可能意味着您必须更换另一个客户端库。简短的回答是您无法继续使用现有设置。


推荐阅读