Java - Access file with user authentication

12,826

Solution 1

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.

The JCIFS project implements CIFS (the Windows SMB file server protocol) and allows you to directly specify the username/password.

See the API for examples.

Solution 2

I am pretty sure, that there is no way to grant fileaccess by java, without a Windows-Call.

You can call cacls file.log /e /t /p Everyone:f but this will be language-dependent.

I had a similar problem: How to change the file ACL in windows, if I only know the SID?

With Java7 there may be a way to do this.

Share:
12,826
Stefanos Kargas
Author by

Stefanos Kargas

Senior Software Engineer Started as a pure desktop developer. Deeply valuing object oriented programming and organized coding according to standards and fundamentals. Currently working full-stack eager to learn new technologies and keep updated to new standards. Java, C# (.Net), Visual Basic (.Net), PHP Spring Framework, MVC, Laravel PostgreSQL, MyPHP, SQL Server, Oracle HTML, CSS, JS, Angular

Updated on June 26, 2022

Comments

  • Stefanos Kargas
    Stefanos Kargas about 2 years

    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:

    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.

    Is there a way to give the authentication username/password through Java, and not through Windows?

    Thank you

  • Stefanos Kargas
    Stefanos Kargas over 13 years
    Can I do it by calling a shell command? It's ok with me. I am using Windows. How would the command be?
  • Christian Kuetbach
    Christian Kuetbach over 13 years
    I change the permission of a file bycalling cacls cacls file.log /e /t /p Everyone:f: support.microsoft.com/kb/318754/en But be aware, that the user with the login "Everyone", may have a different name on a localized windows. For a german translated windows, the call have to be cacls file.log /e /t /p Jeder:f