简体   繁体   English

使用用户名LDAP获取用户DN

[英]getting user DN with user name LDAP

I want to get user DN with the username provided. 我想使用提供的用户名获取用户DN。 What I think is that I want to retrieve all the user data and compare with the username. 我认为我想检索所有用户数据并与用户名进行比较。 And now, I have added objectclass in my search filter and I have no idea why is the data is not retrieving. 现在,我在搜索过滤器中添加了objectclass,但我不知道为什么无法检索数据。 Here are the codes that I currently have. 这是我目前拥有的代码。

Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, url);
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, "cn=admin,ou=sa,o=system");
    env.put(Context.SECURITY_CREDENTIALS, "P@ssw0rd");

    try{
    DirContext context = new InitialDirContext(env);
    SearchControls constraints = new SearchControls();
    constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
    NamingEnumeration result = context.search("", "(objectclass=Person)", constraints);
    while(result.hasMore())
    {
        SearchResult searchResult = (SearchResult) result.next();
        Attributes attrs = searchResult.getAttributes();
        request.setEmail(attrs.get("mail").toString());
        request.setPhoneNumber(attrs.get("personalMobile").toString());
        Attribute ldapattr = attrs.get("photo");
        if(ldapattr != null){
            byte[] photo = (byte[])ldapattr.get();
            request.setPhoto(photo);
        }
    }
    }catch(Exception e){
        System.out.println("can't initialized");
    }
    list.add(request);
    //Specific URL of LDAP with the host and :port 
    return list;
}

Provide a base DN to search. 提供要搜索的基本DN。 eg ou=users below and add username to filter for faster search Don't get all the user data as you are unnecessarily increasing network traffic and doing additional computational work on the client. 例如,下面的ou = users并添加用户名以进行筛选以进行更快的搜索由于您不必要地增加了网络流量并在客户端上进行了其他计算工作,因此请不要获取所有用户数据。 LDAP server excels at this kind of searching. LDAP服务器在这种搜索方面表现出色。 CN is indexed by default but givenName may not be indexed; CN在默认情况下已建立索引,但givenName可能未建立索引; so you might want to add an index for this attribute. 因此您可能想为此属性添加索引。

    NamingEnumeration result = context.search("ou=users", 
"(&(objectClass=person)(sAMAccountName=" + userId + "))", constraints);

If you have givenName 如果您给定的名称

NamingEnumeration result = context.search("ou=users", 
    "(&(objectClass=person)(givenName=" + givenName + "))", constraints);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM