检测Java应用程序是否作为Windows管理员运行

检测Java应用程序是否作为Windows管理员运行

问题描述:

我有一个Java应用程序。无论如何,我可以判断该进程是否在Windows 7上以管理员权限运行。

I have a Java application. Is there anyway I can tell if the process was run with admin privileges, on Windows 7.

我在网上找到了这段代码片段,我认为会为你做这个工作。

I found this code snippet online, that I think will do the job for you.

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;
}

它仅适用于Windows,并内置于核心Java包中。我刚刚测试了这段代码,它确实有效。它让我感到惊讶,但确实如此。

It ONLY works on windows, and comes built in to the core Java package. I just tested this code and it does work. It surprised me, but it does.

SID S-1-5-32-544是Windows操作系统中Administrator组的ID。

The SID S-1-5-32-544 is the id of the Administrator group in the Windows operating system.

这是链接,了解其工作原理的详细信息。

Here is the link for more details of how it works.