使用 App Check 保护自定义后端资源(C++ 项目)
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
您可以使用 App Check 来保护应用的非 Google 自定义后端资源,例如您自己的自托管后端。为此,您需要执行以下两项操作:
- 修改您的应用客户端,以将 App Check 令牌随每个请求一起发送到后端,如本页所述。
- 按照从自定义后端验证 App Check 令牌中所述,修改后端以要求将有效的 App Check 令牌随每个请求一起发送。
准备工作
使用默认的提供程序将 App Check 添加到您的应用。
通过后端请求发送 App Check 令牌
为了确保您的后端请求包含有效且未过期的 App Check 令牌,请在每个请求前面添加一个 AppCheck::GetAppCheckToken()
调用。App Check 库会在必要时刷新令牌。
获取有效的令牌后,请将其随请求一起发送到后端。具体如何实现取决于您自己,但不要将 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.
}
}
}
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-14。
[null,null,["最后更新时间 (UTC):2025-08-14。"],[],[],null,["\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\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\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 }"]]