開始在 C++ 應用程式中使用 App Check
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
本頁說明如何使用
預設供應商:Android 上的 Play Integrity,以及裝置 Check 或 App Attest
Apple 平台。啟用 App Check 後,您就能確保只有您的應用程式可以存取專案的 Firebase 資源。查看
這項功能的總覽。
1. 設定 Firebase 專案
將 Firebase 新增至您的 C++ 專案 (如果尚未這麼做)
已完成其他驗證步驟
在 Play Integrity、Device Check、
或 App Attest 供應商
專案設定 >App Check
專區。
您通常需要註冊所有專案的應用程式,因為一旦啟用 Firebase 產品的強制執行功能,只有已註冊的應用程式才能存取產品的後端資源。
如要進一步瞭解如何向各個供應商註冊,請參閱
請參閱 Android 和 iOS 專屬說明文件
選用:在應用程式註冊設定中,為供應商核發的 App Check 權杖設定自訂存留時間 (TTL)。您可以設定存留時間
設為 30 分鐘至 7 天之間的任何值。變更這個值時,
瞭解下列優缺點:
- 安全性:較短的存留時間可提高安全性,因為這麼做能減少
出現安全漏洞,可能導致權杖遭到洩漏或攔截
攻擊者
- 效能:TTL 越短,應用程式執行認證的頻率就越高。由於應用程式認證程序每次執行時都會為網路要求增加延遲時間,因此 TTL 時間過短可能會影響應用程式效能。
- 配額與費用:縮短存留時間 (TTL) 和頻繁的重新認證作業,皆會破壞您的
配額更快,而如果是付費服務,費用可能更高。
請參閱「配額與限制」。
預設的存留時間對大多數應用程式來說都合理。請注意,App Check 程式庫會在 TTL 時間長度的約一半時間重新整理權杖。
2. 在應用程式中加入 App Check 程式庫
在一組依附元件中加入 App Check 程式庫,如下所示:
App Check 的設定操作說明。
3. 初始化 App Check
將下列初始化程式碼加入應用程式,讓應用程式在您使用任何 Firebase 服務 (包括建立 Firebase 應用程式) 前先執行。
Android
包含 firebase::app_check
的標頭檔案:
#include "firebase/app_check.h"
使用 Play Integrity 供應器初始化 App Check 程式庫:
firebase::app_check::AppCheck::SetAppCheckProviderFactory(
firebase::app_check::PlayIntegrityProviderFactory::GetInstance());
iOS+
包含 firebase::app_check
的標頭檔案:
#include "firebase/app_check.h"
使用裝置檢查或應用程式認證提供者初始化 App Check 程式庫:
firebase::app_check::AppCheck::SetAppCheckProviderFactory(
firebase::app_check::DeviceCheckProviderFactory::GetInstance());
後續步驟
在應用程式中安裝 App Check 程式庫後,即可開始發布
為使用者提供了最新的應用程式
更新後的用戶端應用程式會開始傳送 App Check 權杖和
但 Firebase 產品不需要權杖
您必須在
Firebase 控制台。
監控指標並啟用強制執行機制
不過,啟用違規處置前,請先確認這麼做不會影響現有的合法使用者。另一方面,如果您發現
如果您對應用程式資源都很可疑,不妨啟用強制執行功能
為做出這項決定,您可以查看 App Check 指標:
您使用的服務:
啟用強制執行 App Check
瞭解 App Check 對使用者的影響後,您就能做好準備
下一步,您可以啟用 App Check 強制執行功能:
在偵錯環境中使用 App Check
只要在應用程式註冊 App Check 後,
應用程式所在的環境中,App Check 通常不會歸類為有效應用程式。
例如電腦、開發期間的模擬器
您就能為應用程式建立偵錯版本
使用 App Check 偵錯供應商,而非真正的認證提供者。
請參閱「在 C++ 應用程式中搭配偵錯提供者使用 App Check」一文。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2024-10-16 (世界標準時間)。
[null,null,["上次更新時間:2024-10-16 (世界標準時間)。"],[],[],null,["# Get started using App Check in C++ apps\n\n\u003cbr /\u003e\n\nThis page shows you how to enable App Check in a C++ app, using the\ndefault providers: Play Integrity on Android, and Device Check or App Attest on\nApple platforms. When you enable App Check, you help ensure that\nonly your app can access your project's Firebase resources. See an\n[Overview](/docs/app-check) of this feature.\n\n1. Set up your Firebase project\n-------------------------------\n\n1. [Add Firebase to your C++ project](/docs/cpp/setup) if you haven't\n already done so.\n\n2. Register your apps to use App Check with the Play Integrity, Device Check,\n or App Attest providers in the\n [**Project Settings \\\u003e App Check**](https://console.firebase.google.com/project/_/appcheck)\n section of the Firebase console.\n\n You usually need to register all of your project's apps, because once you\n enable enforcement for a Firebase product, only registered apps will be able\n to access the product's backend resources.\n\n For detailed instructions on how to register with each provider, refer to\n the Android and iOS specific documentation.\n3. **Optional**: In the app registration settings, set a custom time-to-live\n (TTL) for App Check tokens issued by the provider. You can set the TTL\n to any value between 30 minutes and 7 days. When changing this value, be\n aware of the following tradeoffs:\n\n - Security: Shorter TTLs provide stronger security, because it reduces the window in which a leaked or intercepted token can be abused by an attacker.\n - Performance: Shorter TTLs mean your app will perform attestation more frequently. Because the app attestation process adds latency to network requests every time it's performed, a short TTL can impact the performance of your app.\n - Quota and cost: Shorter TTLs and frequent re-attestation deplete your quota faster, and for paid services, potentially cost more. See [Quotas \\& limits](/docs/app-check#quotas_limits).\n\n The default TTL\n is reasonable for most apps. Note that the App Check library refreshes\n tokens at approximately half the TTL duration.\n\n2. Add the App Check library to your app\n----------------------------------------\n\nInclude the App Check library in your set of dependencies, following\n[the setup instructions](/docs/cpp/setup#add-sdks) for App Check.\n\n3. Initialize App Check\n-----------------------\n\nAdd the following initialization code to your app so that it runs before you\nuse any Firebase services including any creation of Firebase Apps. \n\n### Android\n\n1. Include the header file for `firebase::app_check`:\n\n ```c++\n #include \"firebase/app_check.h\"\n ```\n2. Initialize the App Check library with the Play Integrity provider:\n\n firebase::app_check::AppCheck::SetAppCheckProviderFactory(\n firebase::app_check::PlayIntegrityProviderFactory::GetInstance());\n\n### iOS+\n\n1. Include the header file for `firebase::app_check`:\n\n ```c++\n #include \"firebase/app_check.h\"\n ```\n2. Initialize the App Check library with the Device Check or App Attest\n provider:\n\n firebase::app_check::AppCheck::SetAppCheckProviderFactory(\n firebase::app_check::DeviceCheckProviderFactory::GetInstance());\n\nNext steps\n----------\n\nOnce the App Check library is installed in your app, start distributing the\nupdated app to your users.\n\nThe updated client app will begin sending App Check tokens along with every\nrequest it makes to Firebase, but Firebase products will not require the tokens\nto be valid until you enable enforcement in the App Check section of the\nFirebase console.\n\n### Monitor metrics and enable enforcement\n\nBefore you enable enforcement, however, you should make sure that doing so won't\ndisrupt your existing legitimate users. On the other hand, if you're seeing\nsuspicious use of your app resources, you might want to enable enforcement\nsooner.\n\nTo help make this decision, you can look at App Check metrics for the\nservices you use:\n\n- [Monitor App Check request metrics](/docs/app-check/monitor-metrics) for Realtime Database, Cloud Firestore, and Cloud Storage.\n- [Monitor App Check request metrics for Cloud Functions](/docs/app-check/monitor-functions-metrics).\n\n### Enable App Check enforcement\n\nWhen you understand how App Check will affect your users and you're ready to\nproceed, you can enable App Check enforcement:\n\n- [Enable App Check enforcement](/docs/app-check/enable-enforcement) for Realtime Database, Cloud Firestore, and Cloud Storage.\n- [Enable App Check enforcement for Cloud Functions](/docs/app-check/cloud-functions).\n\n### Use App Check in debug environments\n\nIf, after you have registered your app for App Check, you want to run your\napp in an environment that App Check would normally not classify as valid,\nsuch as on desktop, an emulator during development, or from a continuous\nintegration (CI) environment, you can create a debug build of your app that\nuses the App Check debug provider instead of a real attestation provider.\n\nSee [Use App Check with the debug provider in C++ apps](/docs/app-check/cpp/debug-provider)."]]