【问题标题】:Java add group to directory permission list [duplicate]Java将组添加到目录权限列表[重复]
【发布时间】:2019-06-24 06:37:00
【问题描述】:

我正在努力向特定组授予对我使用file.mkdirs() 创建的目录结构的顶层的权限。我找到了如何更改目录的所有者,但这不是我需要的,我需要将一个组添加到有权访问该目录的人的列表中。具体来说,这适用于我正在创建的 Windows 系统和文件夹结构。我需要将组添加到文件夹的安全部分,以允许它们读取和写入所有子目录。这甚至可能吗?

【问题讨论】:

标签: java permissions directory


【解决方案1】:

感谢@Robyte 为我指明了正确的方向。下面是未完成的代码,我需要做一些重构,但它得到了我需要的大部分内容。

try {
    Path path = file.toPath();

    AclFileAttributeView aclView = Files.getFileAttributeView(path, AclFileAttributeView.class);
    if(aclView == null)
    {
        System.out.format("ACL View not supported");
    }

    UserPrincipal up = FileSystems.getDefault().getUserPrincipalLookupService().lookupPrincipalByName("username");
    Set<AclEntryPermission> aep = EnumSet.of(READ_DATA,WRITE_DATA);

    AclEntry builder = AclEntry.newBuilder().setType(AclEntryType.ALLOW).
            setPrincipal(up).setPermissions(aep).build();

    List<AclEntry> acl = aclView.getAcl();
    acl.add(0,builder);
    aclView.setAcl(acl);
} catch (IOException ex) {
    Logger.getLogger(PermissionsUtil.class.getName()).log(Level.SEVERE, null, ex);
}

【讨论】:

    猜你喜欢
    • 2020-07-16
    • 2020-11-08
    • 2020-08-04
    • 1970-01-01
    • 2016-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-25
    相关资源
    最近更新 更多