【问题标题】:Querying Active directory查询活动目录
【发布时间】:2016-11-09 06:35:01
【问题描述】:

我想在用户不输入凭据的情况下针对 Active Directory 查询用户凭据。即用户登录到他的公司系统(内部网络)我需要使用这些凭据来验证 AD 并在用户存在时检索他的电子邮件地址。(不需要单点登录)

【问题讨论】:

    标签: java active-directory


    【解决方案1】:

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

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

    使用 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"
    

    然后选择你需要的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-07
      • 2012-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-10
      相关资源
      最近更新 更多