# 示例策略文件 > 原文: [https://docs.oracle.com/javase/tutorial/security/userperm/policy.html](https://docs.oracle.com/javase/tutorial/security/userperm/policy.html) 以下是想要运行`ExampleGame`的用户的完整策略文件。 此处未描述策略文件语法;如果您有兴趣,请参阅[默认策略实现和策略文件语法](https://docs.oracle.com/javase/8/docs/technotes/guides/security/PolicyFiles.html)页面。 你不需要知道语法;您可以随时使用策略工具创建策略文件,如[创建策略文件](../tour1/index.html),[控制应用程序快速浏览](../tour2/index.html)和[签名代码及授予权限](../toolsign/index.html)所示]课程。 下面是示例策略文件,后面是各个条目的说明。假使,假设 * 策略文件位于 Kim 的计算机上,Kim 的密钥库名为`kim.keystore`。 * `ExampleGame`已由游戏创建者 Terry 的私钥签名,相应的公钥位于`"terry"`别名的密钥库条目中。 * `HighScore`和`HighScorePermissions`类由实现它们的人(Chris)的私钥签名,相应的公钥在`"chris"`别名的密钥库条目中。 这是政策文件: [`kim.policy`](examples/kim.policy) ```java keystore "kim.keystore"; // Here is the permission ExampleGame needs. // It grants code signed by "terry" the // HighScorePermission, if the // HighScorePermission was signed by "chris" grant SignedBy "terry" { permission com.scoredev.scores.HighScorePermission "ExampleGame", signedBy "chris"; }; // Here is the set of permissions the HighScore // class needs: grant SignedBy "chris" { // The HighScore class needs permission to read // "user.home" to find the location of the // highscore file permission java.util.PropertyPermission "user.home", "read"; // It needs permission to read and write the // high score file itself permission java.io.FilePermission "${user.home}${/}.highscore", "read,write"; // It needs to get granted its own permission, // so it can call checkPermission // to see if its caller has permission. // Only grant it the permission // if the permission itself was signed by // "chris" permission com.scoredev.scores.HighScorePermission "*", signedBy "chris"; }; ``` ## 密钥库条目 _ 密钥库*是密钥和证书的存储库,用于查找策略文件中指定的签名者的公钥(本例中为`"terry"`和`"chris"`)。 `keytool`实用程序用于创建和管理密钥库。 对于本课,假设 Kim 想要玩`ExampleGame`。如果 Kim 的密钥库名为`kim.keystore`,那么 Kim 的策略文件在一开始就需要以下行: ```java keystore "kim.keystore"; ``` ## ExampleGame 条目 策略文件条目为来自特定*代码源*的代码指定一个或多个权限 - 来自特定位置(URL)的代码,或由特定实体签名的代码,或两者。 我们的策略文件需要每个游戏的条目,授予由该游戏的创建者的密钥签名的代码`HighScorePermission`,其名称是游戏名称。该权限允许游戏调用`HighScore`方法来获取或更新用户对该特定游戏的高分值。 `ExampleGame`所需的条目是: ```java grant SignedBy "terry" { permission com.scoredev.scores.HighScorePermission "ExampleGame", signedBy "chris"; }; ``` 要求`"terry"`签署`ExampleGame`使 Kim 知道该游戏是 Terry 开发的实际游戏。为了实现这一点,Kim 必须已经使用别名`"terry"`将 Terry 的公钥证书存储到`kim.keystore`中。 请注意,`HighScorePermission`需要由实际执行该权限的人`"chris"`签名。这确保`ExampleGame`被授予`"chris"`实现的实际权限,而不是其他人。和以前一样,为了工作,Kim 必须已经使用别名`"chris"`将 Chris 的公钥证书存储到`kim.keystore`中。 ## HighScore Entry 策略文件中的最后一个条目授予`HighScore`类的权限。更具体地说,它授予由`"chris"`签名的代码的权限,后者创建并签署了该类。要求类由`"chris"`签名确保当`ExampleGame`调用此类来更新用户的高分时,`ExampleGame`确定它正在使用`"chris"`实现的原始类。 要为任何调用它的游戏更新用户的高分值,`HighScore`类需要三个权限: ### 1.读取`"user.home"`属性值的权限。 `HighScore`类将用户的高分值存储在用户主目录的`.highscore`文件中。因此,这个类需要一个`java.util.PropertyPermission`,它允许它读取`"user.home"`属性值,以找出用户主目录所在的确切位置: ```java permission java.util.PropertyPermission "user.home", "read"; ``` ### 2.读取和写入高分文件本身的权限。 需要此权限,因此`HighScore` `getHighScore`和`setHighScore`方法可以访问用户的`.highscore`文件,分别获取或设置当前游戏的当前高分。 这是必需的权限: ```java permission java.io.FilePermission "${user.home}${/}.highscore", "read,write"; ``` 注意:符号`${propName}`指定属性的值。因此,`${user.home}`将被`"user.home"`属性的值替换。符号`${/}`是一种与平台无关的指定文件分隔符的方法。 ### 3.所有 HighScorePermissions(即任何名称的 HighScorePermissions)。 需要此权限,以便`HighScore`检查以确保调用游戏已被授予`HighScorePermission`,其名称是游戏名称将起作用。也就是说,`HighScore`类必须*也*被授予权限,因为权限检查要求当前堆栈上的所有代码都具有指定的权限。 这是必需的权限: ```java permission com.scoredev.scores.HighScorePermission "*", signedBy "chris"; ``` 和以前一样,`HighScorePermission`本身需要由实际执行权限的人`"chris"`签名。