ปกป้องทรัพยากรแบ็กเอนด์ที่กำหนดเองด้วย App Check ในโปรเจ็กต์ Flutter
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
คุณสามารถใช้ App Check เพื่อปกป้องทรัพยากรแบ็กเอนด์ที่กำหนดเองซึ่งไม่ใช่ของ Google สำหรับ
แอปของคุณ เช่น แบ็กเอนด์ที่โฮสต์ด้วยตนเอง โดยจะต้องดำเนินการทั้ง 2 อย่างต่อไปนี้
- แก้ไขไคลเอ็นต์แอปเพื่อส่งโทเค็น App Check พร้อมกับคำขอแต่ละรายการ
ไปยังแบ็กเอนด์ตามที่อธิบายไว้ในหน้านี้
- แก้ไขแบ็กเอนด์ให้กำหนดว่าทุกคำขอต้องมีโทเค็น App Check ที่ถูกต้อง
ตามที่อธิบายไว้ในยืนยันโทเค็น App Check จากแบ็กเอนด์ที่กำหนดเอง
ก่อนเริ่มต้น
เพิ่ม App Check ลงในแอปโดยใช้ผู้ให้บริการเริ่มต้น
ส่งโทเค็น App Check พร้อมคำขอแบ็กเอนด์
หากต้องการให้คำขอแบ็กเอนด์มีโทเค็น App Check ที่ถูกต้องและยังไม่หมดอายุ
ให้เรียกใช้ getToken()
ก่อนคำขอแต่ละรายการ ไลบรารี 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-08-23 UTC
[null,null,["อัปเดตล่าสุด 2025-08-23 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 }"]]