保护 Firebase ML Android 应用的 Cloud 凭据
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
如果您的 Android 应用使用了 Firebase ML 的某个云端 API,那么在生产环境中发布该应用之前,应该采取一些额外步骤来防止未经授权的 API 访问。
对于您的正式版应用,您应确保只有经过身份验证的客户端才能访问云端服务。(请注意,只有未启用 root 权限的设备才能使用所述方法进行身份验证。)
另外,您要创建一个仅用于调试的 API 密钥,以便在测试和开发期间使用。
1. 在 Firebase 中注册正式版应用
首先,在 Firebase 中注册您的正式版应用。
确保您拥有应用的 SHA-1 签名。请参阅对客户端进行身份验证,了解具体操作方法。
在 Firebase 控制台中,依次点击 settings 和“项目设置”,然后选择“设置”标签页。
向下滚动到“您的应用”卡片,然后选择您的 Android 应用。
将应用的 SHA-1 签名添加到应用的信息中。
2. 限制 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 限制”列表中。
3. 创建并使用仅用于调试的 API 密钥
最后,创建一个仅用于开发的新 API 密钥。Firebase ML 可以使用此 API 密钥在无法进行应用身份验证的环境中访问 Google Cloud 服务,例如在模拟器上运行时。
创建一个用于开发的 API 密钥:
打开 Google Cloud 控制台中的凭据页面。收到提示时,选择您的项目。
依次点击创建凭据 > API 密钥,并记下新的 API 密钥。此密钥允许从未经身份验证的应用访问 API,因此请勿泄露此密钥。
为确保新的调试 API 密钥不会在发布的应用中泄露,请在仅用于调试 build 的 Android 清单文件中指定调试 API 密钥:
如果您还没有调试清单,请创建一个,只需依次点击文件 > 新建 > 其他 > Android 清单文件,并从目标源代码集中选择 debug
即可。
在调试清单中,添加以下声明:
<application>
<meta-data
android:name="com.firebase.ml.cloud.ApiKeyForDebug"
android:value="your-debug-api-key" />
</application>
在您的应用中,配置 Firebase ML 以使用证书指纹匹配来对生产环境中的客户端进行身份验证,并确保只在调试版本中使用调试 API 密钥:
Kotlin
val optionsBuilder = FirebaseVisionCloudImageLabelerOptions.Builder()
if (!BuildConfig.DEBUG) {
// Requires physical, non-rooted device:
optionsBuilder.enforceCertFingerprintMatch()
}
// Set other options. For example:
optionsBuilder.setConfidenceThreshold(0.8f)
// ...
// And lastly:
val options = optionsBuilder.build()
FirebaseVision.getInstance().getCloudImageLabeler(options).processImage(myImage)
Java
FirebaseVisionCloudImageLabelerOptions.Builder optionsBuilder =
new FirebaseVisionCloudImageLabelerOptions.Builder();
if (!BuildConfig.DEBUG) {
// Requires physical, non-rooted device:
optionsBuilder.enforceCertFingerprintMatch();
}
// Set other options. For example:
optionsBuilder.setConfidenceThreshold(0.8f);
// ...
// And lastly:
FirebaseVisionCloudImageLabelerOptions options = optionsBuilder.build();
FirebaseVision.getInstance().getCloudImageLabeler(options).processImage(myImage);
后续步骤
如需了解在使用其他 Firebase 功能时的应用发布准备工作,请参阅发布核对清单。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-22。
[null,null,["最后更新时间 (UTC):2025-08-22。"],[],[],null,["\u003cbr /\u003e\n\nIf your Android app uses one of Firebase ML's cloud APIs, before you launch your\napp in production, you should take some additional steps to prevent\nunauthorized API access.\n\nFor your production apps, you will ensure that only authenticated clients can\naccess cloud services. (Note that only non-rooted devices can authenticate using\nthe method described.)\n\nThen, you will create a debug-only API key that you can use for convenience\nduring testing and development.\n\n1. Register your production apps with Firebase\n\nFirst, register your production apps with Firebase.\n\n1. Make sure that you have your app's SHA-1 signatures. Refer to\n [Authenticating your client](//developers.google.com/android/guides/client-auth)\n to learn how.\n\n2. Go to your settings\n *Project settings* in the Firebase console, then select the *Settings*\n tab.\n\n3. Scroll down to the *Your apps* card, then select your Android app.\n\n4. Add your app's SHA-1 signature to your app's information.\n\n2. Restrict the scope of your API keys\n\nNext, 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\n3. Create and use a debug-only API key\n\nFinally, create a new API key to be used only for development. Firebase ML can\nuse this API key to access Google Cloud services in environments where app\nauthentication isn't possible, such as when running on emulators.\n\n1. Create a new API key to be used for development:\n\n 1. Open the [Credentials](https://console.cloud.google.com/apis/credentials?project=_) page of the\n Google Cloud console. When prompted, select your project.\n\n 2. Click **Create credentials \\\u003e API key** and take note of the new API\n key. This key allows API access from unauthenticated apps, so\n **keep this key confidential**.\n\n2. To ensure the new debug API key is not leaked with your released app,\n specify the debug API key in an Android manifest file used only for debug\n builds:\n\n 1. If you don't already have a debug manifest, create one by clicking\n **File \\\u003e New \\\u003e Other \\\u003e Android Manifest File** and selecting `debug`\n from the target source sets.\n\n 2. In the debug manifest, add the following declaration:\n\n ```text\n \u003capplication\u003e\n \u003cmeta-data\n android:name=\"com.firebase.ml.cloud.ApiKeyForDebug\"\n android:value=\"your-debug-api-key\" /\u003e\n \u003c/application\u003e\n ```\n3. In your app, configure Firebase ML to use certificate fingerprint matching to\n authenticate your client in production and to use API keys---the debug\n key---only in debug builds:\n\n Kotlin \n\n ```kotlin\n val optionsBuilder = FirebaseVisionCloudImageLabelerOptions.Builder()\n if (!BuildConfig.DEBUG) {\n // Requires physical, non-rooted device:\n optionsBuilder.enforceCertFingerprintMatch()\n }\n\n // Set other options. For example:\n optionsBuilder.setConfidenceThreshold(0.8f)\n // ...\n\n // And lastly:\n val options = optionsBuilder.build()\n FirebaseVision.getInstance().getCloudImageLabeler(options).processImage(myImage)https://github.com/firebase/snippets-android/blob/391c1646eacf44d2aab3f76bcfa60dfc6c14acf1/mlkit/app/src/main/java/com/google/firebase/example/mlkit/kotlin/MainActivity.kt#L30-L42\n ```\n\n Java \n\n ```java\n FirebaseVisionCloudImageLabelerOptions.Builder optionsBuilder =\n new FirebaseVisionCloudImageLabelerOptions.Builder();\n if (!BuildConfig.DEBUG) {\n // Requires physical, non-rooted device:\n optionsBuilder.enforceCertFingerprintMatch();\n }\n\n // Set other options. For example:\n optionsBuilder.setConfidenceThreshold(0.8f);\n // ...\n\n // And lastly:\n FirebaseVisionCloudImageLabelerOptions options = optionsBuilder.build();\n FirebaseVision.getInstance().getCloudImageLabeler(options).processImage(myImage);https://github.com/firebase/snippets-android/blob/391c1646eacf44d2aab3f76bcfa60dfc6c14acf1/mlkit/app/src/main/java/com/google/firebase/example/mlkit/MainActivity.java#L30-L43\n ```\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."]]