Java安全策略

时间:2017-09-01 15:04:56

标签: java security policy

我正在尝试使用安全策略通过java运行自动化测试:

grant {
    permission java.util.PropertyPermission "*",                                     "read,write";

    permission java.io.FilePermission "${user.dir}",                 "read,write,delete,execute";
    permission java.io.FilePermission "${user.dir}${/}*",            "read,write,delete,execute";
    permission java.io.FilePermission "${user.dir}${/}testtmp",      "read,write,delete,execute";
    permission java.io.FilePermission "${user.dir}${/}testtmp${/}*", "read,write,delete,execute";
};

我想要实现的是jar可以

  • 只读当前目录
  • 在当前目录中创建名为result.json的文件 写信给她
  • 创建一个名为testtmp的目录,并在其中创建,读取和写入文件,以及能够删除和重新创建该文件夹

所以现在发生的事情就是当我跑步时

File execdir = new File("testtmp");
if(execdir.exists()) {
     System.out.println("execdir.getAbsolutePath() + " exists already");
}
...
Files.walk(execdir.toPath(), FileVisitOption.FOLLOW_LINKS)
     .sorted(Comparator.reverseOrder())
     .map(Path::toFile)
     .peek(System.out::println)
     .forEach(File::delete);
几次。我进入if-case,因为文件夹不知何故无法删除。但是当我在没有政策的情况下运行jar时,一切运行良好。

有没有人有想法?

编辑:我尝试了另一个政策文件:

grant {
    permission java.util.PropertyPermission "*",                            "read,write";

    permission java.io.FilePermission       "${user.dir}",                  "read";
    permission java.io.FilePermission       "${user.dir}${/}*",             "read,execute";
    permission java.io.FilePermission       "${user.dir}${/}result.json",   "read,write,delete";
    permission java.io.FilePermission       "${user.dir}${/}testtmp",       "read,write,delete";
    permission java.io.FilePermission       "${user.dir}${/}testtmp${/}*",  "read,write,delete";
};

给了我一个java.security.AccessControlException: access denied ("java.io.FilePermission" "<<ALL FILES>>" "execute")

编辑#2:Ookay我终于通过使用:

更进了一步
grant {
    permission java.util.PropertyPermission "*",                                  "read";

    permission java.io.FilePermission       "${user.dir}${/}testtmp",             "read,write,delete";
    permission java.io.FilePermission       "${user.dir}${/}testtmp${/}-",        "read,write,delete";
    permission java.io.FilePermission       "${user.dir}${/}result.json",         "read,write";
    permission java.io.FilePermission       "/usr/bin/java",                      "read,execute";
    permission java.io.FilePermission       "${user.dir}${/}java.policy",         "read";      
    permission java.io.FilePermission       "${user.dir}${/}build${/}libs${/}-",  "read,execute";
};

但现在我收到java.lang.NoClassDefFoundError: Could not initialize class .. - 错误。

0 个答案:

没有答案