java - 使用 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>
请帮我解决问题。
解决方案
该问题是由于 Google 已弃用 reCaptcha v.1 - 您需要升级,如相应的Google 文档页面所述。这可能意味着您必须更换另一个客户端库。简短的回答是您无法继续使用现有设置。
推荐阅读
- json - 如何从 django 请求中获取分钟/小时/秒?
- java - 如何为 Java 中的 String 对象生成 hashCode()?
- .htaccess - 将所有重定向到 ssl,管理面板除外
- php - 登录并在php中注册没有响应
- python - 如何通过生成两个时间段之间的日期范围来填充数据框中缺失的日期值
- json - 如何从 iOS Swift 中的 JSON 响应中获取数据?
- kubernetes - Helm - Configmap - 仅替换文件的内容而不是文件名
- sql - 根据销售数量选择每个国家/地区最受欢迎的时间
- python - 为什么 python 请求没有终止,为什么打印这些单独的日志?
- javascript - 如何找到在模态输入中键入的值并在按下回车键时更改另一个输入的值