ปกป้องข้อมูลเข้าสู่ระบบระบบคลาวด์ของแอป Firebase ML สำหรับ Android
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
หากแอป Android ใช้ Cloud API ของ Firebase ML คุณควรดำเนินการเพิ่มเติมเพื่อป้องกันการเข้าถึง API ที่ไม่ได้รับอนุญาตก่อนที่จะเปิดตัวแอปที่ใช้งานจริง
สำหรับแอปเวอร์ชันที่ใช้งานจริง คุณจะต้องตรวจสอบว่ามีเพียงไคลเอ็นต์ที่ได้รับการตรวจสอบสิทธิ์เท่านั้นที่เข้าถึงบริการระบบคลาวด์ได้ (โปรดทราบว่ามีเพียงอุปกรณ์ที่ไม่ได้รูทเท่านั้นที่สามารถตรวจสอบสิทธิ์โดยใช้วิธีที่อธิบายไว้
ได้)
จากนั้นคุณจะสร้างคีย์ API สำหรับการแก้ไขข้อบกพร่องเท่านั้น ซึ่งคุณสามารถใช้เพื่อความสะดวก
ในระหว่างการทดสอบและการพัฒนา
1. ลงทะเบียนแอปเวอร์ชันที่ใช้งานจริงกับ Firebase
ก่อนอื่น ให้ลงทะเบียนแอปเวอร์ชันที่ใช้งานจริงกับ Firebase
ตรวจสอบว่าคุณมีลายเซ็น SHA-1 ของแอป ดูวิธีได้ที่การตรวจสอบสิทธิ์ไคลเอ็นต์
ไปที่settings
การตั้งค่าโปรเจ็กต์ในFirebaseคอนโซล แล้วเลือกแท็บการตั้งค่า
เลื่อนลงไปที่การ์ดแอปของคุณ แล้วเลือกแอป 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 ที่มีอยู่จะไม่ให้สิทธิ์เข้าถึงบริการ ML บนคลาวด์ แต่คีย์แต่ละรายการจะยังคงใช้ได้กับ API ที่คุณเพิ่มลงในรายการข้อจำกัดของ API
โปรดทราบว่าหากเปิดใช้ API เพิ่มเติมในอนาคต คุณจะต้องเพิ่ม API เหล่านั้นลงในรายการข้อจำกัดของ API สำหรับคีย์ API ที่เกี่ยวข้อง
3. สร้างและใช้คีย์ API สำหรับการแก้ไขข้อบกพร่องเท่านั้น
สุดท้าย ให้สร้างคีย์ API ใหม่เพื่อใช้สำหรับการพัฒนาเท่านั้น Firebase ML สามารถใช้คีย์ API นี้เพื่อเข้าถึงบริการของ Google Cloud ในสภาพแวดล้อมที่การตรวจสอบสิทธิ์ของแอปเป็นไปไม่ได้ เช่น เมื่อเรียกใช้ในโปรแกรมจำลอง
สร้างคีย์ API ใหม่เพื่อใช้ในการพัฒนาโดยทำดังนี้
เปิดหน้าข้อมูลเข้าสู่ระบบของคอนโซล
Google Cloud เมื่อได้รับข้อความแจ้ง ให้เลือกโปรเจ็กต์
คลิกสร้างข้อมูลเข้าสู่ระบบ > คีย์ API แล้วจดคีย์ API ใหม่ คีย์นี้อนุญาตให้เข้าถึง API จากแอปที่ไม่ได้รับการตรวจสอบสิทธิ์ ดังนั้น
โปรดเก็บคีย์นี้ไว้เป็นความลับ
เพื่อให้มั่นใจว่าคีย์ API สำหรับการแก้ไขข้อบกพร่องใหม่จะไม่รั่วไหลไปพร้อมกับแอปที่เผยแพร่
ให้ระบุคีย์ API สำหรับการแก้ไขข้อบกพร่องในไฟล์ Manifest ของ Android ที่ใช้เฉพาะสำหรับการสร้าง
การแก้ไขข้อบกพร่อง
หากยังไม่มีไฟล์ Manifest สำหรับการแก้ไขข้อบกพร่อง ให้สร้างโดยคลิกไฟล์ > ใหม่ > อื่นๆ > ไฟล์ Manifest ของ Android แล้วเลือก debug
จากชุดแหล่งที่มาเป้าหมาย
เพิ่มการประกาศต่อไปนี้ในไฟล์ Manifest สำหรับการแก้ไขข้อบกพร่อง
<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 Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-21 UTC
[null,null,["อัปเดตล่าสุด 2025-08-21 UTC"],[],[],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."]]