g_recaptcha_v3 套件是適用於 Flutter Web 的外掛程式,可將 Google reCAPTCHA v3(一種防垃圾訊息與濫用保護服務)整合至 Web 應用程式中。它能在無需使用者互動的情況下實現隱形挑戰驗證,協助區分人類使用者與機器人。該套件透過提供 Dart API,簡化了在客戶端取得評分與驗證回應的實作過程。此套件專為 Web 平台設計,可與 Flutter 的 Web 部署順暢搭配。
適用情境
保護網頁表單免受垃圾訊息侵擾
保護登入和註冊端點
防止自動化機器人攻擊
增強API端點的安全性
監控可疑用戶行為
主要特色
隱形 reCAPTCHA 集成
客戶端分數驗證
僅限網頁平台
簡單的 Dart API
支援伺服器端驗證
輕便且快速
適合對象
需要機器人保護的Flutter網頁應用
整合 Google reCAPTCHA v3 的開發者
需要非侵入式驗證碼的專案
注重安全的Web應用
前端開發人員使用驗證
使用建議
僅適用於網頁平台
需要後端驗證
需要 Google reCAPTCHA 站點密鑰
依賴互聯網連接
不適用於行動應用程式
原始 README
以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。
g_recaptcha_v3
Create Google reCAPTCHA v3 tokens for Flutter web. Google reCAPTCHA is a free service that protects your website from spam and abuse. Supports reCAPTCHA Enterprise, recaptcha.net, and WASM builds.
For development, add localhost as domain in reCaptcha console
Step 2
Add g_recaptcha_v3 to pubspec.yaml
flutter pub add g_recaptcha_v3
Optional: manual script tag
The plugin injects the reCAPTCHA script automatically — no index.html changes are needed. If you prefer to load it yourself (e.g. for an earlier load), add the script inside the web/index.html<head> tag, before the flutter.js / flutter_bootstrap.js script. The plugin detects the existing tag and will not add a second one.
<head><scriptsrc="https://www.google.com/recaptcha/api.js?render=<your Recaptcha v3 site key>"></script></head>
Development
1. GRecaptchaV3.ready()
The ready() method should be called before execute()
The ready() method should be called before execute()
import'package:g_recaptcha_v3/g_recaptcha_v3.dart';
voidgenerateToken() async {
String? token = awaitGRecaptchaV3.execute('<your_action>'); //--3print(token);
// send token to server and verify
}
String action - used to verify the string in backend. ( action docs )
token will be null if,
the web setup has any errors (see Error handling for diagnosable exceptions).
the method is called from a non-web platform.
execute() throws a GRecaptchaNotReadyException if ready() was never called.
3. Advanced options
bool ready = awaitGRecaptchaV3.ready(
"<your Recaptcha site key>",
showBadge: false, // hide the badge (see warning below)
useRecaptchaNet: true, // load script from recaptcha.net instead of google.com// (for regions where google.com is blocked, e.g. China)
useEnterprise: true, // use reCAPTCHA Enterprise (grecaptcha.enterprise)
badgeLanguage: 'fr', // badge language (hl param)
scriptNonce: '<nonce>', // CSP nonce for the injected <script> tag
);
// check readiness anywhereif (GRecaptchaV3.isReady) { ... }
4. Error handling
By default execute() returns null on failure. For diagnosable errors:
try {
String? token = awaitGRecaptchaV3.execute('login', throwOnError: true);
} onGRecaptchaNotReadyException {
// ready() was not called or did not succeed
} onGRecaptchaExecutionExceptioncatch (e) {
// the underlying grecaptcha.execute() call failedprint(e.message);
}