ปกป้องข้อมูลเข้าสู่ระบบระบบคลาวด์ของแอป Firebase ML ใน Apple
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
หากแอป Apple ของคุณใช้ Cloud API ของ Firebase ML คุณควรดำเนินการเพิ่มเติมเพื่อป้องกันการเข้าถึง API ที่ไม่ได้รับอนุญาตก่อนที่จะเปิดตัวแอปในเวอร์ชันที่ใช้งานจริง
1. ลดขอบเขตของคีย์ 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 ที่เกี่ยวข้อง
2. สร้างคีย์ API ใหม่เพื่อใช้กับ Firebase ML
จากนั้นสร้างคีย์ API ใหม่สำหรับ Firebase ML ที่อนุญาตเฉพาะการเรียกไปยัง Cloud Vision API โดยทำดังนี้
กลับไปที่หน้าข้อมูลเข้าสู่ระบบ ตรวจสอบว่าได้เลือกโปรเจ็กต์ Firebase แล้ว
คลิกสร้างข้อมูลเข้าสู่ระบบ > คีย์ API จดคีย์ API ใหม่ แล้วคลิกจำกัดคีย์
ในส่วนการจำกัด API ให้เลือกจำกัดคีย์ แล้วเพิ่ม Cloud Vision API เท่านั้นลงในรายการ
คีย์ API นี้ให้สิทธิ์เข้าถึงเฉพาะ Cloud Vision API และใช้ได้โดย
Firebase ML เพื่อเข้าถึงโมเดลที่อิงตามระบบคลาวด์
3. แนะนํา: ลดโควต้าของ Cloud Vision API
คุณควรลดโควต้าต่อผู้ใช้ของ Cloud Vision API จากการตั้งค่าเริ่มต้นเพื่อช่วยลดผลกระทบจากคีย์ที่ถูกบุกรุก
วิธีการมีดังนี้
เปิดหน้าโควต้า Cloud Vision API ของ
Google Cloud คอนโซล เมื่อได้รับข้อความแจ้ง ให้เลือกโปรเจ็กต์
ในส่วนคำขอ ให้ตั้งค่าโควต้าคำขอต่อนาทีต่อผู้ใช้
ให้เหมาะสมกับแอปของคุณ ตัวอย่างเช่น หากแอปของคุณเกี่ยวข้องกับ
การอัปโหลดรูปภาพเอกสารเพื่อรับข้อความกลับมา ผู้ใช้ไม่น่าจะ
ทำเช่นนั้นมากกว่า 1 ครั้งในทุกๆ 2-3 วินาที ดังนั้นโควต้า 30-40
จึงน่าจะปลอดภัย
โปรดทราบว่าในบริบทนี้ "คำขอต่อผู้ใช้" หมายถึงคำขอจากที่อยู่ IP เดียว คุณอาจต้องพิจารณาเรื่องนี้หากคาดว่าจะมีผู้ใช้หลายคนใช้แอปพร้อมกันจากเบื้องหลัง NAT
4. เรียกใช้ Cloud API โดยใช้คีย์ API Firebase ML
สุดท้าย ให้กำหนดค่า Firebase ML ในแอปเพื่อใช้คีย์ API ใหม่
เนื่องจากคีย์ API Firebase ML อนุญาตให้เข้าถึง Cloud Vision API โดยไม่ต้องมีการตรวจสอบสิทธิ์ จึงควรเก็บคีย์ไว้เป็นความลับเพื่อป้องกันการใช้งานที่ไม่ได้รับอนุญาตและการเรียกเก็บเงินจากบัญชีสำหรับการเรียกเก็บเงิน ดังนั้น คุณจึงไม่ควรรวมคีย์ API ไว้ในไบนารีของแอป แต่ให้ตรวจสอบว่าผู้ใช้ที่เชื่อถือได้ลงชื่อเข้าใช้แล้วในเวลาที่แอปทำงาน จากนั้นจึงดึงคีย์ API จากเซิร์ฟเวอร์
แม้จะปฏิบัติตามแนวทางเหล่านี้ แต่ก็อาจมีกรณีที่คีย์ API ถูก
บุกรุกได้ คุณควรดำเนินการเพื่อช่วยลดผลกระทบจากคีย์ที่ถูกบุกรุก เช่น ลดโควต้าต่อผู้ใช้ของ API ตามที่อธิบายไว้ข้างต้น
ใช้การหมุนเวียนคีย์ และออกคีย์ที่แตกต่างกันให้กับกลุ่มผู้ใช้ต่างๆ
หลังจากที่แอปได้รับคีย์ API อย่างปลอดภัยแล้ว เมื่อต้องการเรียกใช้ Firebase MLCloud 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 Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-25 UTC
[null,null,["อัปเดตล่าสุด 2025-07-25 UTC"],[],[],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."]]