如何在ColdFusion中实现recaptcha v3?

时间:2019-07-31 17:41:11

标签: coldfusion recaptcha-v3

首先致电Google以从contact_m.cfm获取令牌,然后将其提交到同一页面进行验证。接下来使用ColdFusion调用保护密钥。远程调用该函数以调用ColdFusion函数。按照已实现的ColdFusion进行渲染,因此不会寻找更改。

grecaptcha.ready(function() {
    grecaptcha.execute('token', {action: 'contact'}).then(function(token) {
    $.ajax(
        {
        url: "./contact_m.cfm", 
        type: "post", 
        contentType: "application/json",
        data: JSON.stringify( {googleToken: token} ),
        success: function(result){
            $.get('./contact_m.cfm?func=googleVerification', function (r) {
            });
        }
    });
});
});

通过Google验证令牌

<cffunction access="public" name="googleVerification"> 
    <cfargument required="true" type="any" name="myArgument"> 
    <cfset requestBody = toString( getHttpRequestData().content ) />

    <cfif  isJSON( requestBody )>
        <cfset token = DeserializeJSON(#requestBody#)/>

        <cfhttp method="post" url="https://www.google.com/recaptcha/api/siteverify" result="googleResult">
            <cfhttpparam name="secret" type="formField" value="6Lf9IrAUAAAAAOhEdBvk1ZyIKX6eUqS06GaSXG_F">
            <cfhttpparam name="response" type="formField" value="#token.googleToken#">
        </cfhttp>

        <cfset googleResponse = DeserializeJSON(#googleResult.FileContent#)/>
        <cfset isHuman = #googleResponse.success#/>
    </cfif>
</cffunction>

和JavaScript函数可检查Google是否成功:

<script>
        function validateHuman(){
        <cfoutput>
            var #toScript(isHuman, "isHuman")#;
        </cfoutput> 
        console.log(isHuman);

        if (isHuman == 'YES') {
            return true;
        } else return false;
    }
</script>

如果Google验证,则允许用户提交表单

<form id="form3" action="contact_m.cfm" method="post" onsubmit="return validateHuman();">

我收到错误消息:isHuman未定义。 相关问题:reCaptcha v3 with ColdFusion

1 个答案:

答案 0 :(得分:1)

可能在这里晚了,但是如果找不到可行的解决方案,则以下内容可能会有所帮助(或对将来的读者而言)。

似乎需要使用reCaptcha v3和Coldfusion的代码。这是一个简单的文件(form.cfm)表单,该表单使用v3验证是否有人在处理该表单。您可以针对自己的特定目的对其进行扩展。

这些行进入了Application.cfm或Application.cfc文件

<cfset application.SiteKey = "_Your_Site_Key_from_Google_">
<cfset application.SecretKey = "_Your_Secret_Key_from_Google_">

这些行保存在我称为form.cfm的文件中。

<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://www.google.com/recaptcha/api.js?render=<cfoutput>#application.SiteKey#</cfoutput>"></script>
</head>
<body>

<cfif ISDEFINED('FORM.FirstName')> <!--- check if form was submitted and if so run code below --->

    <cfhttp url="https://www.google.com/recaptcha/api/siteverify?secret=#application.SecretKey#&response=#FORM['g-recaptcha-response']#" result="Response" />
    <cfset Return = deserializeJSON(Response.FileContent) />

    <cfif Return.success IS 'true' AND Return.score GT 0.5> <!--- check if true and if score is greater than 0.5. Run code below if all good. --->

        <cfoutput>Human: #FORM.FirstName# #FORM.LastName#</cfoutput>
        <!--- you can do database entry and/or email results here --->

    <cfelse>  <!--- if not a human, do this. I usually remove the else part completely, but if you need to do something with the robot, do it here.  --->

        Most likely a robot.

    </cfif>

<cfelse> <!--- show form --->

    <form method="post" action="/form.cfm">  <!--- submit form back to itself --->
      First Name: <input name="FirstName" type="text"><br>
      Last Name: <input name="LastName" type="text"><br>
      <input name="submit" type="submit">
      <input name="g-recaptcha-response" id="g-recaptcha-response" type="hidden" /> <!--- javascript below gives this a value from google. --->
    </form>

    <script>
    grecaptcha.ready(function() {
        grecaptcha.execute('<cfoutput>#application.SiteKey#</cfoutput>', {action: 'homepage'})
            .then(function(token) {
                document.getElementById('g-recaptcha-response').value=token;
            });
        });
    </script>

</cfif>

</body>
</html>

这是本教程从PHP到CF的改编:https://www.youtube.com/watch?v=zGNH_lbpmm8

如果在表单上使用此功能时出现很多误报,请增加可接受的分数(0.6或更高...最高1.0)。不要太高,否则您将过滤掉合法提交的内容。此#会替换cfif语句Return.score GT 0.5中的“ 0.5”。

希望这对某人有帮助。如果那不是您想要的,请纠正我。