2011-04-14 53 views
3

我在理解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中设置的策略不起作用,我在这里丢失了什么?

回答

9

java.policy文件未在正常的java进程中使用。您必须先将java进程配置为使用SecurityManager(.e.g在命令行中添加“-Djava.security.manager”)。更多详情here

2

不要浪费时间搞乱政策文件。对于程序员来说,这对他们来说是非常困难的,对最终用户来说几乎是不可能的。

如果一个应用程序。需要信任,对其进行数字签名,并在提示时说服用户信任代码。

+0

谢谢安德鲁和我太同意你了。我只是好奇地看到,政策文件模型是如何工作的? – Vicky 2011-04-15 01:17:55

+0

@Vicky我从来没有搞错政策文件足以告诉你! ;) – 2011-04-15 01:56:38

+0

哈哈!所以你告诉人们不要乱搞政策文件,因为你自己不明白它们? – 2013-04-08 18:31:43