我在理解Java安全模型時有些困惑。在我${JDK_HOME}/jre/lib/security/java.policy
文件,我可以看到如下條目:在Java中更改系統屬性 - 安全策略文件
grant {
// Allows any thread to stop itself using the java.lang.Thread.stop()
// method that takes no argument.
// Note that this permission is granted by default only to remain
// backwards compatible.
// It is strongly recommended that you either remove this permission
// from this policy file or further restrict it to code sources
// that you specify, because Thread.stop() is potentially unsafe.
// See "http://java.sun.com/notes" for more information.
permission java.lang.RuntimePermission "stopThread";
// allows anyone to listen on un-privileged ports
permission java.net.SocketPermission "localhost:1024-", "listen";
// "standard" properies that can be read by anyone
permission java.util.PropertyPermission "java.vm.version", "read";
..... .....
最後一行,上面寫着:permission java.util.PropertyPermission "java.vm.version", "read";
我把它解釋爲:運行在虛擬機有權限讀取屬性「Java程序java.vm.version」
按照這種理解我寫了一個示例程序只是爲了檢查,如果我得到任何運行時錯誤。如果我改變這個屬性:
System.setProperty("java.vm.version", "my.jvm.version.2345");
System.out.println(System.getProperty("java.vm.version"));
沒有錯誤;而不是System.out.println
顯示我的修改後的值,即my.jvm.version.2345
這是否意味着在java.policy
中設置的策略不起作用,我在這裏丟失了什麼?
謝謝安德魯和我太同意你了。我只是好奇地看到,政策文件模型是如何工作的? – Vicky 2011-04-15 01:17:55
@Vicky我從來沒有搞錯政策文件足以告訴你! ;) – 2011-04-15 01:56:38
哈哈!所以你告訴人們不要亂搞政策文件,因爲你自己不明白它們? – 2013-04-08 18:31:43