如何在 ColdFusion 中实现 recaptcha v3?
How to implement recaptcha v3 in ColdFusion?
首先调用 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 与 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”
希望这对某人有所帮助。如果这不是您要找的,请纠正我。
首先调用 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 与 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
.
希望这对某人有所帮助。如果这不是您要找的,请纠正我。