gpt4 book ai didi

Java LDAP 始终返回单个值而不是列表

转载 作者:行者123 更新时间:2023-12-01 10:53:27 25 4
gpt4 key购买 nike

我想查询我的 ldap,为我提供 sn 包含特定值 (maier) 的所有用户。然而我总是得到一个结果。

public LdapContext getLdapContext(){
LdapContext ctx = null;
try{
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://ldap.url:389");
ctx = new InitialLdapContext(env, null);
System.out.println("Connection Successful.");
}catch(NamingException nex){
System.out.println("LDAP Connection: FAILED");
nex.printStackTrace();
}
return ctx;
}

private User getUserBasicAttributes(String username, LdapContext ctx) {
User user=null;
try {

SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
String[] attrIDs = { "distinguishedName",
"sn",
"givenname",
"mail",
"telephonenumber"};
constraints.setReturningAttributes(attrIDs);
constraints.setCountLimit(200);
NamingEnumeration answer = ctx.search("DC=myDc,DC=com", "sn=*maier*", constraints);
if (answer.hasMore()) {
Attributes attrs = ((SearchResult) answer.next()).getAttributes();
System.out.println("distinguishedName "+ attrs.get("distinguishedName"));
System.out.println("givenname "+ attrs.get("givenname"));
System.out.println("sn "+ attrs.get("sn"));
System.out.println("mail "+ attrs.get("mail"));
System.out.println("telephonenumber "+ attrs.get("telephonenumber"));
}else{
throw new Exception("Invalid User");
}

} catch (Exception ex) {
ex.printStackTrace();
}
return user;
}

我做错了什么吗?

最佳答案

您没有循环,所以您当然只会得到一个结果。将 if (answer.hasMore()) 更改为 while (answer.hasMore())

关于Java LDAP 始终返回单个值而不是列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33729892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com