我试图从 distinguishedName 参数中获取登录用户的组名。我可以找到这个组名,但是搜索很慢,即使我在ldap搜索中找到了这个人,在搜索中得到了
PartialResultException: Unprocessed Continuation Reference(s);
以下是我的代码
我想停下来 ldapTemplate.search
过程中,我得到了一个成功的结果,你们有什么想法吗?
public List<String> getUserGroup(String username) throws Exception {
try {
LdapQuery query;
LdapTemplate ldapTemplate = ldapConf.ldapTemplate();
if (ldapConf.getSearchBase().isEmpty()) {
System.out.println("searchbase empty");
query = query().where(env.getProperty("retouch.ldap.searchKeyword")).isPresent();
} else
query = query().base(ldapConf.getSearchBase()).where(env.getProperty("retouch.ldap.searchKeyword")).isPresent();
List<String> result = ldapTemplate.search(query, new AttributesMapper<String>() {
public String mapFromAttributes(Attributes attrs) throws NamingException, javax.naming.NamingException {
for (Enumeration vals = attrs.get("distinguishedName").getAll(); vals.hasMoreElements();) {
String userName = (String) vals.nextElement();
if (userName.contains(username)) {
return (String) attrs.get("distinguishedName").get();
}
}
return null;
}
});
logger.debug("ldap groups" + result);
while (result.remove(null)) {
}
return result;
} catch (Exception e) {
e.printStackTrace();
logger.error(e, e);
throw e;
}
}
我完全误解了ldap搜索机制。我的ldap查询也是错误的
query = query().where(env.getProperty("ldap.searchKeyword")).isPresent();
而不是 isPresent()
我应该用 is(username)
所以我可以直接得到我想要的用户。另外,我还想在我的代码中获取用户DN。而不是用 new AttributesMapper<String>()
, new ContextMapper<String>()
提供DN