Getting the current NT/2000/XP user using JAAS

Note: if you are using a JDK less than 1.4, make sure you have downloaded the JAAS class libraries and the sample authentication module for Windows at http://java.sun.com/products/jaas/index-10.html. Put jaas.jar and jaasmod.jar in your classpath.

Main.java:

import com.sun.security.auth.module.NTSystem;
 
public class Main {
   public static void main(String []args) {
      NTSystem ntSystem = new NTSystem();
      System.out.println(ntSystem.getName());
   }
}

outputs on my machine:

C:myjaastest>java -Djava.library.path=c:javatoolsjaasmod1_0lib Main
esus

To get other NT properties:

import com.sun.security.auth.module.NTSystem;
import java.util.Arrays;

public class Main {
   public static void main(String []args) {
      NTSystem ntSystem = new NTSystem();
      System.out.println(ntSystem.getName());
      System.out.println(ntSystem.getDomain());
      System.out.println(ntSystem.getDomainSID());
      System.out.println(Arrays.asList((Object[])ntSystem.getGroupIDs()));
      System.out.println(ntSystem.getImpersonationToken());
      System.out.println(ntSystem.getPrimaryGroupID());
      System.out.println(ntSystem.getUserSID());
   }
}

outputs:

C:myjaastest>java -Djava.library.path=c:javatoolsjaasmod1_0lib Main
esus
WORKGROUP

[S-1-5-21-3978503659-2915903831-1203752577-513, S-1-1-0, S
-1-5-32-544, S-1-5-32-545, S-1-5-5-0-51662, S-1-2-0, S-1-5
-4, S-1-5-11]
1252
S-1-5-21-3978503659-2915903831-1203752577-513
S-1-5-21-3978503659-2915903831-1203752577-1007