地址是:http://docs.oracle.com/javase/tutorial/security/tour1/wstep1.html
主要是针对运行java代码的用户而言。让Java代码运行在Secure Manager的管理下,只有在Policy File中声明的权限,代码才可以执行相应的操作,以此来建立一个沙箱。
估计这也是Android的Permission授权机制也是这个哦。
policytool工具:在sdk中D:\Program Files\Java\jdk1.6.0_43\bin\policytool.exe D:\Program Files\Java\jdk1.6.0_43\jre\bin\policytool.exe
PolicyFile的后缀是.java.policy ,默认文件路径是D:\Program Files\Java\jdk1.6.0_43\jre\lib\security\java.policy
System.out.println("About to get os.name property value"); s = System.getProperty("os.name", "not specified"); System.out.println(" The name of your operating system is: " + s); System.out.println("About to get java.version property value"); s = System.getProperty("java.version", "not specified"); System.out.println(" The version of the JVM you are running is: " + s); System.out.println("About to get user.home property value"); s = System.getProperty("user.home", "not specified"); System.out.println(" Your user home directory is: " + s); System.out.println("About to get java.home property value"); s = System.getProperty("java.home", "not specified"); System.out.println(" Your JRE installation directory is: " + s)
About to get os.name property value The name of your operating system is: Windows 7 About to get java.version property value The version of the JVM you are running is: 1.7.0_71 About to get user.home property value Your user home directory is: C:\Users\AlbertSnow About to get java.home property value Your JRE installation directory is: D:\Program Files\Java\jdk1.7.0_71\jre
用: java -Djava.security.manager GetProps (-D,这个参数用于设置系统的键值对;改命令使Java运行在安全机制下)
About to get os.name property value The name of your operating system is: Windows 7 About to get java.version property value The version of the JVM you are running is: 1.7.0_71 About to get user.home property value Caught exception java.security.AccessControlException: access denied ("java.util.PropertyPermission" "user.home" "read")
当程序运行时,系统会加载默认的policy file(策略文件)并赋予所有代码,访问普通属性的权限。"os.name","java.version" 之所以可以显示是因为他们不是安全敏感性的属性。
“user.home”、 “java.home"不在系统policy file赋予代码读权限的属性。
系统默认的Policy file:
java.home\lib\security\java.policy
java.home/lib/security/java.policy
java.home是指安装jre的目录
1.在cmd中打开policytool
2.跟记事本一样在C:/Test/下创建PolicyFile: examplepolicy
cmd中打开policytool
CodeBase是授权对象以URL的格式。如果GetProps.java在C:/Test文件下,CodeBase就是:file:/c:/Test/
SignedBy就是要授予的权限:java.util.PropertyPermission java.home,"read" (选完以后的格式)
方法1:
java -Djava.security.manager -Djava.security.policy=examplepolicy GetProps
(注意:在C:/Test/文件夹下运行,否则找不到这个文件了)
方法2:
D:\Program Files\Java\jdk1.6.0_43\jre\lib\security\java.security
java.security文件相当于一个配置文件,java会自动载入:
填写一个键值对:
policy.url.3=file:/C:/Test/examplepolicy
就ok了,注意那个数字 3,是从现有的数字往下递推的。如果现有最大是4,那你就写5
运行时:
java -Djava.security.manager GetProps
版权声明:本文为博主原创文章,未经博主允许不得转载。
Java Security安全系列文档翻译笔记————PolicyFile
原文地址:http://blog.csdn.net/zhjali123/article/details/47275463