从 Java 读取 Windows ACL
在 Java 程序中,我希望能够列出有权读取给定文件的 Windows 用户和组。 Java 没有内置的功能来读取 Windows ACL 信息(至少在 Java 7),所以我正在寻找其他解决方案。
是否有任何第三方库可以提供对 Windows 文件的 ACL 信息的直接访问?
如果做不到这一点,也许运行 cacls 并捕获然后处理输出将是一个合理的临时解决方案 - cacls 的输出格式是否在任何地方都有完整记录,并且它是否可能在 Windows 版本之间发生变化?
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.
Are there any third party libraries available which can provide direct access to the ACL information for a Windows file?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
mdma 的答案 涵盖了 Java 6 的最佳方法,但这里有一个 Java 7 的简单示例:
mdma's answer covers the best approaches for Java 6 but here's a quick example for Java 7:
如果您了解 Windows API,则可以使用 JNA (JNI,无需编写本机代码)调用Windows API获取ACL数据。
另外,这里还有一篇文章,它使用 VBScript 获取文件安全详细信息。您可以修改它,并让它以可解析的格式(例如 XML)返回详细信息。您可以通过运行“cscript.exe”、使用 Runtime.exec() 或 ProcessBuilder。您可以将 ACL 信息写入标准输出,并使用 java.lang.Process 上可用的流来读取进程输出。
虽然 exec'ing vbscript 可能看起来有点老套,但它会让您快速开始,因为大部分代码已经编写并且可以工作(我想 - 我还没有尝试过文章中的脚本。)还避免了需要通过 JNA 将相关 Win32 api 映射到 java。
If you know the windows APIs, you could use JNA (JNI without the hassle of writing native code) to call the Windows APIs to get the ACL data.
Altenatively here is an article that gets the file security details using VBScript. You could modify this, and have it return the details in a parsable format (e.g. XML). You can invoke a VBScript file by running "cscript.exe", using Runtime.exec() or ProcessBuilder. You can write the ACL info to standard output, and use the streams available on the
java.lang.Process
to read the process output.Although exec'ing vbscript might seem a bit hacky, it will get you off to a quick start, since most of the code is already written and working (I presume - I haven't tried the script in the article.) Using the script also avoids needing to map the relevant Win32 apis to java via JNA.