我正在尝试为我的 Flutter 应用程序实现 reCaptcha 功能,但是在验证码注册中,我需要提供一个我没有用于移动应用程序的域。我浏览了几篇教如何在移动应用程序中实现 reCaptcha 的指南,但这些指南使用包名而不是域注册了他们的 reCaptcha。2020 年在 Flutter 应用程序或任何移动应用程序中实现 reCaptcha 的正确方法是什么?
你可以使用这个插件flutter_recaptcha。
对于域,我遇到了同样的问题。我第一次发现,我需要用“我不是机器人”的复选框选项在这里,我不得不检查的github仓库找到这些信息,“ !!!记住这个域名添加到reCAPTCHA的设置:recaptcha- flutter-plugin.firebaseapp.com, ”解释了它。
在主页上没有看到它后,我迷路了一段时间,但现在它是有道理的。希望它有帮助。
编辑
尝试后我注意到了一些事情,我想提一下。该插件不提供用于对用户服务器端进行身份验证的验证码响应,因此它看起来不是很有用。但是,它是一个简单的插件,因此可以将其用作示例。我认为这些步骤是创建一个带有验证码的网页。与插件一样,使用 webview 打开页面,然后捕获表单的 post 输出和提交表单的用户的 ip 地址,使用类似这样的东西,然后将其发送到 flutter,然后使用该信息提交您的请求,以及使用Google 库来验证验证码。
指示
我刚刚完成了这个,我找到了一个很好的方法。首先,创建一个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>
Run Code Online (Sandbox Code Playgroud)
然后,将其托管在您的域中,例如 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;
},
)
);
}
}
Run Code Online (Sandbox Code Playgroud)
确保您在https://www.google.com/recaptcha上注册了验证码(点击右上角的“管理控制台”)。
然后,您已经构建了前端。要调用验证码,只需运行:
Navigator.of(context).push(
MaterialPageRoute(
builder: (context){
return Captcha((String code)=>print("Code returned: "+code));
}
),
);
Run Code Online (Sandbox Code Playgroud)
您可以使用任何您想要的回调,如下所示:
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);
}
),
);
}
}
}
Run Code Online (Sandbox Code Playgroud)
服务器端,您可以按照此处的说明操作(我按照“直接下载”和“使用”部分进行操作)。我发现对于用法,我可以简单地使用代码:
$recaptcha = new \ReCaptcha\ReCaptcha($secret);
$resp = $recaptcha->verify($gRecaptchaResponse, $remoteIp);
if ($resp->isSuccess()) {
// Verified!
} else {
$errors = $resp->getErrorCodes();
}
Run Code Online (Sandbox Code Playgroud)
使用setExpectedHostname,如在本例中,是不必要的。
之后,一切正常!我认为这是目前在 Flutter 中实现 Google reCaptcha V2 的最佳方式(适用于 iOS 和 Android)。
| 归档时间: |
|
| 查看次数: |
8968 次 |
| 最近记录: |