[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-21。"],[],[],null,["# Set custom audiences for services\n\nThis page describes how to use custom audiences for authorization.\n\nClients that call an Identity and Access Management-protected Cloud Run service\nmust provide a valid ID token that includes an\n[audience claim](/docs/authentication/token-types#id-aud) matching the receiving\nservice's `*.run.app` URL. For clients that don't know this URL, you can use a\ncustom audience value.\n\nUnderstanding custom audiences\n------------------------------\n\nCloud Run provides an Invoker (`roles/run.invoker`) role to support\n[access control with IAM](/run/docs/securing/managing-access).\nIAM access control makes use of Google-signed ID tokens,\nwhich are packaged as [JSON Web Tokens (JWTs)](https://en.wikipedia.org/wiki/JSON_Web_Token).\nThe contents of these tokens conform to an OIDC standard.\n\nAn audience field is encoded in the token to specify the intended target that\ncan use the token.\nThis limits the risk of a replay attack, where an intercepted token intended\nfor use with one service is replayed against a different service.\n\nBy convention, the audience is the full URL of the target service. By default in\nCloud Run, this is the Google-generated URL for a service ending in\n`run.app`.\n\nHowever, a Cloud Run service might sit behind a URL other than the\ndefault-generated URL, such as in the following scenarios:\n\n- When using a [custom domain](/run/docs/mapping-custom-domains) to reach a service where the client is unaware of the Google-generated URL.\n- When deploying multiple services [behind a load balancer](/load-balancing/docs/https/setting-up-https-serverless) where a client can't anticipate which regional service a request reaches. Google-generated URLs for services are region-specific even if the service name is the same.\n\nIn these scenarios, you must configure a service to accept custom\naudience values that allow additional targets known by a client. The default\nGoogle-generated URL always remains as an accepted audience value.\n\nSet and update custom audiences\n-------------------------------\n\nSetting custom audiences for Cloud Run is done at the service level\nand applies to all serving revisions, similar to IAM\nauthorization membership.\n\nYou can set multiple custom audiences, as long as JSON-encoding of\nthe audiences as a string list does not exceed 32,768 characters.\n\nAny configuration change leads to the\ncreation of a new revision. Subsequent revisions will also automatically get\nthis configuration setting unless you make explicit updates to change it. \n\n### gcloud\n\nYou can set custom audiences on a service by using the following command: \n\n```bash\ngcloud run services update SERVICE --add-custom-audiences=AUDIENCE\n```\n\nReplace\n\n- \u003cvar translate=\"no\"\u003eSERVICE\u003c/var\u003e with the name of your Cloud Run service\n- \u003cvar translate=\"no\"\u003eAUDIENCE\u003c/var\u003e with a string for the custom audience you want to support, for example, `myservice` or `https://myservice.example.com`\n\nYou can remove all custom audiences from a service by using the following command: \n\n```bash\ngcloud run services update SERVICE --clear-custom-audiences\n```\n\n### YAML\n\n1. If you are creating a new service, skip this step.\n If you are updating an existing service, download its [YAML configuration](/run/docs/reference/yaml/v1):\n\n ```bash\n gcloud run services describe SERVICE --format export \u003e service.yaml\n ```\n2. Set the `run.googleapis.com/custom-audiences` annotation on the Service\n metadata (**not** on the `template` metadata):\n\n ```yaml\n apiVersion: serving.knative.dev/v1\n kind: Service\n metadata:\n name: SERVICE\n annotations:\n run.googleapis.com/custom-audiences: '[\"\u003cvar translate=\"no\"\u003eAUDIENCE\u003c/var\u003e\"]'\n spec:\n template:\n ...\n ```\n\n Replace\n - \u003cvar translate=\"no\"\u003eSERVICE\u003c/var\u003e with the name of your Cloud Run service\n - \u003cvar translate=\"no\"\u003eAUDIENCE\u003c/var\u003e with a string for the custom audience you want to support, for example, `myservice` or `https://myservice.example.com`\n\n Note that the value of the attribute is a quoted JSON array of strings,\n requiring the use of both double and single quotes.\n3. Replace the service with its new configuration by using the following\n command:\n\n ```bash\n gcloud run services replace service.yaml\n ```\n\n### Terraform\n\n\u003cbr /\u003e\n\nTo learn how to apply or remove a Terraform configuration, see\n[Basic Terraform commands](/docs/terraform/basic-commands).\nAdd the following to a [`google_cloud_run_v2_service`](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/cloud_run_v2_service) resource in your Terraform configuration: \n\n\u003cbr /\u003e\n\n resource \"google_cloud_run_v2_service\" \"default\" {\n name = \"\u003cvar translate=\"no\"\u003eSERVICE\u003c/var\u003e\"\n location = \"\u003cvar translate=\"no\"\u003eREGION\u003c/var\u003e\"\n\n custom_audiences = [\"\u003cvar translate=\"no\"\u003eAUDIENCE\u003c/var\u003e\"]\n template {\n containers {\n image = \"us-docker.pkg.dev/cloudrun/container/hello\"\n }\n }\n }\n\nReplace:\n\n- \u003cvar translate=\"no\"\u003eSERVICE\u003c/var\u003e with the name of your Cloud Run service.\n- \u003cvar translate=\"no\"\u003eREGION\u003c/var\u003e with the Google Cloud region. For example, `europe-west1`.\n- \u003cvar translate=\"no\"\u003eAUDIENCE\u003c/var\u003e with a string for the custom audience you want to support, for example, `myservice`.\n\nVerifying custom audiences\n--------------------------\n\n1. Get an ID token for a service account which has IAM permission\n to invoke the service. Note the use of the custom audience \u003cvar translate=\"no\"\u003eAUDIENCE\u003c/var\u003e.\n\n ```bash\n export TOKEN=$(gcloud auth print-identity-token --impersonate-service-account SERVICE_ACCOUNT_EMAIL --audiences='\u003cvar translate=\"no\"\u003eAUDIENCE\u003c/var\u003e')\n ```\n\n Replace:\n - \u003cvar translate=\"no\"\u003eSERVICE_ACCOUNT_EMAIL\u003c/var\u003e with the email of the service account. It ends with `.iam.gserviceaccount.com`.\n - \u003cvar translate=\"no\"\u003eAUDIENCE\u003c/var\u003e with the custom audience value that you set on the service.\n2. Call the endpoint of the service with that ID token\n\n ```bash\n curl -H \"Authorization: Bearer ${TOKEN}\" ENDPOINT\n ```\n\n Replace \u003cvar translate=\"no\"\u003eENDPOINT\u003c/var\u003e with the endpoint to reach your service, for\n example its custom domain or `.run.app` URL.\n3. Confirm that the request is authorized and you see the expected response of\n your service."]]