新增自訂 JavaCallout 安全性政策

本頁內容適用於 ApigeeApigee Hybrid

查看 Apigee Edge 說明文件。

什麼是自訂 JavaCallout 安全性政策?

JavaCallout 政策可讓您使用 Java 程式碼自訂 API 行為。實作 JavaCallout 時,Apigee 會使用 Java 權限政策,為 API 設定預設權限。在 Apigee Hybrid 中,您可以建立自訂 JavaCallout 安全性政策,變更預設權限。

您可以使用資源建立自訂 JavaCallout 安全性政策,該資源定義於 securityPolicy 類型的資源檔案中。資源是在環境層級設定。資源檔案可以有任何名稱,但您必須在自訂 Java 政策檔案名稱中加入 .policy 後置字串。例如: strict-security.policy

下列指令會新增名為 strict-security.policy 的資源檔案。

curl -X POST "https://apigee.googleapis.com/v1/organizations/my-organization/environments/test/resourcefiles?name=CustomJavaSecurityPolicy&type=securityPolicy"
-H "Authorization: Bearer $TOKEN" \
-H "Content-type: multipart/form-data" \
-F file=@/Users/home/strict-security.policy
安全性政策檔案支援與標準 Java 安全性政策相同的語法;請參閱「 預設政策實作和政策檔案語法」。

建立定義自訂 JavaCallout 政策的安全性政策檔案後,Apigee 執行階段就能偵測到該政策,並使用政策定義的自訂權限。如果沒有自訂 JavaCallout 政策,Apigee 會使用自訂 Java 或 Python 程式碼的預設安全政策。

安全性政策檔案

以下範例顯示安全性政策檔案的內容。

下方的程式碼行會指定自訂 JavaCallout 政策的程式碼位置,並授予目錄所有權限。

Relaxed security policy file for testing
// javacallout code has just read permission in the installed dir and everything below it
  grant codeBase "file:${javacallout.dir}/-"  {
    permission java.security.AllPermission;
}

這些指令行會指定自訂 Jython/Python Callout 的程式碼位置,並授予目錄讀取權限。

// Jython/Python secure
grant codeBase "file:${jython-secure-jar}" {
    // No logging permissions for secure jar. Hence value of the AllExcept target parameter set to 0
    permission com.apigee.securitypolicy.AllExcept "0", "java.io.FilePermission";
    permission java.io.FilePermission "{T}conf_security-policy_install.dir{/T}/lib/-" , "read";
    // Add JRE read permissions to jython. Existing permissions have two formats to java home. Keep the same.
    permission java.io.FilePermission "${java.home}/-", "read,readLink";
    permission java.io.FilePermission "${JAVA_HOME}/-", "read,readLink";
}

範例

下列範例說明如何執行與自訂 JavaCallout 安全性政策相關的特定工作。

建立自訂 JavaCallout 安全性政策

下列指令會建立自訂 JavaCallout 安全性政策,定義於名為 strict-security.policy 的資源檔案中。

curl -X POST "https://apigee.googleapis.com/v1/organizations/my-organization/environments/test/resourcefiles?name=CustomJavaSecurityPolicy&type=securityPolicy"
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-type: multipart/form-data" \
  -F file=@/Users/home/strict-security.policy

查看所有安全性政策

您可以使用下列指令,查看 API 中所有現有的自訂 JavaCallout 安全性政策。

curl -X GET "https://apigee.googleapis.com/v1/organizations/my-organization/environments/test/resourcefiles/securityPolicy" \
  -H "Authorization: Bearer $TOKEN"

查看安全性政策檔案的內容

下列指令會取得個別安全政策檔案的內容,方便您查看。

curl -X GET "https://apigee.googleapis.com/v1/organizations/my-organization/environments/test/resourcefiles/securityPolicy/CustomJavaSecurityPolicy" \
  -H "Authorization: Bearer $TOKEN"

更新環境範圍的政策

下列指令會更新環境範圍的安全性政策。

curl -X PUT "https://apigee.googleapis.com/v1/organizations/my-organization/environments/test/resourcefiles/securityPolicy/CustomJavaSecurityPolicy"
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-type: multipart/form-data" \
  -F file=@/Users/home/strict-security-revised.policy

刪除環境範圍的安全性政策

下列指令會刪除環境範圍的安全政策。

curl -X DELETE https://apigee.googleapis.com/v1/organizations/my-organization/environments/test/resourcefiles/securityPolicy/CustomJavaSecurityPolicy \
  -H "Authorization: Bearer $TOKEN"