範例:在多個網站和多個 Android 應用程式之間共用憑證
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
如要設定多個網站和多個 Android 應用程式共用憑證,您必須:
- 指定主要網域:選擇一個網址做為連結的主要參考點。
- 建立
assetlinks.json
檔案:
- 主網域:在主網域上建立
assetlinks.json
檔案,列出所有網域共用憑證。
- 其他網域:在其他網域上建立
assetlinks.json
檔案,並將每個檔案連結回主網域。
- 在每個應用程式的資訊清單中設定數位資產連結:設定每個應用程式,以便連結回主要網域中的
assetlinks.json
檔案。
在以下範例中,我們將 example.com 指定為主網域,所有其他列出的網域都會與 example.com 和彼此共用憑證。
以下兩個套件名稱的應用程式也會與所有網站和彼此共用憑證:
com.example.android.myapplication
com.example.appname
您必須先完成必要條件,才能設定憑證共用功能。
如要宣告跨這些網站和 Android 應用程式共用憑證,請按照下列步驟操作:
使用以下內容建立 assetlinks.json
檔案:
[
{
"relation": ["delegate_permission/common.get_login_creds"],
"target": {
"namespace": "web",
"site": "https://example.com"
}
},
{
"relation": ["delegate_permission/common.get_login_creds"],
"target": {
"namespace": "web",
"site": "https://example.org"
}
},
{
"relation": ["delegate_permission/common.get_login_creds"],
"target": {
"namespace": "web",
"site": "https://example.net"
}
},
{
"relation": ["delegate_permission/common.get_login_creds"],
"target": {
"namespace": "web",
"site": "https://myownpersonaldomain.com"
}
},
{
"relation" : [
"delegate_permission/common.get_login_creds"
],
"target" : {
"namespace" : "android_app",
"package_name" : "com.example.android.myapplication",
"sha256_cert_fingerprints" : [ "AA:BB:CC:DD:EE:FF:11:22:33:44:55:66:77:88:99:00:AA:BB:CC:DD:EE:FF:11:22:33:44:55:66:77:88:99:00"
]
}
},
{
"relation" : [
"delegate_permission/common.get_login_creds"
],
"target" : {
"namespace" : "android_app",
"package_name" : "com.example.appname",
"sha256_cert_fingerprints" : [ "00:11:22:33:44:55:66:77:88:99:AA:BB:CC:DD:EE:FF:00:11:22:33:44:55:66:77:88:99:AA:BB:CC:DD:EE:FF"
]
}
}
]
將 Digital Asset Links JSON 檔案存放在主網域的對應已知位置,在本例中為:https://example.com/.well-known/assetlinks.json
。
請使用以下內容建立另一個 assetlinks.json
檔案:
[
{
"relation": ["delegate_permission/common.get_login_creds"],
"target": {
"namespace": "web",
"site": "example.com"
}
}
]
請在其他網域的以下位置代管此 Digital Asset Links JSON 檔案的副本,在本例中為:
example.org/.well-known/assetlinks.json
example.net/.well-known/assetlinks.json
mypersonaldomain.com/.well-known/assetlinks.json
在 <application>
下方的兩個 Android 應用程式資訊清單檔案中新增下列行:
<meta-data android:name="asset_statements" android:resource="@string/asset_statements"/>
在 Android 應用程式中宣告關聯,即可在資訊清單中設定數位資產連結。新增物件,用於指定要載入的 assetlinks.json
檔案。在這種情況下:
<string name="asset_statements" translatable="false">
[{
\"include\": \"https://example.com/.well-known/assetlinks.json\"
}]
</string>
完成上述步驟後,您就可以在多個網站和 Android 應用程式中順利設定憑證共用。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-25 (世界標準時間)。
[null,null,["上次更新時間:2025-07-25 (世界標準時間)。"],[],[],null,["# Example: Share credentials across multiple websites and multiple Android apps\n\nTo set up credential sharing across multiple websites and multiple Android apps\nyou must:\n\n- **Designate a primary domain**: Choose one URL as the primary reference point for links.\n- **Create `assetlinks.json` files** :\n - **Primary domain** : Create an `assetlinks.json` file on your primary domain that lists all domains sharing credentials.\n - **Other domains** : Create an `assetlinks.json` file on each of the other domains, each linking back to the primary domain.\n- **Configure digital asset linking in each App's manifest** : Set up each app to link back to the `assetlinks.json` file in the primary domain.\n\n| **Important:** While Google treats credential sharing relationships as transitive, individual [Digital Asset Links are\n| not](https://developers.google.com/digital-asset-links/reference/rpc/google.digitalassetlinks.v1).\n\nIn the following example, we designated\n[example.com](https://example.com/) as the primary domain, and all other\nlisted domains share credentials with\n[example.com](https://example.com/) and across themselves.\n\n- [example.com](https://example.com/) (primary domain)\n- [example.org](https://example.jp/)\n- [example.net](https://example.de/)\n- [myownpersonaldomain.com](http://myownpersonaldomain.com)\n\nThe two apps with the following package names also share credentials with all\nthe websites and between them:\n\n- `com.example.android.myapplication`\n- `com.example.appname`\n\nBefore setting up credential sharing you must [complete the prerequisites](/identity/credential-sharing/set-up#prerequisites).\n\nTo declare credential sharing across these websites and Android apps:\n\n1. Create an `assetlinks.json` file with the following content:\n\n [\n {\n \"relation\": [\"delegate_permission/common.get_login_creds\"],\n \"target\": {\n \"namespace\": \"web\",\n \"site\": \"https://example.com\"\n }\n },\n {\n \"relation\": [\"delegate_permission/common.get_login_creds\"],\n \"target\": {\n \"namespace\": \"web\",\n \"site\": \"https://example.org\"\n }\n },\n {\n \"relation\": [\"delegate_permission/common.get_login_creds\"],\n \"target\": {\n \"namespace\": \"web\",\n \"site\": \"https://example.net\"\n }\n },\n {\n \"relation\": [\"delegate_permission/common.get_login_creds\"],\n \"target\": {\n \"namespace\": \"web\",\n \"site\": \"https://myownpersonaldomain.com\"\n }\n },\n {\n \"relation\" : [\n \"delegate_permission/common.get_login_creds\"\n ],\n \"target\" : {\n \"namespace\" : \"android_app\",\n \"package_name\" : \"com.example.android.myapplication\",\n \"sha256_cert_fingerprints\" : [ \"AA:BB:CC:DD:EE:FF:11:22:33:44:55:66:77:88:99:00:AA:BB:CC:DD:EE:FF:11:22:33:44:55:66:77:88:99:00\"\n ]\n }\n },\n {\n \"relation\" : [\n \"delegate_permission/common.get_login_creds\"\n ],\n \"target\" : {\n \"namespace\" : \"android_app\",\n \"package_name\" : \"com.example.appname\",\n \"sha256_cert_fingerprints\" : [ \"00:11:22:33:44:55:66:77:88:99:AA:BB:CC:DD:EE:FF:00:11:22:33:44:55:66:77:88:99:AA:BB:CC:DD:EE:FF\"\n ]\n }\n }\n ]\n\n2. Host the Digital Asset Links JSON file in the corresponding well-known\n location on the primary domain, in this case:\n `https://example.com/.well-known/assetlinks.json`.\n\n3. Create another `assetlinks.json` file with the following content:\n\n [\n {\n \"relation\": [\"delegate_permission/common.get_login_creds\"],\n \"target\": {\n \"namespace\": \"web\",\n \"site\": \"example.com\"\n }\n }\n ]\n\n4. Host copies of this Digital Asset Links JSON file at the following location\n on the other domains, in this case:\n\n - `example.org/.well-known/assetlinks.json`\n - `example.net/.well-known/assetlinks.json`\n - `mypersonaldomain.com/.well-known/assetlinks.json`\n5. Add the following line to both Android Apps manifests files under\n `\u003capplication\u003e`:\n\n \u003cmeta-data android:name=\"asset_statements\" android:resource=\"@string/asset_statements\"/\u003e\n\n6. Configure digital asset linking in the manifest by declaring the association\n in the Android app. Add an object that specifies the `assetlinks.json` files\n to load. In this case:\n\n \u003cstring name=\"asset_statements\" translatable=\"false\"\u003e\n [{\n \\\"include\\\": \\\"https://example.com/.well-known/assetlinks.json\\\"\n }]\n \u003c/string\u003e\n\nAfter following these steps, you have successfully set up seamless credential\nsharing across multiple websites and Android apps."]]