samples 策略文件
以下是要运行ExampleGame
的用户的完整策略文件。
这里没有描述策略文件的语法。如果您有兴趣,请参见默认策略实现和策略文件语法页。
您不需要了解语法;您可以始终使用策略工具来创建策略文件,如创建策略文件,快速浏览控制应用和签署代码并授予其权限类中所示。
以下是示例策略文件,后跟各个条 Object 描述。假使,假设
-
该策略文件位于 Kim 的计算机上,而 Kim 的密钥库名为
kim.keystore
。 -
ExampleGame
已由游戏创建者 Terry 的私钥签名,并且相应的公钥位于以"terry"
为别名的密钥库条目中。 -
HighScore
和HighScorePermissions
类由实现它们的人(Chris)的私钥签名,并且相应的公钥位于"chris"
别名的密钥库条目中。
以下是策略文件:kim.policy
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
Util 用于创建和 管理 密钥库。
在本类中,假设 Kim 想玩ExampleGame
。如果 Kim 的密钥库名为kim.keystore
,则 Kim 的策略文件一开始需要以下行:
keystore "kim.keystore";
ExampleGame 条目
策略文件条目为来自特定代码源的代码指定一个或多个权限-来自特定位置(URL)的代码或由特定实体签名的代码,或两者。
我们的 Policy 文件需要为每个游戏提供一个条目,并授予该游戏创建者的密钥HighScorePermission
签名的代码,其名称为游戏名称。该权限允许游戏调用HighScore
方法以获取或更新该特定游戏的用户的高分值。
ExampleGame
所需的条目是:
grant SignedBy "terry" {
permission
com.scoredev.scores.HighScorePermission
"ExampleGame", signedBy "chris";
};
要求ExampleGame
由"terry"
签名可使 Kim 知道该游戏是 Terry 开发的实际游戏。为此,Kim 必须已经使用别名"terry"
将 Terry 的公钥证书存储到kim.keystore
中。
请注意,HighScorePermission
必须由实际实现该权限的人"chris"
签名。这样可以确保向ExampleGame
授予"chris"
实现的实际权限,而不是其他人。和以前一样,Kim 必须已经使用别名"chris"
将 Chris 的公钥证书存储到kim.keystore
中,才能正常工作。
HighScore 条目
策略文件中的最后一个条目授予HighScore
类的权限。更具体地说,它授予由创建并签名该类的"chris"
签名的权限。要求由"chris"
签名的类可确保ExampleGame
调用该类以更新用户的高分时,ExampleGame
可以确定它正在使用"chris"
实现的原始类。
要更新所有要求这样做的游戏的用户高分值,HighScore
类需要三个权限:
1.允许读取“ user.home”属性值。
HighScore
类将用户的高分值存储在用户主目录中的.highscore
文件中。因此,此类需要一个java.util.PropertyPermission
,该类允许它读取"user.home"
属性值以准确找出用户主目录所在的位置:
permission java.util.PropertyPermission
"user.home", "read";
2.允许读写高分文件本身。
需要此权限,以便HighScore
getHighScore
和setHighScore
方法可以访问用户的.highscore
文件以分别获取或设置当前游戏的当前高分。
这是必需的权限:
permission java.io.FilePermission
"${user.home}${/}.highscore", "read,write";
注意:符号${propName}
指定属性的值。因此,${user.home}
将被"user.home"
属性的值替换。 ${/}
是一种独立于平台的指定文件分隔符的方式。
3.所有 HighScorePermissions(即任何名称的 HighScorePermissions)。
需要此权限,以便HighScore
检查以确保已向调用游戏授予名称为游戏名称的HighScorePermission
。也就是说,还必须授予HighScore
类权限,因为权限检查要求当前堆栈上的所有代码都具有指定的权限。
这是必需的权限:
permission com.scoredev.scores.HighScorePermission
"*", signedBy "chris";
和以前一样,HighScorePermission
本身需要由实际实现许可的人"chris"
签名。