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

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

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

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

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

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

gcloud

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

    使用 --session-length 标志设置会话时长。该值必须为 0s,或介于 1 小时到 24 小时之间。指定时长(以小时为单位)。 例如,使用“12h”可将会话时长设置为 12 小时。 使用 --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)。该值必须为 0s 或介于 3600s(1 小时)到 86400s(24 小时)之间。如果设置为 0s,则 sessionLengthEnabled 必须设置为 false 或留空。您不能将 sessionLength 设置为 0s,也不能将 sessionLengthEnabled 设置为 true。
  • sessionReauthMethod 可以是 LOGIN、PASSWORD 或 SECURITY_KEY。
  • 使用 scopedAccessSettings 定义应用专属会话控制。 如需了解详情,请参阅为特定应用定义配置。

Terraform

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

  • session_length:会话时长(以秒为单位)。例如,3600s 会将会话时长设置为 1 小时。末尾必须包含 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 Directory 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。以下示例展示了如何创建会话控制,要求每小时对使用 SECURITY_KEY 的 Google Cloud SDK 重新进行身份验证,并每 4 小时对使用 LOGIN 的 Google Cloud 控制台重新进行身份验证。

通过 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 Directory API 群组 ID。请勿使用群组电子邮件地址或 "groups/..." 资源名称。请改为传递原始 ID 或使用 trimprefix(google_cloud_identity_group.example.id, "groups/")。

覆盖 Google Cloud 应用的默认 16 小时会话时长

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

如需替换此默认政策并为这些应用解除会话控制(使会话时长实际上无限长),您可以创建一个云绑定,其中 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 Directory API 群组 ID。请勿使用群组邮箱或 "groups/..." 资源名称。请改为传递原始 ID 或使用 trimprefix(google_cloud_identity_group.example.id, "groups/")。