簡體   English   中英

如何使用spring在ldap中執行搜索操作

[英]How to perform search operation in ldap using spring

我想從ldap搜索特定的用戶詳細信息。 所以我寫下了檢索用戶詳細信息的代碼,但它返回了用戶對象列表。 基本上我只想要人物obejct而不是人物對象列表。 使用ldap模板進行retreiving。 如何修改此代碼以便返回person對象?

public void searchByFirstName(String loginId) {

        AndFilter filter = new AndFilter();
        filter.and(new EqualsFilter("objectclass", "Person"));
        filter.and(new EqualsFilter("cn", loginId));
        List list = ldapTemplate.search("", 
            filter.encode(),
            new AttributesMapper() {
                public Object mapFromAttributes(Attributes attrs) throws NamingException        {
                    return attrs.get("sn").get();
                }
            });


}

您正在調用的方法, ldapTemplate.search()始終返回匹配對象的列表。 這是因為它在LDAP服務器上找到符合條件的所有對象。 如果您不確定是否存在與您的loginId匹配的用戶,那么您已經使用了正確的方法。 只需檢查列表的長度並從返回的列表中檢索第一個項目。

要從LDAP獲取單個項目,您需要知道LDAP服務器中用戶的可分辨名稱 (DN)。 DN是LDAP中對象的唯一標識符,如果您要專門查找單個對象,則需要知道這一點。 根據您的LDAP配置,這可能類似於cn=<loginId>,ou=users,dc=yourorg,dc=com

如果可以從loginId構造DN,則可以使用ldapTemplate.lookup(String,AttributesMapper)方法查找單個對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM