![](/img/trans.png)
[英]Check for user logged in with Active Directory in Java application
[英]Determine if active directory user started application
我想知道是否以及哪個 Active Directory 用戶啟動了我的 windows java 應用程序。
由於安全原因,我不能使用System.getProperty("user.name")
/ System.getenv("USERDOMAIN")
。
Advapi32Util.getUserName()
和NTSystem.getName()
似乎只返回本地用戶,而不是啟動應用程序的活動目錄用戶。
提前致謝
您可以執行 cmd 命令並獲取 output(如果您的安全允許)。
public static String executeCmdCommand(String command)
{
String result = null;
try (var inputStream = Runtime.getRuntime().exec(command).getInputStream();
var s = new Scanner(inputStream))
{
result = s.hasNext() ? s.next() : null;
}
catch (IOException e)
{
e.printStackTrace();
}
return Objects.requireNonNullElse(result, "").trim();
}
然后,executeCmdCommand("whoami") 將返回 "domain/username"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.