ปกป้องทรัพยากรแบ็กเอนด์ที่กำหนดเองด้วย App Check ในโปรเจ็กต์ Flutter
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
คุณสามารถใช้ App Check เพื่อปกป้องทรัพยากรแบ็กเอนด์ที่กำหนดเองที่ไม่ใช่ของ Google สำหรับแอป เช่น แบ็กเอนด์ที่โฮสต์เอง โดยจะต้องดำเนินการทั้ง 2 อย่างต่อไปนี้
- แก้ไขไคลเอ็นต์แอปให้ส่งโทเค็น App Check พร้อมกับคำขอแต่ละรายการไปยังแบ็กเอนด์ ตามที่อธิบายไว้ในหน้านี้
- แก้ไขแบ็กเอนด์ให้กำหนดให้มีโทเค็น App Check ที่ถูกต้องกับคำขอทุกรายการ ตามที่อธิบายไว้ในยืนยันโทเค็น App Check จากแบ็กเอนด์ที่กำหนดเอง
ก่อนเริ่มต้น
เพิ่ม App Check ลงในแอปโดยใช้ผู้ให้บริการเริ่มต้น
ส่งโทเค็น App Check กับคำขอแบ็กเอนด์
ตรวจสอบว่าคําขอแบ็กเอนด์มีโทเค็น App Check ที่ถูกต้องและยังไม่หมดอายุ โดยให้เรียกใช้ getToken()
ก่อนส่งคําขอแต่ละรายการ ไลบรารี App Check จะรีเฟรชโทเค็นหากจำเป็น
เมื่อคุณมีโทเค็นที่ถูกต้องแล้ว ให้ส่งโทเค็นพร้อมกับคําขอไปยังแบ็กเอนด์ คุณเลือกวิธีดำเนินการได้ตามต้องการ แต่อย่าส่งโทเค็น App Check เป็นส่วนหนึ่งของ URL รวมถึงในพารามิเตอร์การค้นหา เนื่องจากจะทำให้โทเค็นเสี่ยงต่อการรั่วไหลและการดักรับโดยไม่ตั้งใจ แนวทางที่แนะนําคือการส่งโทเค็นในส่วนหัว HTTP ที่กําหนดเอง
เช่น
void callApiExample() async {
final appCheckToken = await FirebaseAppCheck.instance.getToken();
if (appCheckToken != null) {
final response = await http.get(
Uri.parse("https://yourbackend.example.com/yourExampleEndpoint"),
headers: {"X-Firebase-AppCheck": appCheckToken},
);
} else {
// Error: couldn't get an App Check token.
}
}
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-06-17 UTC
[null,null,["อัปเดตล่าสุด 2025-06-17 UTC"],[],[],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/flutter/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 `getToken()`. The App Check library\nwill 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() async {\n final appCheckToken = await FirebaseAppCheck.instance.getToken();\n if (appCheckToken != null) {\n final response = await http.get(\n Uri.parse(\"https://yourbackend.example.com/yourExampleEndpoint\"),\n headers: {\"X-Firebase-AppCheck\": appCheckToken},\n );\n } else {\n // Error: couldn't get an App Check token.\n }\n }"]]