gpt4 book ai didi

java - 查询 Activity 目录

转载 作者:行者123 更新时间:2023-12-01 09:38:56 26 4
gpt4 key购买 nike

我想在 Active Directory 中查询用户凭据,而无需用户输入其凭据。即用户登录到他的公司系统(内部网络),我需要使用这些凭据来验证 AD 并检索他的电子邮件地址(如果用户存在)。(无需单点登录)

最佳答案

当然,现在回答已经太晚了,但是...像我这样的人可以搜索相同的答案...

我只是不确定为什么需要验证用户凭据?如果用户已经登录,则...凭据将得到验证。

可以使用 Windows powershell 获取他的电子邮件(以及来自 AD 的其他信息)。

public class TestWindowsAD {

public static void main(String... args) throws Exception {

System.out.println("Current user e-mail: " + getCurrentUserEmail());

}

private static String getCurrentUserEmail() {

String cmd = "powershell \"Add-Type -AssemblyName System.DirectoryServices.AccountManagement;[System.DirectoryServices.AccountManagement.UserPrincipal]::Current.EmailAddress;\"";
String userEmail = "";
if (!System.getProperty("os.name").toLowerCase().startsWith("win")) { throw new RuntimeException(
"We are not in Windows! OS is " + System.getProperty("os.name")); }
Runtime rt = Runtime.getRuntime();
Process pr;
try {
pr = rt.exec(cmd);
pr.waitFor();
BufferedReader bf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String nextLine = null;

while (true) {
nextLine = bf.readLine();
if (nextLine == null) break;
userEmail = nextLine;
}
bf.close();
} catch (Exception e) {
System.err.println("Failed to get user email: " + e.getMessage());
throw new RuntimeException(e);
}

return userEmail;
}

附注如果您需要更多信息,只需在命令提示符下运行:

powershell "Add-Type -AssemblyName System.DirectoryServices.AccountManagement;[System.DirectoryServices.AccountManagement.UserPrincipal]::Current"

然后选择您需要的内容。

关于java - 查询 Activity 目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38601372/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com