【问题标题】:Getting user info from LDAP by using JAVA使用 JAVA 从 LDAP 获取用户信息
【发布时间】:2017-10-23 20:59:42
【问题描述】:

对于 LDAP,我使用 LDAP 测试服务器,即 Forumsys,我们可以在 the link 中看到 Forumsys LDAP 的用户和组。

我想从他们的群组中获取用户的信息。我在 JAVA 上观看了一些关于 LDAP 的视频并尝试去做。但是,我无法得到它们。我的代码返回 null。

我该如何解决?我在获取用户和组信息方面的问题在哪里?

这是我的代码:

import javax.naming.*;
import javax.naming.directory.*;

import java.util.Hashtable;



public class LDAPV2 {
    public static void main(String[] args) throws NamingException{
        Hashtable <String,String> env = new Hashtable<>();
        env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL,"ldap://ldap.forumsys.com:389/dc=example,dc=com");
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, "uid=boyle,dc=example,dc=com");
        env.put(Context.SECURITY_CREDENTIALS, "password");

        DirContext context = new InitialDirContext(env);
        DirContext groupCx = (DirContext) context.lookup("ou=chemists");


        NamingEnumeration <Binding> groups = groupCx.listBindings("");
        while (groups.hasMore()){
            String bindingName = groups.next().getName();
            Attributes groupAttributes = groupCx.getAttributes(bindingName);
            Attribute groupName=groupAttributes.get("cn");
            System.out.println(groupName);
        }
    }
}

【问题讨论】:

    标签: java active-directory ldap


    【解决方案1】:

    ou=chemists 在您正在查找的目录中为空。所以它没有子绑定,所以while 循环永远不会执行。

    但它确实有一些 属性,您可以使用这些属性进行打印:

        Attributes groupAttributes = groupCx.getAttributes("");
        Attribute groupName = groupAttributes.get("uniqueMember");
        System.out.println(groupName);
    

    【讨论】:

      猜你喜欢
      • 2018-05-21
      • 1970-01-01
      • 2019-12-03
      • 2012-10-02
      • 2013-04-17
      • 2020-02-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多