使用AccessController.checkPermission的java目录权限 [英] java directory permissions with AccessController.checkPermission

查看:94
本文介绍了使用AccessController.checkPermission的java目录权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过checkPermission()方法获取目录权限,如代码中所示。

im trying to get directory permissions via the checkPermission() method, as in the code.

private static boolean dirPermissions(String dir, String perm) {
    try {
        AccessController.checkPermission(new FilePermission(dir, perm));
        return true;
    } catch (AccessControlException e) {
        return false;
    }
}

大多数的java(< = 6)文档相关的File类方法(例如canRead(),canWrite()等)表明它们对文件很有用,但不是目录(我错了吗?)。

the java (<=6) documentation for most of the relevant File class methods (e.g. canRead(), canWrite(), etc.) suggest that they are useful for file, but not directories (am i wrong here?).

我没有在服务器上运行代码,或者运行mutliple线程并且需要在它们之间保存acc,或者做任何棘手的事情,它尽可能简单 - 使用本地运行的jvm和代码进行全新的eclipse安装,并且即时访问本地目录。

im not running the code on a server, or running mutliple threads and need to save acc's between them, or doing anything tricky, its as simple as can be - a fresh eclipse install with local running jvm and code, and im accessing local directories.

然而,checkPermission总是为文件夹抛出异常(即访问被拒绝),显然jvm具有读/写权限,因为我可以设法创建文件夹和写入文件等。

however, checkPermission always throws an exception (i.e. access denied) for folders that obviously the jvm has read/write permissions to because i can manage to create folders and write to files etc.

大概它与本地java.policy无关,因为我成功写入文件,所以我拥有必要的安全权限。任何想法......?

presumably it isn't related to the local java.policy because im successfully writing to files, so i have the necessary security permissions. any ideas...?

推荐答案

首先你应该使用 SecurityManager

FilePermission 处理好目录。您可能希望包含下面包含的所有文件和目录。最后使用 / - 。对于非递归包含的文件和目录,请使用 / *

FilePermission copes with directories fine. You probably want to include all the files and directories included below. Use /- at the end for that. Use /* for non-recursive contained files and directories.

checkPermission ProtectionDomain )没有所需的权限,c $ c>将抛出异常。

checkPermission will throw an exception if any frame (or rather its ProtectionDomain) of the current access control context (acc) does not have the required permission.

FilePermission 与操作系统权限无关。

FilePermission has nothing to do with OS permissions.

这篇关于使用AccessController.checkPermission的java目录权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆