Bảo vệ thông tin đăng nhập của bạn trên đám mây cho ứng dụng Android ML ML
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Nếu ứng dụng Android của bạn sử dụng một trong các API đám mây của Firebase ML, thì trước khi ra mắt ứng dụng trong quá trình phát hành công khai, bạn nên thực hiện một số bước bổ sung để ngăn chặn hành vi truy cập API trái phép.
Đối với các ứng dụng phát hành công khai, bạn sẽ đảm bảo rằng chỉ những ứng dụng khách đã xác thực mới có thể truy cập vào các dịch vụ đám mây. (Xin lưu ý rằng chỉ những thiết bị không bị can thiệp hệ thống mới có thể xác thực bằng phương thức được mô tả.)
Sau đó, bạn sẽ tạo một khoá API chỉ dành cho gỡ lỗi mà bạn có thể sử dụng cho thuận tiện trong quá trình kiểm thử và phát triển.
1. Đăng ký các ứng dụng phát hành công khai với Firebase
Trước tiên, hãy đăng ký các ứng dụng phát hành công khai của bạn với Firebase.
Đảm bảo bạn có chữ ký SHA-1 của ứng dụng. Hãy tham khảo phần Xác thực ứng dụng của bạn để tìm hiểu cách thực hiện.
Chuyển đến phần Project settings (Cài đặt dự án) trong bảng điều khiển Firebase, sau đó chọn thẻ Settings (Cài đặt).settings
Di chuyển xuống thẻ Ứng dụng của bạn, sau đó chọn ứng dụng Android của bạn.
Thêm chữ ký SHA-1 của ứng dụng vào thông tin của ứng dụng.
2. Hạn chế phạm vi của khoá API
Tiếp theo, hãy định cấu hình các khoá API hiện có để không cho phép truy cập vào Cloud Vision API:
Mở trang Thông tin đăng nhập của bảng điều khiển Google Cloud. Khi được nhắc, hãy chọn dự án của bạn.
Đối với mỗi khoá API hiện có trong danh sách, hãy mở chế độ chỉnh sửa.
Trong phần API restrictions (Hạn chế cho API), hãy chọn Restrict key (Hạn chế cho khoá), sau đó thêm tất cả API mà bạn muốn khoá API có quyền truy cập vào danh sách. Nhớ không thêm Cloud Vision API.
Khi định cấu hình các quy tắc hạn chế đối với API của một khoá API, bạn đang khai báo rõ ràng những API mà khoá đó có quyền truy cập. Theo mặc định, khi bạn chọn Don't restrict key (Không hạn chế khoá) trong mục API restrictions (Hạn chế cho API), khoá API có thể được dùng để truy cập vào mọi API được bật cho dự án.
Giờ đây, các khoá API hiện có của bạn sẽ không cấp quyền truy cập vào các dịch vụ ML trên đám mây, nhưng mỗi khoá sẽ tiếp tục hoạt động cho mọi API mà bạn đã thêm vào danh sách hạn chế API.
Xin lưu ý rằng nếu bật thêm API trong tương lai, bạn phải thêm các API đó vào danh sách quy tắc hạn chế đối với API cho khoá API hiện hành.
3. Tạo và sử dụng khoá API chỉ dành cho gỡ lỗi
Cuối cùng, hãy tạo một khoá API mới chỉ dùng cho mục đích phát triển. Firebase ML có thể dùng khoá API này để truy cập vào các dịch vụ Google Cloud trong những môi trường không thể xác thực ứng dụng, chẳng hạn như khi chạy trên trình mô phỏng.
Tạo một khoá API mới để dùng cho quá trình phát triển:
Mở trang Thông tin đăng nhập của bảng điều khiển Google Cloud. Khi được nhắc, hãy chọn dự án của bạn.
Nhấp vào Tạo thông tin xác thực > Khoá API rồi ghi lại khoá API mới. Khoá này cho phép truy cập API từ các ứng dụng chưa được xác thực, vì vậy hãy giữ bí mật khoá này.
Để đảm bảo khoá API gỡ lỗi mới không bị rò rỉ cùng với ứng dụng phát hành, hãy chỉ định khoá API gỡ lỗi trong một tệp kê khai Android chỉ dùng cho các bản gỡ lỗi:
Nếu bạn chưa có tệp kê khai gỡ lỗi, hãy tạo một tệp bằng cách nhấp vào File > New > Other > Android Manifest File (Tệp > Mới > Khác > Tệp kê khai Android) rồi chọn debug trong các tập hợp nguồn đích.
Trong ứng dụng của bạn, hãy định cấu hình Firebase ML để sử dụng tính năng so khớp dấu vân tay chứng chỉ nhằm xác thực ứng dụng khách trong phiên bản phát hành công khai và chỉ sử dụng khoá API (khoá gỡ lỗi) trong các bản gỡ lỗi:
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);
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-09-05 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/b694d4dbd411d31be39655f47691c3e9f3529b03/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/b694d4dbd411d31be39655f47691c3e9f3529b03/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."]]