在 C++ 專案中使用 App Check 保護自訂後端資源

您可以使用 App Check 保護應用程式的非 Google 自訂後端資源,例如您自己的自行管理後端。如要這樣做,您必須同時完成下列兩項操作:

  • 修改應用程式用戶端,以便在每個要求傳送至後端時,一併傳送 App Check 權杖,如本頁所述。
  • 修改後端,要求每項要求都必須提供有效的 App Check 權杖,如「從自訂後端驗證 App Check 權杖」一文所述。

事前準備

使用預設供應商,在應用程式中新增 App Check。

透過後端要求傳送 App Check 權杖

為確保後端要求包含有效且未過期的 App Check 權杖,請在每項要求前呼叫 AppCheck::GetAppCheckToken()。應用程式檢查程式庫會視需要重新整理權杖。

取得有效的權杖後,請將權杖連同要求傳送至後端。具體做法由您決定,但請勿將 App Check 權杖當作網址的一部分傳送,包括在查詢參數中,否則可能會發生意外洩漏和攔截的情形。建議您透過自訂 HTTP 標頭傳送權杖。

例如:

void CallApiExample() {
    firebase_app_check::AppCheck* app_check = firebase::app_check::AppCheck::GetInstance();
    Future<std::string> app_check_future = app_check->GetAppCheckToken(false);
    app_check_future.OnCompletion([&](const Future<std::string>& future_token) {
        if (future_token.result()) {
            // Got a valid App Check token. Include it in your own http calls.
        }
    }
}

Firebase gives you the tools and infrastructure you need to build better mobile and web apps, improve app quality, and grow your business.

更新時間:Feb 28, 2025

Firebase Authentication lets you add an end-to-end identity solution to your app for easy user authentication, sign-in, and onboarding in just a few lines of code.

更新時間:Feb 28, 2025

Firebase Authentication lets you add an end-to-end identity solution to your app for easy user authentication, sign-in, and onboarding in just a few lines of code.

更新時間:Feb 28, 2025