如何在NetBeans中为特定应用程序设置security.policy文件?

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

设置特定安全策略的最简单方法是通过运行时参数.例如,这就是我们在这里针对同一问题所做的事情:

  1. 打开"项目属性 - >运行"
  2. 选择运行时配置
  3. 编辑运行时配置的"VM选项"
  4. 添加以下内容:

    -Djava.security.manager -Djava.security.policy=src/dir1/dir2/important.policy

src/dir1/dir2/important.policy在您的示例中您将更改为指向您的文件JAASTest.policy.


Ata*_*nna 5

在设置系统安全管理器之前,请添加安全策略。

根据您给定的代码先添加

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)