1

我想从 ldap 搜索特定的用户详细信息。所以我写下了以下检索用户详细信息的代码,但它返回用户对象列表。基本上我只想要人对象而不是人对象列表。用于使用 ldap 模板检索即时消息。如何修改此代码以使其返回人员对象?

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();
                }
            });


}
4

1 回答 1

4

The method you're calling, ldapTemplate.search() always returns a list of matching objects. This is because it is finding all the objects that match your criteria on the LDAP server. If you're not certain that a user matching your loginId exists, you are using the correct method already. Just check the length of the list and retrieve the first item from the returned list.

To get just a single item from LDAP, you need to know the distinguished name (DN) of a user in the LDAP server. A DN is a unique identifier of an object in LDAP, and you need to know this if you're going to look up a single object specifically. Depending on your LDAP configuration, this might be something like cn=<loginId>,ou=users,dc=yourorg,dc=com.

If you can construct the DN from the loginId you have, you can use the ldapTemplate.lookup(String, AttributesMapper) method to find just a single object.

于 2011-06-22T11:10:49.083 回答