我正在尝试为我的 Flutter 应用程序实现 reCaptcha 功能,但是在验证码注册中,我需要提供一个我没有用于移动应用程序的域。我浏览了几篇教如何将 reCaptcha 实现到移动应用程序的指南,但这些指南使用包名而不是域注册了他们的 reCaptcha。 2020 年在 Flutter 应用程序或任何移动应用程序中实现 reCaptcha 的正确方法是什么?
最佳答案
你可以使用这个插件,flutter_recaptcha .
对于域,我遇到了同样的问题。我首先发现我需要使用 here 中的“我不是机器人”复选框选项我不得不检查 github repository要查找此信息,“!!! 请记住将此域添加到 reCaptcha 设置中:recaptcha-flutter-plugin.firebaseapp.com”,它对此进行了解释。
在主页上没有看到它后,我迷路了一段时间,但现在它是有道理的。希望它有帮助。
编辑
尝试后我注意到了一些事情,我想提一下。该插件不提供用于对用户服务器端进行身份验证的验证码响应,因此它看起来不是很有用。但是,它是一个简单的插件,因此可以将其用作示例。我认为这些步骤是创建一个带有验证码的网页。和插件一样,使用webview打开页面,然后捕获表单的post输出和提交表单的用户的ip地址,using something like this ,然后 send it to flutter然后使用该信息提交您的请求,并使用 Google library验证验证码。
使用说明
我刚刚完成了这个,我找到了一个很好的方法。
首先,创建一个html页面,如下所示:
<html>
<head>
<title>reCAPTCHA</title>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
</head>
<body style='background-color: aqua;'>
<div style='height: 60px;'></div>
<form action="?" method="POST">
<div class="g-recaptcha"
data-sitekey="YOUR-SITE-KEY"
data-callback="captchaCallback"></div>
</form>
<script>
function captchaCallback(response){
//console.log(response);
if(typeof Captcha!=="undefined"){
Captcha.postMessage(response);
}
}
</script>
</body>
</html>
然后,将其托管在您的域中,例如 example.com/captcha。
然后,创建一个flutter Widget,如下所示:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
class Captcha extends StatefulWidget{
Function callback;
Captcha(this.callback);
@override
State<StatefulWidget> createState() {
return CaptchaState();
}
}
class CaptchaState extends State<Captcha>{
WebViewController webViewController;
@override
initState(){
super.initState();
}
@override
Widget build(BuildContext context) {
return Center(
child: WebView(
initialUrl: "https://example.com/captcha.html",
javascriptMode: JavascriptMode.unrestricted,
javascriptChannels: Set.from([
JavascriptChannel(
name: 'Captcha',
onMessageReceived: (JavascriptMessage message) {
//This is where you receive message from
//javascript code and handle in Flutter/Dart
//like here, the message is just being printed
//in Run/LogCat window of android studio
//print(message.message);
widget.callback(message.message);
Navigator.of(context).pop();
})
]),
onWebViewCreated: (WebViewController w) {
webViewController = w;
},
)
);
}
}
确保您在 https://www.google.com/recaptcha 注册了验证码 key (单击右上角的“管理控制台”)。
然后,您已经构建了前端。要调用验证码,只需运行:
Navigator.of(context).push(
MaterialPageRoute(
builder: (context){
return Captcha((String code)=>print("Code returned: "+code));
}
),
);
您可以使用任何您想要的回调,如下所示:
class GenericState extends State<Generic>{
void methodWithCaptcha(String captchaCode){
// Do something with captchaCode
}
@override
Widget build(BuildContext context) {
return Center(child:FlatButton(
child: Text("Click here!"),
onPressed: (){
Navigator.of(context).push(
MaterialPageRoute(
builder: (context){
return Captcha(methodWithCaptcha);
}
),
);
}
}
}
服务器端,可以按照说明here (我遵循了“直接下载”和“使用”部分)。我发现对于用法,我可以简单地使用代码:
$recaptcha = new \ReCaptcha\ReCaptcha($secret);
$resp = $recaptcha->verify($gRecaptchaResponse, $remoteIp);
if ($resp->isSuccess()) {
// Verified!
} else {
$errors = $resp->getErrorCodes();
}
使用
setExpectedHostname
,就像在这个例子中一样,是不必要的。之后,一切正常!我认为这是目前在 Flutter 中实现 Google reCaptcha V2 的最佳方式(适用于 iOS 和 Android)。
关于flutter - 如何将 reCaptcha 实现到 Flutter 应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60675575/