带ColdFusion的reCaptcha v3

时间:2019-01-29 23:43:47

标签: 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>

我的表单中有一个隐藏字段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# />

我得到一个标题为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
}

更新

解决方案似乎是亚历克斯建议的:

<cfdump var=#toString(captchaResponse.filecontent)# />

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

1 个答案:

答案 0 :(得分:3)

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

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

但是,这里还有其他需要注意的地方。当使用cfhttp而不将throwOnError属性设置为true(默认值为false)时,失败的HTTP请求仍将返回结果,即残废的结果。该结构将不包含fileContent键,因此会在运行时导致异常。如果无法访问https://www.google.com/recaptcha/api/siteverify或JRE不支持接受的TLS协议,则可能需要在此处添加错误处理。我们在SNI和TLS 1.2的旧版本ColdFusion(即8)中遇到了这个问题。请注意。