coldfusion - 如何在 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
未定义。相关问题:带有 ColdFusion 的 reCaptcha v3
解决方案
也许来晚了,但如果找不到可行的解决方案,以下内容可能会有所帮助(或对未来的读者)。
似乎想要的是使用带有 Coldfusion 的 reCaptcha v3 的代码。这是一个简单的文件 (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 语句中的“0.5” Return.score GT 0.5
。
希望这可以帮助某人。如果那不是您想要的,请纠正我。
推荐阅读
- java - 错误:类 kotlin.reflect.jvm.internal.FunctionCaller$FieldSetter
- spring - Spring Mongo Repository - 在 mongo Repository 上实现软删除的最佳方法?
- c - 指针和指针位置的区别
- python - 在 Python 中调用函数进行测试时出现问题?
- c# - 为什么 HttpClient 不返回 302 代码?
- wpf - 在 MouseOver XAML (WPF) 上更改按钮上的图像
- r - 使用 plotly::api_create 和 ggplot2::geom_sf 函数时文件太大
- c - 数组的二维动态分配并将其作为参数传递的问题
- java - 如何在命令行上指定密钥库类型?
- php - 如何从具有多个输入的动态添加的行中存储数据