在 Java 中更改系统属性 - 安全策略文件 [英] Changing System Property in Java - Security Policy file
问题描述
我对 Java 安全模型的理解有些混乱.在我的 ${JDK_HOME}/jre/lib/security/java.policy
文件中,我可以看到以下条目:
I have some confusion in understanding Java Security model. In my ${JDK_HOME}/jre/lib/security/java.policy
file, I could see below entries:
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'
I interpret it as: The Java Program that runs in the VM has permission to read the property 'java.vm.version'
根据这种理解,我编写了一个示例程序来检查是否出现任何运行时错误 如果我更改此属性:
Following this understanding I wrote a sample program just to check, If I get any run time error If I alter this property:
System.setProperty("java.vm.version", "my.jvm.version.2345");
System.out.println(System.getProperty("java.vm.version"));
没有错误;而是 System.out.println
显示我修改后的值,即 my.jvm.version.2345
There was no error; instead the System.out.println
display my modified value i.e. my.jvm.version.2345
这是否意味着 java.policy
中设置的策略不起作用,我在这里遗漏了什么?
Does that mean policies set in java.policy
is not working, what am i missing here?
推荐答案
java.policy 文件不用于普通的 java 进程.您必须首先配置 java 进程以使用 SecurityManager(例如,将-Djava.security.manager"添加到命令行).更多详细信息此处.
the java.policy file is not used in a normal java process. you must first configure the java process to use a SecurityManager (.e.g add "-Djava.security.manager" to the command line). More details here.
这篇关于在 Java 中更改系统属性 - 安全策略文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!