Ochrona danych logowania aplikacji Firebase ML na Androida w chmurze
Zadbaj o dobrą organizację dzięki kolekcji
Zapisuj i kategoryzuj treści zgodnie ze swoimi preferencjami.
Jeśli Twoja aplikacja na Androida korzysta z jednego z interfejsów API w chmurze Firebase ML, przed udostępnieniem jej w wersji produkcyjnej podejmij dodatkowe działania, które uniemożliwią nieautoryzowany dostęp przez interfejs API.
W przypadku aplikacji produkcyjnych musisz zadbać o to, aby tylko uwierzytelnieni klienci mieli dostęp do usług w chmurze. (Pamiętaj, że uwierzytelnianie za pomocą opisanej metody jest możliwe tylko na urządzeniach bez dostępu do roota).
Następnie utworzysz klucz interfejsu API tylko do debugowania, którego możesz wygodnie używać podczas testowania i programowania.
1. Rejestrowanie aplikacji produkcyjnych w Firebase
Najpierw zarejestruj aplikacje produkcyjne w Firebase.
Upewnij się, że masz podpisy SHA-1 aplikacji. Aby dowiedzieć się, jak to zrobić, przeczytaj artykuł Uwierzytelnianie klienta.
Otwórz settingsUstawienia projektu w Firebasekonsoli, a następnie wybierz kartę Ustawienia.
Przewiń w dół do karty Twoje aplikacje, a potem wybierz aplikację na Androida.
Dodaj podpis SHA-1 aplikacji do informacji o niej.
2. Ograniczanie zakresu kluczy interfejsu API
Następnie skonfiguruj istniejące klucze interfejsu API, aby zablokować dostęp do interfejsu Cloud Vision API:
Otwórz stronę Dane logowania w konsoliGoogle Cloud. Gdy pojawi się prośba, wybierz projekt.
Otwórz widok edycji każdego klucza API na liście.
W sekcji Ograniczenia interfejsów API wybierz Ogranicz klucz, a następnie dodaj do listy wszystkie interfejsy API, do których klucz ma mieć dostęp. Pamiętaj, aby nie uwzględniać interfejsu Cloud Vision API.
Konfigurując ograniczenia interfejsów API klucza interfejsu API, wyraźnie deklarujesz interfejsy API, do których klucz ma dostęp. Domyślnie, gdy w sekcji Ograniczenia interfejsów API jest wybrana opcja Nie ograniczaj klucza, klucz interfejsu API może być używany do uzyskiwania dostępu do dowolnego interfejsu API włączonego w projekcie.
Od tej pory istniejące klucze interfejsu API nie będą przyznawać dostępu do usług ML w chmurze, ale każdy klucz będzie nadal działać w przypadku interfejsów API, które zostały dodane do listy ograniczeń interfejsów API.
Pamiętaj, że jeśli w przyszłości włączysz dodatkowe interfejsy API, musisz dodać je do listy ograniczeń interfejsu API dla odpowiedniego klucza interfejsu API.
3. Tworzenie i używanie klucza interfejsu API tylko do debugowania
Na koniec utwórz nowy klucz interfejsu API, który będzie używany tylko na potrzeby programowania. Firebase ML może używać tego klucza interfejsu API, aby uzyskiwać dostęp do usług Google Cloud w środowiskach, w których uwierzytelnianie aplikacji jest niemożliwe, np. podczas działania na emulatorach.
Utwórz nowy klucz interfejsu API, który będzie używany na potrzeby programowania:
Otwórz stronę Dane logowania w konsoliGoogle Cloud. Gdy pojawi się prośba, wybierz projekt.
Kliknij Utwórz dane logowania > Klucz interfejsu API i zanotuj nowy klucz interfejsu API. Ten klucz umożliwia dostęp do interfejsu API z nieuwierzytelnionych aplikacji, więc zachowaj go w tajemnicy.
Aby mieć pewność, że nowy klucz interfejsu API debugowania nie wycieknie wraz z opublikowaną aplikacją, określ go w pliku manifestu Androida używanym tylko w przypadku kompilacji debugowania:
Jeśli nie masz jeszcze manifestu debugowania, utwórz go, klikając Plik > Nowy > Inne > Plik manifestu Androida i wybierając debug z docelowych zestawów źródeł.
W pliku manifestu debugowania dodaj tę deklarację:
W aplikacji skonfiguruj Firebase ML tak, aby w wersji produkcyjnej uwierzytelniać klienta za pomocą dopasowywania odcisków cyfrowych certyfikatów, a w wersjach debugowania używać tylko kluczy interfejsu API (klucza debugowania):
Kotlin
valoptionsBuilder=FirebaseVisionCloudImageLabelerOptions.Builder()if(!BuildConfig.DEBUG){// Requires physical, non-rooted device:optionsBuilder.enforceCertFingerprintMatch()}// Set other options. For example:optionsBuilder.setConfidenceThreshold(0.8f)// ...// And lastly:valoptions=optionsBuilder.build()FirebaseVision.getInstance().getCloudImageLabeler(options).processImage(myImage)
FirebaseVisionCloudImageLabelerOptions.BuilderoptionsBuilder=newFirebaseVisionCloudImageLabelerOptions.Builder();if(!BuildConfig.DEBUG){// Requires physical, non-rooted device:optionsBuilder.enforceCertFingerprintMatch();}// Set other options. For example:optionsBuilder.setConfidenceThreshold(0.8f);// ...// And lastly:FirebaseVisionCloudImageLabelerOptionsoptions=optionsBuilder.build();FirebaseVision.getInstance().getCloudImageLabeler(options).processImage(myImage);
[[["Łatwo zrozumieć","easyToUnderstand","thumb-up"],["Rozwiązało to mój problem","solvedMyProblem","thumb-up"],["Inne","otherUp","thumb-up"]],[["Brak potrzebnych mi informacji","missingTheInformationINeed","thumb-down"],["Zbyt skomplikowane / zbyt wiele czynności do wykonania","tooComplicatedTooManySteps","thumb-down"],["Nieaktualne treści","outOfDate","thumb-down"],["Problem z tłumaczeniem","translationIssue","thumb-down"],["Problem z przykładami/kodem","samplesCodeIssue","thumb-down"],["Inne","otherDown","thumb-down"]],["Ostatnia aktualizacja: 2025-08-31 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/cc307b137a106d66a52fa6e9bee6f4f55dab9f76/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/cc307b137a106d66a52fa6e9bee6f4f55dab9f76/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."]]