我尝试将下载链接插入到带有 reCaptcha 的表单中(按照 Google 的说明),但即使您没有通过验证码挑战,提交按钮也会打开链接。
代码很简单:
<head>
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body>
<form action="download link">
<input type="submit" value="download">
<div class="g-recaptcha" data-sitekey="xxx"></div>
</form>
</body>
有人知道为什么这不起作用吗?谢谢!
最佳答案
我认为您对 Google reCaptcha 的工作原理有点困惑 - 它不会阻止 POST(用户可以轻松绕过此类操作),它用于允许服务器端代码来检查用户不是机器人。
这意味着您还必须在服务器端有一些东西来检查正在提交的内容。你不能只在客户端完成这一切。 (虽然看起来 Google 正在客户端完成所有操作,但 reCaptcha 按钮实际上位于另一台服务器上的 iframe 中。)
例如。请在此处查看 Google 的演示:https://www.google.com/recaptcha/api2/demo
请注意,当您单击“提交”时,它仍然会将数据 POST 回服务器 - 服务器会响应判断您是否是人类。
正如 Google 的文档所述:
When your users submit the form where you integrated reCAPTCHA, you'll get as part of the payload a string with the name "g-recaptcha-response". In order to check whether Google has verified that user, send a POST request with these parameters:
URL: https://www.google.com/recaptcha/api/siteverify
secret (required) xxx
response (required) The value of 'g-recaptcha-response'.
remoteip The end user's ip address.
您基本上需要检查 POST 请求 secret
是否与您的 Recaptcha 帐户中的 key 匹配。如果是,那么您应该向用户提供下载链接,如果没有,则返回错误消息。
您可以在 reCaptcha 文档中了解有关此过程的更多信息:https://developers.google.com/recaptcha/docs/verify
更新:如果您不关心有人能够伪造结果,您可以使用 JavaScript 来做到这一点,如下所示:JSFiddle
关于html - 使用 Google 的 reCaptcha 保护下载链接免受机器人攻击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34092288/