您可以透過工作階段控制項,設定使用者獲得存取權後必須重新驗證的頻率,以及是否需要完整登入、僅輸入密碼或使用實體安全金鑰。
您可以套用工作階段控制選項,執行下列操作:
- 強制具備權限的使用者頻繁重新驗證:要求具備進階權限的使用者 (如專案擁有者和帳單管理員) 頻繁重新驗證。
- 為特定應用程式設定較長的工作階段:允許支援的應用程式 (例如 Google Cloud 控制台、Google Cloud SDK 或特定 OAuth 應用程式) 延長工作階段時間,以保留最佳效能所需的大型內容視窗。
定義工作階段長度和重新驗證方式
建立 Access Context Manager 繫結時,您可以定義工作階段控制項。 如要進一步瞭解工作階段控制項,請參閱「使用存取權繫結將政策套用至使用者群組」。
gcloud
-
使用
--session-length旗標設定工作階段持續時間。這個值必須是0s,或是介於 1 小時至 24 小時之間。以小時為單位指定時間長度。舉例來說,如要設定 12 小時的工作階段,請使用「12h」。使用--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 User Access Binding 資源中,設定 session_settings 引數,以設定適用於所有使用者流量的一般工作階段長度控制項:
session_length:工作階段持續時間 (以秒為單位)。舉例來說,3600s會將工作階段時間長度設為 1 小時。結尾必須加上s。session_length_enabled:設為 false 即可停用指定的工作階段設定。session_reauth_method:用於重新整理憑證的驗證挑戰類型。選項包括LOGIN、PASSWORD或SECURITY_KEY。use_oidc_max_age:進階欄位,用於設定工作階段是否遵守選用的 OIDC 最長有效時間參數,如果驗證憑證是 OAuth 權杖,則會指定該參數。
定義工作階段控制項時,系統只會使用與要求相符的最新存取權繫結,解析工作階段控制項設定。
政策設定範例
以下範例說明如何建立工作階段控制項,透過 LOGIN 每 18 小時重新驗證一次,並透過 SECURITY_KEY 每兩小時重新驗證一次特定應用程式 (SENSITIVE_APP_ID)。
預設設定
Google Cloud CLI 指令中的 --level、--session-length 和 --session-reauth-method 標記 (或 API 呼叫的 JSON 主體中對應的欄位),會為 scopedAccessSettings 中未明確定義的所有應用程式設定預設行為。
應用程式專屬設定
YAML 檔案 (或 JSON 內文) 中的 scopedAccessSettings 區段可讓您覆寫特定應用程式的預設設定。在這個範例中,我們使用 SECURITY_KEY 為用戶端 ID 為 SENSITIVE_APP_ID 的應用程式設定兩小時的重新驗證規定。
應用程式專屬設定會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 組織的 IDGROUP_ID:群組金鑰BINDING_FILE_PATH:繫結檔案的路徑SESSION_LENGTH:工作階段長度,例如18h
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 = "{Organization ID}"
group_key = "{Group Key}"
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 = "SENSITIVE_APP_ID"
}
}
}
active_settings {
session_settings {
session_length = "7200s"
session_length_enabled = true
session_reauth_method = "SECURITY_KEY"
use_oidc_max_age = false
}
}
}
}
應用程式的政策設定範例 Google Cloud
您可以為 Google Cloud 應用程式設定應用程式專屬的重新驗證控制項,例如要求 Google Cloud SDK 使用 SECURITY_KEY,而控制台使用 LOGIN。 Google Cloud 以下範例說明如何建立工作階段控制項,規定 Google Cloud SDK 必須每小時重新驗證一次 (使用 SECURITY_KEY),而控制台則必須每 4 小時重新驗證一次 (使用 Google Cloud )。LOGIN
YAML 檔案 (或 JSON 內文) 中的 scopedAccessSettings 區段可讓您覆寫特定應用程式的預設設定。在本範例中,我們使用 SECURITY_KEY 為 Google Cloud SDK 設定一小時的重新驗證規定,並使用 LOGIN 為 Google Cloud 控制台設定四小時的重新驗證規定,同時使用 name 欄位識別這些應用程式。
如要讓特定應用程式免受工作階段控制,請將
sessionLength 欄位設為 0s 或 sessionLengthEnabled
,並將 false 設為 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 組織的 IDGROUP_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 = "{Organization ID}"
group_key = "{Group Key}"
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
}
}
}
}
覆寫 Google Cloud 應用程式的預設 16 小時工作階段長度
自 2026 年 6 月起,系統會為部分 Google Cloud 機構導入 16 小時的預設工作階段長度。這項預設政策不會顯示在 Google 管理控制台中,受影響的 Google Cloud 機構必須使用 Access Context Manager API,為使用者覆寫這項政策。
如要覆寫這項預設政策,並免除這些應用程式的工作階段控制項 (有效延長工作階段長度),您可以建立雲端繫結,並將 Google Cloud SDK 和 Google Cloud 控制台 (「Cloud 控制台」) 應用程式的 sessionLengthEnabled 設為 false。scopedAccessSettings
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 組織的 IDGROUP_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 = "{Organization ID}"
group_key = "{Group Key}"
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
}
}
}
}