如何设置“修改"?使用Java在Windows文件上的ACL

编程入门 行业动态 更新时间:2024-10-09 22:17:19
本文介绍了如何设置“修改"?使用Java在Windows文件上的ACL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何使用Java 7的File API设置Windows ACL,使其模仿添加用户/组和以下选项:

How do you set the Windows ACL using Java 7's File API so that it mimics adding the user/group and the following options:

例如,在操作系统属性"对话框中,您可以选择以下基本写访问权限:

For example, in the OS Properties dialog, you can select the following for basic write access:

  • ☑修改
  • ☑读取并执行(自动选择)
  • ☑列出文件夹内容(自动选择)
  • ☑读取(自动选择)
  • ☑写入(自动选择)

但是,当我通过Java 7的File API使用类似的选项时,它会选择:

However, when I use similar options using Java 7's File API, it instead selects:

  • ☑特别
    • ☑高级(不要编辑,单击用户或组,查看)
      • 显示高级权限
      • 高级权限
        • ☑(多行复选框)

        这不仅难以管理(遍历对话框更容易错过某些内容),而且其行为与单击这些框不同.一些UAC提示的行为有所不同.更糟糕的是,由于遍历对话框,很难切换权限(例如,从Windows桌面),从而留出更多出错的机会.

        Not only is this harder to administer (traversing dialogs is easier to miss something), it doesn't behave the same was as simply clicking those boxes. Some of the UAC prompts behave differently. Worse, it's harder to toggle the permissions (e.g. from the Windows desktop) due to the dialog traversing, leaving more chance of making a mistake.

        如何使用Java设置这些复选框?

        How do I use Java to set these checkboxes?

        UserPrincipal authenticatedUsers = path.getFileSystem().getUserPrincipalLookupService() .lookupPrincipalByName("Authenticated Users"); AclFileAttributeView view = Files.getFileAttributeView(path, AclFileAttributeView.class); // Create ACL to give "Authenticated Users" "modify" access AclEntry entry = AclEntry.newBuilder() .setType(AclEntryType.ALLOW) .setPrincipal(authenticatedUsers) .setPermissions(DELETE, DELETE_CHILD, LIST_DIRECTORY, READ_DATA, READ_ATTRIBUTES, ADD_FILE, WRITE_DATA, WRITE_ATTRIBUTES) .build(); List<AclEntry> acl = view.getAcl(); acl.add(0, entry); // insert before any DENY entries view.setAcl(acl);

        推荐答案

        我能够模仿Windows属性文件的权限,方法是创建一个我想模仿的文件夹,通过属性"对话框设置值,然后将其回显...

        I was able to mimic the Windows Properties file permissions by making a folder that I wanted to mimic, setting the values through the Properties dialog, then echoing them back out...

        // Echo ACL Path path = Paths.get("C:\\myfolder"); UserPrincipal authenticatedUsers = path.getFileSystem().getUserPrincipalLookupService() .lookupPrincipalByName("Authenticated Users"); AclFileAttributeView view = Files.getFileAttributeView(path, AclFileAttributeView.class); for(AclEntry entry : view.getAcl()) { if(entry.principal().equals(authenticatedUsers)) { System.out.println("=== flags ==="); for (AclEntryFlag flags : entry.flags()) { System.out.println(flags.name()); } System.out.println("=== permissions ==="); for (AclEntryPermission permission : entry.permissions()) { System.out.println(permission.name()); } } }

        输出:

        === flags === DIRECTORY_INHERIT FILE_INHERIT === permissions === WRITE_NAMED_ATTRS WRITE_ATTRIBUTES DELETE WRITE_DATA READ_ACL APPEND_DATA READ_ATTRIBUTES READ_DATA EXECUTE SYNCHRONIZE READ_NAMED_ATTRS

        然后,我能够将这些值重新插入到我的原始示例中:

        Then, I was able to plug these values back into my original example:

        UserPrincipal authenticatedUsers = path.getFileSystem().getUserPrincipalLookupService() .lookupPrincipalByName("Authenticated Users"); AclFileAttributeView view = Files.getFileAttributeView(path, AclFileAttributeView.class); // Create ACL to give "Authenticated Users" "modify" access AclEntry entry = AclEntry.newBuilder() .setType(AclEntryType.ALLOW) .setPrincipal(authenticatedUsers) .setFlags(DIRECTORY_INHERIT, FILE_INHERIT) .setPermissions(WRITE_NAMED_ATTRS, WRITE_ATTRIBUTES, DELETE, WRITE_DATA, READ_ACL, APPEND_DATA, READ_ATTRIBUTES, READ_DATA, EXECUTE, SYNCHRONIZE, READ_NAMED_ATTRS) .build();

        ...,现在已完全根据需要检查修改".

        ... and now "Modify" is checked exactly as desired.

更多推荐

如何设置“修改"?使用Java在Windows文件上的ACL

本文发布于:2023-11-28 10:53:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642108.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何设置   文件   quot   Java   Windows

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!