带ColdFusion的reCaptcha v3

scg*_*ugh 5 coldfusion recaptcha

我正在尝试将reCaptcha(v3)集成到ColdFusion站点。我对CF语法不太热,目前看来服务器端的验证请求没有任何帮助。

谁能看到任何明显错误的东西和/或将我指向正确的方向?

客户端:

<script src='https://www.google.com/recaptcha/api.js?render=6..."></script>
<script>
    grecaptcha.ready(function() {
        grecaptcha.execute('6...', {action: 'contact'})
        .then(function(token) {
            $("#recaptchaToken").val(token);
        });
    });
</script>
Run Code Online (Sandbox Code Playgroud)

我的recaptchaToken表单中有一个隐藏字段,可以看到其中的token值。

服务器端:

<cfhttp
  url="https://www.google.com/recaptcha/api/siteverify"
  method="POST"
  result="captchaResponse">
  <cfhttpparam
    type="formfield"
    name="secret"
    value='6...'
  />
  <cfhttpparam
    type="formfield"
    name="response"
    value='#form.recaptchaToken#'
  />
</cfhttp>

<cfdump var=#captchaResponse.filecontent# />
Run Code Online (Sandbox Code Playgroud)

我得到一个红色框输出,标题为 object of java.io.ByteArrayOutputStream

我企图把两者captchaResponsecaptchaResponse.filecontent没有用。

我期望的数据形式为:

{
  "success": true|false,      // whether this request was a valid reCAPTCHA token for your site
  "score": number             // the score for this request (0.0 - 1.0)
  "action": string            // the action name for this request (important to verify)
  "challenge_ts": timestamp,  // timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)
  "hostname": string,         // the hostname of the site where the reCAPTCHA was solved
  "error-codes": [...]        // optional
}
Run Code Online (Sandbox Code Playgroud)

更新资料

该解决方案似乎如下所示:

<cfdump var=#toString(captchaResponse.filecontent)# />
Run Code Online (Sandbox Code Playgroud)

这为我提供了预期格式的JSON字符串,因此我可以将其转换为对象并完成验证。

Ale*_*lex 4

每当cfhttp不确定如何处理响应时,原始内容保持不变并保留为字节数组。这通常表明响应服务器未指定 Content-Type 标头,或者仅检索了部分内容。

要强制内容的字符串表示形式,您可以使用toString()转换原始字节数组,例如toString(captchaResponse.filecontent)。该函数非常强大,还可以处理已经转换的字符串,因此通常可以安全使用。

然而,这里还有一些事情需要注意。当使用时cfhttp不将该属性设置throwOnErrortrue(默认值为false)时,失败的 HTTP 请求仍然会返回一个结果,一个残缺的结果。该结构不会包含fileContent密钥,因此会在运行时引发异常。您可能需要在此处添加错误处理,以防https://www.google.com/recaptcha/api/siteverify无法访问或您的 JRE 不支持接受的 TLS 协议。我们在以前版本的 ColdFusion(即 8)中遇到了 SNI 和 TLS 1.2 的问题。请注意。