Java - 使用用户身份验证访问文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4734845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - Access file with user authentication
提问by Stefanos Kargas
I have a server where I work with a database and files using a java app. When I start my app I give a report regarding file access to the server using:
我有一台服务器,我在其中使用 Java 应用程序处理数据库和文件。当我启动我的应用程序时,我会使用以下方法提供有关对服务器的文件访问的报告:
public static boolean folderExists(String folderPath) {
File folderToCheck = new File(folderPath);
return folderToCheck.exists();
}
Every time I start my app (after a fresh restart of my computer) I get a false response, even though the server is on. The reason is because I must give an authentication as another user. What I do is access the server through Windows where I am being asked for username/password, and after that I get a true response regarding file access to the server.
每次我启动我的应用程序时(在我的计算机重新启动后),我都会收到错误的响应,即使服务器已打开。原因是我必须作为另一个用户进行身份验证。我所做的是通过 Windows 访问服务器,在那里我被要求输入用户名/密码,然后我得到关于对服务器的文件访问的真实响应。
Is there a way to give the authentication username/password through Java, and not through Windows?
有没有办法通过 Java而不是通过 Windows提供身份验证用户名/密码?
Thank you
谢谢
采纳答案by Uriah Carpenter
On Windows 'native' Java IO (e.g. java.io.File) always inherits the security context of the user running the JVM process. For example, you could run the Java app as a Windows service with the correct credentials.
在 Windows 上,“本机”Java IO(例如 java.io.File)总是继承运行 JVM 进程的用户的安全上下文。例如,您可以使用正确的凭据将 Java 应用程序作为 Windows 服务运行。
The JCIFS project implements CIFS(the Windows SMB file server protocol) and allows you to directly specify the username/password.
JCIFS 项目实现了CIFS(Windows SMB 文件服务器协议)并允许您直接指定用户名/密码。
See the APIfor examples.
有关示例,请参阅API。
回答by Christian Kuetbach
I am pretty sure, that there is no way to grant fileaccess by java, without a Windows-Call.
我很确定,没有 Windows 调用,就无法通过 java 授予文件访问权限。
You can call cacls file.log /e /t /p Everyone:f
but this will be language-dependent.
您可以打电话,cacls file.log /e /t /p Everyone:f
但这将取决于语言。
I had a similar problem: How to change the file ACL in windows, if I only know the SID?
我有一个类似的问题:如果我只知道 SID,如何在 Windows 中更改文件 ACL?
With Java7 there may be a way to do this.
使用 Java7 可能有一种方法可以做到这一点。