保护 Firebase ML Apple 应用的 Cloud 凭据
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
如果您的 Apple 应用使用了 Firebase ML 的云端 API,那么在生产环境中发布该应用之前,应该采取一些额外措施来防止未经授权的 API 访问。
1. 缩小现有 API 密钥的范围
首先,配置现有的 API 密钥以禁止访问 Cloud Vision API:
打开 Google Cloud 控制台中的凭据页面。收到提示时,选择您的项目。
针对列表中的每个现有 API 密钥,打开修改视图。
在“API 限制”部分中,选择限制密钥,然后向列表中添加您希望此 API 密钥有权访问的所有 API。切勿包含 Cloud Vision API。
在配置 API 密钥的 API 限制时,您应明确声明该密钥有权访问的 API。默认情况下,如果在“API 限制”部分中选择“不限制密钥”,即表示一个 API 密钥可用于访问已为项目启用的所有 API。
现在,您现有的 API 密钥不会授予对云端机器学习服务的访问权限,但对于您添加到每个密钥的“API 限制”列表中的任何 API,仍可继续使用该密钥进行访问。
请注意,如果您日后启用任何其他 API,必须将其添加到相应 API 密钥的“API 限制”列表中。
2. 创建用于 Firebase ML 的新 API 密钥
接下来,为 Firebase ML 创建一个仅允许调用 Cloud Vision API 的新 API 密钥:
返回凭据页面。确保您的 Firebase 项目仍处于选中状态。
点击创建凭据 > API 密钥。记下新的 API 密钥,然后点击限制密钥。
在“API 限制”部分,选择限制密钥,然后仅仅向列表添加 Cloud Vision API。
此 API 密钥仅授予对 Cloud Vision API 的访问权限,可供 Firebase ML 用于访问基于云端的模型。
3. 建议:减少 Cloud Vision API 的配额
为了帮助缓解密钥泄露造成的影响,您应该更改 Cloud Vision API 的默认设置,以减少每位用户的配额。为此,请执行以下操作:
打开 Google Cloud 控制台的 Cloud Vision API 配额页面。收到提示时,选择您的项目。
在请求部分中,将 Requests per minute per user(每分钟每位用户的请求数)配额设置为适合您应用的合理值。例如,如果您的应用需要上传文档的图片以返回其文本,那么用户执行此操作的频率不会超过几秒钟一次,因此将配额设为 30-40 可能比较安全。
请注意,在此上下文中,“每位用户的请求数”是指从单个 IP 地址发出的请求。如果预计有多位用户从 NAT 后方同时使用您的应用,您可能需要考虑这一点。
4. 使用 Firebase ML API 密钥调用 Cloud API
最后,在您的应用中配置 Firebase ML 以使用新的 API 密钥。
由于 Firebase ML API 密钥允许对 Cloud Vision API 进行未经身份验证的访问,因此您必须做好密钥的保密工作,以防止未经授权的使用并避免系统因此向您的结算账号扣费。为此,您应避免在应用的二进制文件中包含 API 密钥,而应在应用运行时验证登录的是否为已知的可靠用户,然后再从服务器检索 API 密钥。
不过即使遵守这些做法,API 密钥也可能会被泄露。您应采取措施来帮助缓解密钥泄露造成的影响,例如,如上所述减少 API 的每用户配额、实施密钥轮替政策,以及向不同的用户组提供不同的密钥。
在您的应用安全获得 API 密钥后,当您想要调用 Firebase ML Cloud API 时,请指定相关密钥:
Swift
if let cloudVisionKey = getYourApiKey() { // See note above about securing your API key
let options = VisionCloudDetectorOptions()
options.apiKeyOverride = cloudVisionKey
let cloudDetector = Vision.vision().cloudLandmarkDetector(options: options)
}
Objective-C
NSString *cloudVisionKey = [self getYourApiKey]; // See note above about securing your API key
if (cloudVisionKey != nil) {
FIRVisionCloudDetectorOptions *options =
[[FIRVisionCloudDetectorOptions alloc] init];
options.APIKeyOverride = cloudVisionKey;
FIRVisionCloudLandmarkDetector *landmarkDetector =
[vision cloudLandmarkDetectorWithOptions:options];
}
此外,您还应遵循有关如何保护 API 密钥的一般建议。
后续步骤
如需了解在使用其他 Firebase 功能时的应用发布准备工作,请参阅发布核对清单。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-13。
[null,null,["最后更新时间 (UTC):2025-08-13。"],[],[],null,["\u003cbr /\u003e\n\nIf your Apple app uses one of Firebase ML's cloud APIs, before you launch your app\nin production, you should take some additional steps to prevent unauthorized\nAPI access.\n\n1. Reduce the scope of existing API keys\n\nFirst, configure your existing API keys to disallow access to the Cloud Vision\nAPI:\n\n1. Open the [Credentials](https://console.cloud.google.com/apis/credentials?project=_) page of the\n Google Cloud console. When prompted, select your project.\n\n2. For each existing API key in the list, open the editing view.\n\n3. In the *API restrictions* section, select **Restrict key** , then add to the\n list all of the APIs to which you want the API key to have access. Make sure\n to ***not*** include the Cloud Vision API.\n\n When you configure an API key's *API restrictions* , you are explicitly\n declaring the APIs to which the key has access. **By default, when the *API\n restrictions* section has *Don't restrict key* selected, an API key can be\n used to access any API that is enabled for the project.**\n\nNow, your existing API keys will not grant access to cloud ML services, but each\nkey will continue to work for any APIs that you added to its *API restrictions*\nlist.\n\nNote that if you enable any additional APIs in the future, you must add them to\nthe *API restrictions* list for the applicable API key.\n\n2. Create a new API key for use with Firebase ML\n\nNext, create a new API key for Firebase ML that only allows calls to the\nCloud Vision API:\n\n1. Return to the [Credentials](https://console.cloud.google.com/apis/credentials) page. Be sure your\n Firebase project is still selected.\n\n2. Click **Create credentials \\\u003e API key** . Take note of the new API key, then\n click **Restrict key**.\n\n3. In the *API restrictions* section, select **Restrict key** , then add to the\n list ***only*** the Cloud Vision API.\n\nThis API key grants access only to the Cloud Vision API and can be used by\nFirebase ML to access cloud-based models.\n\n3. Recommended: Reduce the Cloud Vision API's quota\n\nTo help mitigate the consequences of a compromised key, you should reduce the\nCloud Vision API's per-user quota from its default setting. To do so:\n\n1. Open the [Cloud Vision API Quotas](https://console.cloud.google.com/apis/api/vision.googleapis.com/quotas?project=_) page of the\n Google Cloud console. When prompted, select your project.\n\n2. In the **Requests** section, set the **Requests per minute per user** quota\n to something reasonable for your app. For example, if your app involves\n uploading a picture of a document to get back its text, it's unlikely that a\n user will do that more than once every few seconds, so a quota of 30-40\n would probably be safe.\n\n Note that in this context \"requests per user\" refers to requests from a\n single IP address. You might need to consider this if you expect multiple\n users to use your app at the same time from behind a NAT.\n\n4. Call Cloud APIs using your Firebase ML API key\n\nFinally, in your app, configure Firebase ML to use your new API key.\n\nBecause the Firebase ML API key allows unauthenticated access to the Cloud Vision\nAPI, it's important to keep the key confidential to prevent unauthorized use and\ncharges to your billing account. To do so, you should refrain from including\nyour API key in your app binary. Instead, at app runtime, verify that a known\ngood user is signed in, and only then, retrieve the API key from a server.\n\nEven when these practices are observed, it is possible for an API key to be\ncompromised. You should take steps to help mitigate the consequences of a\ncompromised key, such as reducing the API's per-user quota as described above,\nimplementing key rotation policies, and issuing different keys to different\ngroups of users.\n\nAfter your app has safely acquired the API key, when you want to call an\nFirebase ML Cloud API, specify the key: \n\nSwift \n\n if let cloudVisionKey = getYourApiKey() { // See note above about securing your API key\n let options = VisionCloudDetectorOptions()\n options.apiKeyOverride = cloudVisionKey\n let cloudDetector = Vision.vision().cloudLandmarkDetector(options: options)\n }\n\nObjective-C \n\n NSString *cloudVisionKey = [self getYourApiKey]; // See note above about securing your API key\n if (cloudVisionKey != nil) {\n FIRVisionCloudDetectorOptions *options =\n [[FIRVisionCloudDetectorOptions alloc] init];\n options.APIKeyOverride = cloudVisionKey;\n FIRVisionCloudLandmarkDetector *landmarkDetector =\n [vision cloudLandmarkDetectorWithOptions:options];\n }\n\nIn addition, you should follow the general advice in\n[Securing an API key](https://cloud.google.com/docs/authentication/api-keys#securing_an_api_key).\n\nNext steps\n\nSee the [launch checklist](/support/guides/launch-checklist) for information on\npreparing your app to launch when using other Firebase features."]]