我正在尝试将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
我企图把两者captchaResponse
并captchaResponse.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字符串,因此我可以将其转换为对象并完成验证。
每当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)中遇到了这个问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句