【发布时间】:2014-02-13 12:45:12
【问题描述】:
我正在尝试按照 oracle 教程使用安全管理器来授予或拒绝对 Java 应用程序中系统资源的访问权限:http://docs.oracle.com/javase/tutorial/security/tour2/index.html
我想授予我的 NetBeans 项目对“java.home”和“user.home”属性的访问权限。 为此,我生成了一个名为 $HOME/.java.policy 的私有策略文件:
grant codeBase "file:/home/myself/NetBeansProjects/" {
permission java.util.PropertyPermission "user.home", "read";
permission java.util.PropertyPermission "java.home", "read";
};
codeBase 指向我的 NetBeans 项目所在的目录 (/home/myself/NetBeansProjects/)
在 java.security 我定义了以下网址:
# The default is to have a single system-wide policy file,
# and a policy file in the user's home directory.
policy.url.1=file:${java.home}/lib/security/java.policy
policy.url.2=file:${user.home}/.java.policy
现在我的一个 NetBeans 项目正在尝试读取“java.home”和“user.home”属性:
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 程序,我仍然会收到 AccessControlException:
$ pwd
/home/myself/NetBeansProjects/GetProps/src
$ java -Djava.security.manager getprops.GetProps
About to get user.home property value
Caught exception java.security.AccessControlException: access denied ("java.util.PropertyPermission" "user.home" "read")
我想知道问题是否存在于我的私有策略文件中的 codeBase 路径中?
我正在测试的系统和软件: NetBeans IDE 7.4(内部版本 201310111528) 爪哇:1.7.0_45; OpenJDK 客户端虚拟机 24.45-b08 运行时:OpenJDK 运行时环境 1.7.0_45-b31 系统:在 i386 上运行的 Linux 版本 3.12.3-1-ARCH; UTF-8; es_ES (nb)
【问题讨论】:
-
您是否尝试使用
java -Djava.security.manager -Djava.security.policy=$HOME/.java.policy getprops.GetProps运行您的程序以确保您的策略文件被真正使用? -
是的,我得到了同样的结果
-
旁注,您为什么要与安全经理一起运行?你真的需要吗?
-
当您说“从命令行”时:它是否在 Netbeans 内部工作?
标签: java netbeans securitymanager