在Java中更改系统属性 - 安全策略文件

时间:2011-04-14 17:23:57

标签: java security

我对理解Java安全模型感到困惑。在我的${JDK_HOME}/jre/lib/security/java.policy文件中,我可以看到以下条目:

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";

我将其解释为:在VM中运行的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中设置的政策不起作用,我在这里缺少什么?

2 个答案:

答案 0 :(得分:9)

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

答案 1 :(得分:2)

不要浪费时间搞乱政策文件。它们很难让程序员做对,而对最终用户来说几乎是不可能的。

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