Proteggere le risorse di backend personalizzate con App Check nei progetti Flutter
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Puoi utilizzare App Check per proteggere le risorse di backend personalizzate non Google per
la tua app, come il tuo backend self-hosted. A questo scopo, dovrai fare entrambe le seguenti operazioni:
- Modifica il client dell'app per inviare un token App Check insieme a ogni richiesta
al tuo backend, come descritto in questa pagina.
- Modifica il backend in modo che richieda un token App Check valido per ogni richiesta,
come descritto in Verificare i token App Check da un backend personalizzato.
Prima di iniziare
Aggiungi App Check alla tua app utilizzando i provider predefiniti.
Inviare token App Check con le richieste di backend
Per assicurarti che le richieste di backend includano un token App Check valido e non scaduto,
precedi ogni richiesta con una chiamata a getToken()
. La libreria App Check
aggiornerà il token, se necessario.
Una volta ottenuto un token valido, invialo insieme alla richiesta al tuo backend. I dettagli su come eseguire questa operazione dipendono da te, ma non inviare
token App Check come parte degli URL, inclusi i parametri di query, in quanto
li rende vulnerabili a divulgazione e intercettazione accidentali. L'approccio consigliato è inviare il token in un'intestazione HTTP personalizzata.
Ad esempio:
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.
}
}
Salvo quando diversamente specificato, i contenuti di questa pagina sono concessi in base alla licenza Creative Commons Attribution 4.0, mentre gli esempi di codice sono concessi in base alla licenza Apache 2.0. Per ulteriori dettagli, consulta le norme del sito di Google Developers. Java è un marchio registrato di Oracle e/o delle sue consociate.
Ultimo aggiornamento 2025-08-23 UTC.
[null,null,["Ultimo aggiornamento 2025-08-23 UTC."],[],[],null,["# Protect custom backend resources with App Check in Flutter projects\n\n\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----------------\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-------------------------------------------\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 }"]]