为什么Spring LDAP的LdapTemplate不返回标题,部门和名称?公司属性? [英] Why does Spring LDAP's LdapTemplate not return title, department & company attributes?

查看:334
本文介绍了为什么Spring LDAP的LdapTemplate不返回标题,部门和名称?公司属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 spring-ldap-core-2.3.1.RELEASE.jar JDK 1.8 和; Tomcat 8.0 可以通过 LdapTemplate 访问AD信息。诸如 title department & ldapTemplate.search(..,。,..)方法未返回 company

I'm using spring-ldap-core-2.3.1.RELEASE.jar over JDK 1.8 & Tomcat 8.0 to access AD information through LdapTemplate. The attributes such as title,department & company are not being returned by the ldapTemplate.search(..,.,..) method.

我正在使用以下代码行进行搜索:-

I'm using the following lines of code to search :-

LdapQuery ldapQuery = LdapQueryBuilder.query()
                                       .where("objectclass").is("user")
                                       .and("objectcategory").is("person")
                                       .and("cn").like(strWildcardText+"*");
ldapTemplate.search(ldapQuery, new ADUserAttributesMapper());

以下是 ADUserAttributesMapper 类:-

public class ADUserAttributesMapper implements AttributesMapper<ADUserBean> {
    @Override
    public ADUserBean mapFromAttributes(Attributes attributes) throws NamingException {
        if(attributes==null) {
            return null;
        }

        adUserBean.setName((attributes.get("name")!=null) ? attributes.get("name").get().toString() : null);
        adUserBean.setCommonName((attributes.get("cn")!=null) ? attributes.get("cn").get().toString() : null);
        adUserBean.setDisplayName((attributes.get("displayname")!=null) ? attributes.get("displayname").get().toString() : null);
        adUserBean.setGivenName((attributes.get("givenname")!=null) ? attributes.get("givenname").get().toString() : null); // for FIRST NAME
        adUserBean.setMiddleName((attributes.get("initials")!=null) ? attributes.get("initials").get().toString() : null); // for MIDDLE NAME / INITIALS
        adUserBean.setLastName((attributes.get("sn")!=null) ? attributes.get("sn").get().toString() : null); // for LAST NAME
        adUserBean.setDepartment((attributes.get("department")!=null) ? attributes.get("department").get().toString() : null);
        adUserBean.setUserPrincipalName((attributes.get("userprincipalname")!=null) ? attributes.get("userprincipalname").get().toString() : null); // Logon Name
        adUserBean.setsAMAccountName((attributes.get("samaccountname")!=null) ? attributes.get("samaccountname").get().toString() : null); // Logon Name (pre-Windows 2000)
        adUserBean.setDistinguishedName((attributes.get("distinguishedname")!=null) ? attributes.get("distinguishedname").get().toString() : null);
        adUserBean.setMailID((attributes.get("mail")!=null) ? attributes.get("mail").get().toString() : null);
        adUserBean.setTitle((attributes.get("title")!=null) ? attributes.get("title").get().toString() : null); // Job Title
        adUserBean.setTelephoneNumber((attributes.get("telephonenumber")!=null) ? attributes.get("telephonenumber").get().toString() : null);
        adUserBean.setObjectCategory((attributes.get("objectcategory")!=null) ? attributes.get("objectcategory").get().toString() : null);

        return adUserBean;
    }
}

标题部门& company 属性属于AD用户属性的 Organization 标签,如下图所示:-

The title,department & company attributes belong to the Organization tab of the AD user properties as shown in the below image :-

此外,从常规选项卡中,缩写(缩写)属性不会被Spring-LDAP的 ldapTemplate 挑选/列出。 LdapQueryBuilder.query()对象可以访问 attributes(...)方法,该方法采用属性的字符串数组要提取的名称。但是,即使在此处明确提及它们之后,诸如 initials title department之类的属性的值& company 不返回。

Also, from the General tab the initials(initials) attribute is not being picked up/listed by Spring-LDAP's ldapTemplate. The LdapQueryBuilder.query() object has access to attributes(...) method that takes a string array of attribute names that are to be fetched. But even after mentioning them there explicitly, values for attributes such as initials, title, department & company are not returned.

Eclipse IDE中的 LDAP浏览器插件列出了标题部门& 组织标签下的公司属性没有问题。

The LDAP Browser plugin within the Eclipse IDE lists the title,department & company properties under the Organization tab without a problem.

即使 com4j API返回标题部门& company 属性。

Even the com4j API returns the title, department & company attributes.

是否有任何配置限制了该属性的列表或它的限制? Spring-LDAP API本身?这些属性不是 BasicAttributes 的一部分吗?如何通过Spring-LDAP获取这些属性?

Is there any configuration that is limiting the attribute(s) listing or is it a limitation with Spring-LDAP API itself? Are these attributes not part of BasicAttributes? How to fetch these attributes through Spring-LDAP?

更新(2017年8月1日):
普通的 Java JNDI 方法/代码不返回部门公司 title 属性(即使在属性字符串数组中明确提到了这些属性),但令人惊讶的是,它确实返回了 initials 属性值。

UPDATE (01-Aug-2017): The plain Java JNDI approach/code does NOT return department,company,title attributes (even with these attributes being explicitly mentioned in attributes string array), but surprisingly it does return the initials attribute value.

更新(2017年8月2日):
与@Pierre的建议(如下)类似,使用 SearchControls object:-

UPDATE (02-Aug-2017): Similar to @Pierre's suggestion (below) tried the following code using SearchControls object :-

String strFilter= "(&(objectclass=top)(cn=cgma*))";
String[] attrs = new String[] {"cn","givenName","sn","initials","title","department","company"};
long maxResults = 10; // for example 
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
searchControls.setReturningAttributes(attrs);
searchControls.setCountLimit(maxResults);
List<String> aLstOfADUsers = ldapTemplate.search("",strFilter,searchControls,new AttributesMapper<String>() 
                                                                     {
                                                                        public String mapFromAttributes(Attributes attrs) throws NamingException {
                                                                            try
                                                                            {
                                                                                System.out.println(attrs.toString());
                                                                                return attrs.get("cn").get().toString();
                                                                            }
                                                                            catch(Exception ex) {
                                                                                ex.printStackTrace();
                                                                                return null;
                                                                            }
                                                                        }
                                                                     });

return aLstOfADUsers;

即使此不会返回缩写标题公司& 部门属性值。

Even this does not return the initials, title, company & department attribute values.

推荐答案

人员属性可能是内部属性,您默认不会回来。您可以使用所使用的搜索方法(传递LdapQuery对象的属性)明确指定要返回的属性,但不返回。如果您看一下org.springframework.ldap.core.LdapTemplate类,似乎您无法将SearchControls对象传递给您使用的方法签名。因此,为了能够指定要获取的属性,请替换为:

The person attributes might be internal attributes which you wouldn't get back by default. You can specify explicitly which attributes you want returned BUT not in the search method you're using (the one where you pass in an LdapQuery object). If you take a look at the org.springframework.ldap.core.LdapTemplate class, it doesn't seem like you can pass in the SearchControls object to the method signature you're using. So, to be able to specify attributes to fetch, replace this:

LdapQuery ldapQuery = LdapQueryBuilder.query()
                                       .where("objectclass").is("user")
                                       .and("objectcategory").is("person")
                                       .and("cn").like(strWildcardText+"*");
ldapTemplate.search(ldapQuery, new ADUserAttributesMapper());

使用此:

        LikeFilter filter = new LikeFilter("cn", strWildcardText+"*");

        // list of attributes to retrieve
        String[] attrs = new String[] {"title","department","company"};
        long maxResults = 10; // for example 

        SearchControls searchControls = new SearchControls();
        searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        searchControls.setReturningAttributes(attrs);
        searchControls.setCountLimit(numResults);

        ldapTemplate.search(DistinguishedName.EMPTY_PATH, filter.encode(), searchControls, new ADUserAttributesMapper());

以上应该可以。您也可以尝试这样的操作(我还没有尝试过):

The above should work. You could also try something like this (I haven't tried that yet):

ldapTemplate.search( "dc=yourorg,dc=com", 
        "(&(cn=" +strWildcardText + "*)(&(objectClass=person)(objectcategory=person)))",
        SearchControls.SUBTREE_SCOPE,
        new String[]{ "title","department","company" },
        new ADUserAttributesMapper() );

最后,要取回所有属性,请要求检索上面代码中的所有属性(我上面的示例仅要求提供3个属性,这将返回所有属性):

Finally, to get ALL attributes back, ask to retrieve ALL attributes in the code above (my above example only asked for 3 attributes, this would return ALL of them):

        String[] attrs = new String[]{"*","+"};

这篇关于为什么Spring LDAP的LdapTemplate不返回标题,部门和名称?公司属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆