为重新身份验证配置会话控制

通过会话控制,您可以配置用户在获得访问权限后必须重新进行身份验证的频率,以及是否需要完整登录、仅密码或硬件安全密钥。

您可以应用会话控制功能来执行以下操作:

  • 强制要求特权用户频繁重新进行身份验证:要求拥有较高权限的用户(例如项目所有者和结算管理员)更频繁地重新进行身份验证。
  • 为某些应用配置更长的会话时长:允许受支持的应用(例如 Google Cloud 控制台、Google Cloud SDK 或特定 OAuth 应用)具有更长的会话时长,以保留实现最佳性能所需的大型上下文窗口。

定义会话时长和重新身份验证方法

您可以在创建 Access Context Manager 绑定时定义会话控制。如需详细了解会话控制,请参阅使用访问权限绑定将政策应用于用户群组。

gcloud

  • 为所有应用设置默认会话控制

    使用 --session-length 标志设置会话时长。对于Google Cloud 用户会话,时长必须为 0s(或 0h),或介于 1 小时到 24 小时之间。Google Cloud 用户会话的时长上限为 24 小时。以小时为单位指定时长。例如,使用 12h 可将会话时长设置为 12 小时。如需将会话时长增加到默认的 16 小时以上,请将 --session-length 设置为最多 24h,或将 --session-length=0 设置为停用会话时长限制。对于员工身份联合延长会话,最长时长为 90 天。使用 --session-reauth-method 标志指定重新身份验证方法。例如,您可以将会话时长设置为 3 小时 (3h),并设置 LOGIN、PASSWORD 或 SECURITY_KEY 重新身份验证方法。

    除非被应用专属设置覆盖,否则此设置将应用于所有应用。

  • 设置应用专用会话控制

    在 YAML 文件中定义 scopedAccessSettings,以使用 clientId 为特定应用指定会话控制。这样,您就可以替换这些应用的默认会话控制。然后,您可以使用 --binding-file flag 传递 YAML 文件。

REST API

在 POST 请求的 JSON 正文中的 sessionSettings 对象内定义 sessionLength 和 sessionReauthMethod 字段,以创建或更新 GcpUserAccessBinding 绑定。

  • sessionLength 是会话时长(以秒为单位),格式为秒数后跟 s(例如 3600s)。对于Google Cloud 用户会话,该值必须为 0s 或介于 3600s(1 小时)到 86400s(24 小时)之间。Google Cloud 用户会话的时长上限为 24 小时 (86400s)。如需将会话时长增加到默认的 16 小时以上,请将 sessionLength 设置为最多 86400s(24 小时),或将 sessionLength 设置为 0s,并将 sessionLengthEnabled 设置为 false,以停用会话限制(会话时长无限制)。如果设置为 0s,则 sessionLengthEnabled 必须设置为 false 或留空。您不能将 sessionLength 设置为 0s,也不能将 sessionLengthEnabled 设置为 true。对于员工身份联合,延长会话最多允许 90 天 (7776000s)。
  • sessionReauthMethod 可以是 LOGIN、PASSWORD 或 SECURITY_KEY。
  • 使用 scopedAccessSettings 定义应用专属会话控制。 如需了解详情,请参阅为特定应用定义配置。

Terraform

在 Terraform Google Cloud 用户访问权限绑定资源中,设置 session_settings 参数,以配置适用于所有用户流量的常规会话时长控制:

  • session_length:练习时长(以秒为单位)。例如,3600s 会将会话时长设置为 1 小时,86400s 会将会话时长上限设置为 24 小时。末尾必须包含 s。
  • session_length_enabled:设置为 false 可停用会话限制。
  • session_reauth_method:用于刷新凭据的身份验证质询类型。选项包括 LOGIN、PASSWORD 或 SECURITY_KEY。
  • use_oidc_max_age:一个高级字段,用于配置会话是否遵循可选的 OIDC 最长有效期参数,如果身份验证凭证是 OAuth 令牌,则会指定该参数。

定义会话控制时,系统在解析会话控制设置时只会使用与请求匹配的最新创建的访问权限绑定。

政策配置示例

以下示例展示了如何创建会话控制,要求每 18 小时使用 LOGIN 重新进行身份验证,并要求每两小时使用 SECURITY_KEY 为特定应用 (SENSITIVE_APP_ID) 重新进行身份验证。

默认设置

Google Cloud CLI 命令中的 --level、--session-length 和 --session-reauth-method 标志(或 API 调用的 JSON 正文中的相应字段)会为 scopedAccessSettings 中未明确定义的所有应用设置默认行为。

应用专属设置

借助 YAML 文件(或 JSON 正文)中的 scopedAccessSettings 部分,您可以替换特定应用的默认设置。在此示例中,我们为客户端 ID 为 SENSITIVE_APP_ID 的应用设置了 SECURITY_KEY 的两小时重新身份验证要求。

scopedAccessSettings 中的应用专属设置会完全覆盖默认(全局)会话控制设置。如果为某个应用定义了限定范围的设置,则默认会话控制设置不适用于该应用。

如需在全局控制处于活动状态时使某些应用免于会话控制,您必须明确将其添加到 scopedAccessSettings,并将 sessionLength 字段设置为 0s,将 sessionLengthEnabled 设置为 false。

gcloud

以下示例展示了会话设置配置:

scopedAccessSettings:
- scope:
    clientScope:
      restrictedClientApplication:
        clientId: SENSITIVE_APP_ID
  activeSettings:
    sessionSettings:
      sessionLength: 7200s
      sessionReauthMethod: SECURITY_KEY
      sessionLengthEnabled: true

创建访问权限绑定:

gcloud access-context-manager cloud-bindings create \
    --organization=ORG_ID \
    --group-key=GROUP_ID \
    --binding-file=BINDING_FILE_PATH \
    --session-length=SESSION_LENGTH \
    --session-reauth-method LOGIN

替换以下内容:

  • ORG_ID: Google Cloud组织的 ID

  • GROUP_ID:唯一的 Cloud Identity 群组 ID

  • BINDING_FILE_PATH:绑定文件的路径

  • SESSION_LENGTH:会话时长,例如 18h

如需获取唯一群组 ID,请运行以下命令:

gcloud identity groups describe GROUP_EMAIL

将 GROUP_EMAIL 替换为群组的邮箱。 唯一群组 ID 是输出中 name 字段内 groups/ 之后返回的值。例如,groups/01gf8i8311xalqg 中的唯一群组 ID 为 01gf8i8311xalqg。

REST API

API 请求的 JSON 正文示例:

{
  "groupKey": "GROUP_ID",
  "sessionSettings": {
    "sessionLength": "64800s",
    "sessionReauthMethod": "LOGIN",
    "sessionLengthEnabled": true
  },
  "scopedAccessSettings": [
    {
      "scope": {
        "clientScope": {
          "restrictedClientApplication": {
            "clientId": "SENSITIVE_APP_ID"
          }
        }
      },
      "activeSettings": {
        "sessionSettings": {
          "sessionLength": "7200s",
          "sessionReauthMethod": "SECURITY_KEY",
          "sessionLengthEnabled": true
        }
      }
    }
  ]
}

按以下格式构建 POST 请求:

POST https://accesscontextmanager.googleapis.com/v1/organizations/ORG_ID/gcpUserAccessBindings

将 ORG_ID 替换为 Google Cloud组织的 ID。

Terraform

如需指定全局和特定应用适用的会话控制,请设置 session_settings 和 scoped_access_settings 实参:

    resource "google_access_context_manager_gcp_user_access_binding" "gcp_user_access_binding" {
      organization_id = "ORG_ID"
      group_key = "GROUP_ID"
      session_settings {
        session_length = "64800s"
        session_length_enabled = true
        session_reauth_method = "LOGIN"
        use_oidc_max_age = false
      }
      scoped_access_settings {
        scope {
          client_scope {
            restricted_client_application {
              client_id = "CLIENT_ID"
            }
          }
        }
        active_settings {
          session_settings {
            session_length = "7200s"
            session_length_enabled = true
            session_reauth_method = "SECURITY_KEY"
            use_oidc_max_age = false
          }
        }
      }
    }

替换以下内容:

  • ORG_ID: Google Cloud组织的数字 ID。

  • GROUP_ID:原始 Cloud Identity 或 Google Workspace 目录 API 群组 ID。请勿使用群组邮箱或 "groups/..." 资源名称。请改为传递原始 ID 或使用 trimprefix(google_cloud_identity_group.example.id, "groups/")。

  • CLIENT_ID:应用的 OAuth 2.0 客户端 ID。

Google Cloud 应用的政策配置示例

您可以为Google Cloud 应用配置特定于应用的重新身份验证控制,例如要求 Google Cloud SDK 使用 SECURITY_KEY, Google Cloud 控制台使用 LOGIN。以下示例展示了如何创建会话控制,要求每小时对 Google Cloud SDK 进行一次重新身份验证(使用 SECURITY_KEY),并每 4 小时对 Google Cloud 控制台进行一次重新身份验证(使用 LOGIN)。

借助 YAML 文件(或 JSON 正文)中的 scopedAccessSettings 部分,您可以替换特定应用的默认设置。在此示例中,我们使用 name 字段来标识这些应用,为 Google Cloud SDK 设置了 1 小时重新身份验证要求(使用 SECURITY_KEY),为 Google Cloud 控制台设置了 4 小时重新身份验证要求(使用 LOGIN)。

如需将某些应用从会话控制中排除,请将 sessionLength 字段设置为 0s,或将 sessionLengthEnabled 设置为 false。系统随后会忽略 sessionReauthMethod 方法。

gcloud

以下示例展示了会话设置配置:

scopedAccessSettings:
- scope:
    clientScope:
      restrictedClientApplication:
        name: Google Cloud SDK
  activeSettings:
    sessionSettings:
      sessionLength: 3600s
      sessionReauthMethod: SECURITY_KEY
      sessionLengthEnabled: true
- scope:
    clientScope:
      restrictedClientApplication:
        name: Cloud Console
  activeSettings:
    sessionSettings:
      sessionLength: 14400s
      sessionReauthMethod: LOGIN
      sessionLengthEnabled: true

创建访问权限绑定:

gcloud access-context-manager cloud-bindings create \
    --organization=ORG_ID \
    --group-key=GROUP_ID \
    --binding-file=BINDING_FILE_PATH

替换以下内容:

  • ORG_ID: Google Cloud组织的 ID

  • GROUP_ID:唯一的 Cloud Identity 群组 ID

  • BINDING_FILE_PATH:绑定文件的路径

REST API

API 请求的 JSON 正文示例:

{
  "groupKey": "GROUP_ID",
  "scopedAccessSettings": [
    {
      "scope": {
        "clientScope": {
          "restrictedClientApplication": {
            "name": "Google Cloud SDK"
          }
        }
      },
      "activeSettings": {
        "sessionSettings": {
            "sessionLength": "3600s",
            "sessionReauthMethod": "SECURITY_KEY",
            "sessionLengthEnabled": true
          }
      }
    },
    {
      "scope": {
        "clientScope": {
          "restrictedClientApplication": {
            "name": "Cloud Console"
          }
        }
      },
      "activeSettings": {
        "sessionSettings": {
            "sessionLength": "14400s",
            "sessionReauthMethod": "LOGIN",
            "sessionLengthEnabled": true
          }
      }
    }
  ]
}

按以下格式构建 POST 请求:

POST https://accesscontextmanager.googleapis.com/v1/organizations/ORG_ID/gcpUserAccessBindings

将 ORG_ID 替换为 Google Cloud组织的 ID。

Terraform

如需为 Google Cloud SDK 和 Google Cloud 控制台指定会话时长设置,请设置适当的 scoped_access_settings 参数:

    resource "google_access_context_manager_gcp_user_access_binding" "gcp_user_access_binding" {
      organization_id = "ORG_ID"
      group_key = "GROUP_ID"
      scoped_access_settings {
        scope {
          client_scope {
            restricted_client_application {
              name = "Google Cloud SDK"
            }
          }
        }
        active_settings {
          session_settings {
            session_length = "3600s"
            session_length_enabled = true
            session_reauth_method = "SECURITY_KEY"
            use_oidc_max_age = false
          }
        }
      }
      scoped_access_settings {
        scope {
          client_scope {
            restricted_client_application {
              name = "Cloud Console"
            }
          }
        }
        active_settings {
          session_settings {
            session_length = "14400s"
            session_length_enabled = true
            session_reauth_method = "LOGIN"
            use_oidc_max_age = false
          }
        }
      }
    }

替换以下内容:

  • ORG_ID: Google Cloud组织的数字 ID。

  • GROUP_ID:原始 Cloud Identity 或 Google Workspace 目录 API 群组 ID。请勿使用群组邮箱或 "groups/..." 资源名称。请改为传递原始 ID 或使用 trimprefix(google_cloud_identity_group.example.id, "groups/")。

为以下用户替换默认的 16 小时会话时长:

Google Cloud applications

自 2026 年 6 月起,我们为部分 Google Cloud 组织引入了 16 小时的默认会话时长。此默认政策在 Google 管理控制台中不可见,受影响的 Google Cloud 组织必须使用 Access Context Manager API 为其用户替换此政策。

如需替换此默认政策,您可以将会话时长增加到最长 24 小时 (86400s),也可以通过使用 scopedAccessSettings 创建云绑定来免除这些应用的会话控制(使会话时长实际上无限长),其中 sessionLengthEnabled 针对 Google Cloud SDK 和 Google Cloud 控制台(“Cloud 控制台”)应用均设置为 false。

gcloud

以下示例展示了用于为这些应用停用会话控制的 YAML 配置:

scopedAccessSettings:
- scope:
    clientScope:
      restrictedClientApplication:
        name: Google Cloud SDK
  activeSettings:
    sessionSettings:
      sessionLength: 0s
      sessionReauthMethod: LOGIN
      sessionLengthEnabled: false
- scope:
    clientScope:
      restrictedClientApplication:
        name: Cloud Console
  activeSettings:
    sessionSettings:
      sessionLength: 0s
      sessionReauthMethod: LOGIN
      sessionLengthEnabled: false

使用此文件创建或更新访问权限绑定:

gcloud access-context-manager cloud-bindings create \
    --organization=ORG_ID \
    --group-key=GROUP_ID \
    --binding-file=BINDING_FILE_PATH

替换以下内容:

  • ORG_ID: Google Cloud组织的 ID

  • GROUP_ID:唯一的 Cloud Identity 群组 ID(例如,包含您希望此替换设置应用到的所有用户的群组的 ID)

  • BINDING_FILE_PATH:绑定文件的路径

REST API

API 请求的 JSON 正文示例:

{
  "groupKey": "GROUP_ID",
  "scopedAccessSettings": [
    {
      "scope": {
        "clientScope": {
          "restrictedClientApplication": {
            "name": "Google Cloud SDK"
          }
        }
      },
      "activeSettings": {
        "sessionSettings": {
          "sessionLength": "0s",
          "sessionReauthMethod": "LOGIN",
          "sessionLengthEnabled": false
        }
      }
    },
    {
      "scope": {
        "clientScope": {
          "restrictedClientApplication": {
            "name": "Cloud Console"
          }
        }
      },
      "activeSettings": {
        "sessionSettings": {
          "sessionLength": "0s",
          "sessionReauthMethod": "LOGIN",
          "sessionLengthEnabled": false
        }
      }
    }
  ]
}

按以下格式构建 POST 请求:

POST https://accesscontextmanager.googleapis.com/v1/organizations/ORG_ID/gcpUserAccessBindings

将 ORG_ID 替换为 Google Cloud组织的 ID。

Terraform

如需指定为 Google Cloud SDK 和 Google Cloud 控制台停用会话控制,请使用 session_length_enabled = false 设置适当的 scoped_access_settings 参数:

    resource "google_access_context_manager_gcp_user_access_binding" "gcp_user_access_binding" {
      organization_id = "ORG_ID"
      group_key = "GROUP_ID"
      scoped_access_settings {
        scope {
          client_scope {
            restricted_client_application {
              name = "Google Cloud SDK"
            }
          }
        }
        active_settings {
          session_settings {
            session_length = "0s"
            session_length_enabled = false
            session_reauth_method = "LOGIN"
            use_oidc_max_age = false
          }
        }
      }
      scoped_access_settings {
        scope {
          client_scope {
            restricted_client_application {
              name = "Cloud Console"
            }
          }
        }
        active_settings {
          session_settings {
            session_length = "0s"
            session_length_enabled = false
            session_reauth_method = "LOGIN"
            use_oidc_max_age = false
          }
        }
      }
    }

替换以下内容:

  • ORG_ID: Google Cloud组织的数字 ID。

  • GROUP_ID:原始 Cloud Identity 或 Google Workspace 目录 API 群组 ID。请勿使用群组邮箱或 "groups/..." 资源名称。请改为传递原始 ID 或使用 trimprefix(google_cloud_identity_group.example.id, "groups/")。