在 C++ 專案中使用 App Check 保護自訂後端資源
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
您可以透過 App Check 保護應用程式的非 Google 自訂後端資源,例如自行管理的後端。如要這麼做,請完成下列兩項操作:
- 按照本頁說明,修改應用程式用戶端,在每次向後端發出要求時,一併傳送應用程式檢查權杖。
- 按照「從自訂後端驗證 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.
}
}
}
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-23 (世界標準時間)。
[null,null,["上次更新時間:2025-08-23 (世界標準時間)。"],[],[],null,["# Protect custom backend resources with App Check in C++ projects\n\n\u003cbr /\u003e\n\nYou can use App Check to protect non-Google custom backend resources for\nyour app, like your own self-hosted backend. To do so, you'll need to do both of\nthe following:\n\n- Modify your app client to send an App Check token along with each request to your backend, as described on this page.\n- Modify your backend to require a valid App Check token with every request, as described in [Verify App Check tokens from a custom backend](/docs/app-check/custom-resource-backend).\n\nBefore you begin\n----------------\n\nAdd App Check to your app, using the [default providers](/docs/app-check/cpp/default-providers).\n\nSend App Check tokens with backend requests\n-------------------------------------------\n\nTo ensure your backend requests include a valid, unexpired, App Check token,\nprecede each request with a call to `AppCheck::GetAppCheckToken()`. The App\nCheck library will refresh the token if necessary.\n\nOnce you have a valid token, send it along with the request to your backend. The\nspecifics of how you accomplish this are up to you, but *don't send\nApp Check tokens as part of URLs*, including in query parameters, as this\nmakes them vulnerable to accidental leakage and interception. The recommended\napproach is to send the token in a custom HTTP header.\n\nFor example: \n\n void CallApiExample() {\n firebase_app_check::AppCheck* app_check = firebase::app_check::AppCheck::GetInstance();\n Future\u003cstd::string\u003e app_check_future = app_check-\u003eGetAppCheckToken(false);\n app_check_future.OnCompletion([&](const Future\u003cstd::string\u003e& future_token) {\n if (future_token.result()) {\n // Got a valid App Check token. Include it in your own http calls.\n }\n }\n }"]]