views:

653

answers:

4

Hi!

I want to develop an Java application that can detect the user logged on a Window Domain. These credentials are going to be used to logging on the Java application.

How can I do this?

Thanks!

+12  A: 
System.getProperty("user.name")
Spencer Ruport
Thank you very much for your help.
VansFannel
Can I use this on a Java Web app?
VansFannel
This is very easy to spoof. -Duser.name=Administrator
Peter Lawrey
I don't understand you.
VansFannel
You can change the user.name property on the command line, like any other property. You can use this in Java web app, but it gives you the user of the web process, not the user sending a request.
Peter Lawrey
+8  A: 

If you need to domain name, you can use this :

  com.sun.security.auth.module.NTSystem NTSystem = new
          com.sun.security.auth.module.NTSystem();
  System.out.println(NTSystem.getName());
  System.out.println(NTSystem.getDomain());

Bye.

RealHowTo
Can I use this on a Java Web app?
VansFannel
No. This is for a Java application (Windows OS).
RealHowTo
This one is better that the System.getProperty("user.name") example, becouse its very easy to trick the JVM to think there is a different user logged in.
Azder
A: 

Note: System.getProperty("user.name") will only work if the user launches the application. If the program is run by System or an application like LANDesk, then the user will come out as "SYSTEM" (under domain "NT AUTHORITY").

In which case, the second solution using NTSystem will return the correct results.

cwm
A: 

I noticed that the topicstarter asks afterwards in comments if s/he can use it in a Java webapp which was answered with "no" everytime. This is true if you run the particular code at the server side, but not if you run it at the client side in flavor of an applet or jnlp which is embedded in the requested jsp/html page. It however has to send the needed information to the server side afterwards.

BalusC