標籤:java sdk 安全 文檔
地址是: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
測試類別GetProps.java
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)
通過正常的編譯執行: java GetProps 是可以全部得到屬性的。
About to get os.name property value The name of your operating system is: Windows 7About to get java.version property value The version of the JVM you are running is: 1.7.0_71About to get user.home property value Your user home directory is: C:\Users\AlbertSnowAbout 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 7About to get java.version property value The version of the JVM you are running is: 1.7.0_71About to get user.home property valueCaught exception java.security.AccessControlException: access denied ("java.util.PropertyPermission" "user.home" "read")
Security-Sensitive Properties:(安全敏感性屬性)
當程式運行時,系統會載入預設的policy file(策略檔案)並賦予所有代碼,訪問普通屬性的許可權。"os.name","java.version" 之所以可以顯示是因為他們不是安全敏感性的屬性。
“user.home”、 “java.home"不在系統policy file賦予代碼讀許可權的屬性。
系統預設的Policy file:
- Windows:
java.home\lib\security\java.policy
- UNIX:
java.home/lib/security/java.policy
java.home是指安裝jre的目錄
建立自訂的PolicyFile:
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" (選完以後的格式)
使用該PolicyFile的授權在安全模式下運行:
方法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