从Java读取Windows ACL [英] Reading Windows ACLs from Java
问题描述
在Java程序中,我希望能够列出有权读取给定文件的Windows用户和组。 Java没有内置的能力来读取Windows ACL信息(至少在 Java 7 ),所以我正在寻找其他解决方案。
From within a Java program, I want to be able to list out the Windows users and groups who have permission to read a given file. Java has no built-in ability to read the Windows ACL information out (at least until Java 7), so I'm looking for other solutions.
是否有可用的第三方库可以直接访问Windows文件的ACL信息?
Are there any third party libraries available which can provide direct access to the ACL information for a Windows file?
如果失败,可能会运行cacls并捕获然后处理输出将是一个合理的临时解决方案 - cacls的输出格式是否在任何地方都有详细记录,是否可能在Windows版本之间进行更改?
Failing that, maybe running cacls and capturing and then processing the output would be a reasonable temporary solution - Is the output format of cacls thoroughly documented anywhere, and is it likely to change between versions of Windows?
推荐答案
mdma 的答案涵盖了Java 6的最佳方法,但这里是Java 7的一个简单示例:
mdma's answer covers the best approaches for Java 6 but here's a quick example for Java 7:
Path file = Paths.get("c:\\test-file.dat");
AclFileAttributeView aclFileAttributes = Files.getFileAttributeView(
file, AclFileAttributeView.class);
for (AclEntry aclEntry : aclFileAttributes.getAcl()) {
System.out.println(aclEntry.principal() + ":");
System.out.println(aclEntry.permissions() + "\n");
}
这篇关于从Java读取Windows ACL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!