授予所需的权限

原文: https://docs.oracle.com/javase/tutorial/security/tour1/wstep2.html

要创建新条目,请单击主策略工具窗口中的添加策略条目按钮。这将显示“策略条目”对话框,如下图所示。

the Policy Entry dialog

策略条目为来自特定代码源的代码指定一个或多个权限 - 来自特定位置(URL)的代码,由特定实体签名的代码或两者。

CodeBaseSignedBy 文本框指定要授予您将在文件中添加的权限的代码。

  • CodeBase 值表示代码源位置;您授予该位置代码的权限。空 CodeBase 条目表示“任何代码” - 代码的来源无关紧要。
  • SignedBy 值表示存储在密钥库中的证书的别名。该证书中的公钥用于验证代码上的数字签名。您授予对由别名指定的密钥库条目中的公钥对应的私钥签名的代码的权限。 SignedBy 条目是可选的;省略它表示“任何签名者” - 代码是签名还是由谁签名并不重要。

如果您同时拥有 CodeBaseSignedBy 条目,则权限仅授予来自指定位置的代码。命名别名。

您可以从存储示例的位置(URL)授予对所有代码的权限。

在“策略条目”对话框的 CodeBase 文本框中键入以下 URL:

  1. https://docs.oracle.com/javase/tutorial/security/tour1/examples/

注意:这是一个 URL。因此,它必须始终使用斜杠作为分隔符,而不是反斜杠。

SignedBy 文本框留空,因为您不需要对代码进行签名。


Note: To grant the permission to any code (.class file) not just from the directory specified previously but from the security directory and its subdirectories, type the following URL into the CodeBase box:

  1. https://docs.oracle.com/javase/tutorial/security/

您已指定代码的来源( CodeBase ),并且代码不必签名(因为没有 SignedBy 值)。

您现在已指定此策略条目,因此单击“策略条目”对话框中的完成按钮。 “策略工具”窗口现在包含表示策略条目的行,显示CodeBase值。

the PolicyTool window, showing the new policy entry

注意:我们将在下一课中授予对此新策略条目的权限。