Mik*_*ike 9 java netbeans security-policy
我遇到了一些麻烦 - 实际上很多 - 试图弄清楚NetBeans如何为特定应用程序读取我的策略文件.请看下面的代码:
public static void main(final String[] args)
{
System.setSecurityManager(new SecurityManager());
System.setProperty("java.security.policy","file:/C:/Users/kBPersonal/Documents/NetBeansProjects/JAASTest/JAASTest.policy");
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame frame = new JAASFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
Run Code Online (Sandbox Code Playgroud)
无论我做什么,我都会收到以下错误,让我知道NetBeans没有读取我的security.policy文件(我甚至将它的位置添加到了主安全.policy文件中C:\Program Files (x86)\Java\jre6\lib\security\java.security
).顺便说一句,第20行是我尝试设置的地方System.setProperty("java.security.policy, ...)
Exception in thread "main" java.security.AccessControlException: access denied (java.util.PropertyPermission java.security.policy write)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:323)
at java.security.AccessController.checkPermission(AccessController.java:546)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
at java.lang.System.setProperty(System.java:725)
at JAASTest.main(JAASTest.java:20)
Run Code Online (Sandbox Code Playgroud)
非常感谢任何和所有的帮助!
小智 18
如果您使用该System.setProperty()
方法添加策略文件,请在创建之前确保它是SecurityManager
.我之前使用SecurityManager
过该System.setProperty()
方法,并在创建SecurityManager
通常的工作之前调用它.
Bob*_*oss 16
设置特定安全策略的最简单方法是通过运行时参数.例如,这就是我们在这里针对同一问题所做的事情:
添加以下内容:
-Djava.security.manager -Djava.security.policy=src/dir1/dir2/important.policy
src/dir1/dir2/important.policy
在您的示例中您将更改为指向您的文件JAASTest.policy
.
在设置系统安全管理器之前,请添加安全策略。
根据您给定的代码先添加
System.setProperty("java.security.policy","file:/C:/Users/kBPersonal/Documents/NetBeansProjects/JAASTest/JAASTest.policy");
Run Code Online (Sandbox Code Playgroud)
然后
System.setSecurityManager(new SecurityManager());
Run Code Online (Sandbox Code Playgroud)