如何使用spring在ldap中执行搜索操作
我想从 ldap 搜索特定用户详细信息。所以我写下了以下代码来检索用户详细信息,但它返回用户对象列表。基本上我只想要人对象而不是人对象列表。使用 LDAP 模板检索 IM。我如何修改此代码以使其返回 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();
}
});
}
I want to search specific user details from ldap. so i have wrote down following code retrieving user details but it returns list of user object. Basically i want only person obejct not list of person objects. for retreiving i m using ldap template. How can i modify this code so that it return person object?
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();
}
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您调用的方法 ldapTemplate.search() 总是返回匹配的列表对象。这是因为它正在 LDAP 服务器上查找与您的条件匹配的所有对象。如果您不确定是否存在与您的
loginId
匹配的用户,那么您已经使用了正确的方法。只需检查列表的长度并从返回的列表中检索第一项。要从 LDAP 获取单个项目,您需要知道 LDAP 服务器中用户的专有名称 (DN)。 DN 是 LDAP 中对象的唯一标识符,如果您要专门查找单个对象,则需要知道这一点。根据您的 LDAP 配置,这可能类似于
cn=,ou=users,dc=yourorg,dc=com
。如果您可以根据您拥有的
loginId
构建 DN,则可以使用 ldapTemplate.lookup(String, AttributesMapper) 方法仅查找单个对象。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.