Check if current user is admin (Windows) Tag(s): Environment Security


import java.io.*;

public class WindowsUtils {

  private WindowsUtils() {  }

  public static boolean isAdmin() {
    String groups[] =
      (new com.sun.security.auth.module.NTSystem()).getGroupIDs();
    for (String group : groups) {
          if (group.equals("S-1-5-32-544"))
              return true;
    }
    return false;
  }

  public static void main(String[] args) throws IOException {
    System.out.println("Current user is admin ? " + WindowsUtils.isAdmin());
  }
}
"S-1-5-32-544" is a well-known security identifier in Windows. If the current user has it then he's an administrator.

A complete list of those identifers can be found this Microsoft document.

This HowTo is using a com.sun.* package. This is not a good thing since these packages may disappear in the future and they may not be available in a JDK from a different vendor or plateform. See also this HowTo.


blog comments powered by Disqus