JavaJDNILDAP组搜索只返回一个结果
我需要获取userDN的组名。我正在尝试此代码,但无法正常工作,因为ctx.search()将为每个组返回一个成员。我的意思是,即使我的一些组有多个用户作为成员(使用member,而不是memberOf),search()也只会返回一个 (我使用OpenLDAP作为LDAP服务器) 最后,我的代码不会返回提供的userDN的所有组,因为不会找到所有匹配项 谢谢你的帮助JavaJDNILDAP组搜索只返回一个结果,ldap,jndi,openldap,Ldap,Jndi,Openldap,我需要获取userDN的组名。我正在尝试此代码,但无法正常工作,因为ctx.search()将为每个组返回一个成员。我的意思是,即使我的一些组有多个用户作为成员(使用member,而不是memberOf),search()也只会返回一个 (我使用OpenLDAP作为LDAP服务器) 最后,我的代码不会返回提供的userDN的所有组,因为不会找到所有匹配项 谢谢你的帮助 public Set<String> getLDAPGroupNames(String userDN) th
public Set<String> getLDAPGroupNames(String userDN) throws NamingException {
Set<String> userRoleNames = new HashSet<>();
if (isLDAPUserRepositoryEnabled()) {
for (String roleName : getLDAPGroupNames()) {
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
constraints.setReturningAttributes(new String[] { "member" });
constraints.setCountLimit(100);
// First input parameter is search bas, it can be "CN=Users,DC=YourDomain,DC=com"
// Second Attribute can be uid=username
LdapContext ctx = getLdapContext();
try {
NamingEnumeration<SearchResult> answer = ctx.search(ldapGroupContextDN, "cn=" + roleName, constraints);
while (answer.hasMore()) {
NamingEnumeration<? extends Attribute> attributes = answer.next().getAttributes().getAll();// FIXME Only returns first of the member entry for the group.
while (attributes.hasMore()) {
Attribute attribute = attributes.next();
if (userDN.equalsIgnoreCase((String) attribute.get()))
userRoleNames.add(roleName);
}
}
} finally {
ctx.close();
}
}
} else
throw new IllegalStateException("Can't return LDAP group names. No LDAP Context enabled.");
return userRoleNames;
}
public Set getLDAPGroupNames(字符串userDN)引发NamingException{
Set userRoleNames=new HashSet();
如果(isLDAPUserRepositoryEnabled()){
对于(字符串roleName:getLDAPGroupNames()){
SearchControls约束=新的SearchControls();
约束.setSearchScope(SearchControls.SUBTREE_范围);
SetReturningAttribute(新字符串[]{“成员”});
限制。setCountLimit(100);
//第一个输入参数是search bas,它可以是“CN=Users,DC=YourDomain,DC=com”
//第二个属性可以是uid=username
LdapContext ctx=getLdapContext();
试一试{
NamingEnumeration answer=ctx.search(ldapGroupContextDN,“cn=”+roleName,约束);
while(answer.hasMore()){
NamingEnumeration过滤器应该是“(&(cn={0})(member={1}))”
,带有过滤器参数{roleName,userDN}
,您应该删除您自己的代码中检查userDN的部分。然后,只有将该用户作为成员的组才会返回,您所要做的就是保存它们。我知道这是一个老问题,但我有相同的问题并找到了解决方案。我将其包括在这里,以便其他人能够受益
JNDI确实处理多值属性
组的成员,而不是查找用户所属的组。也就是说,userRoleNames拥有组中的用户,而不是用户的角色。如果您想要特定用户拥有的角色(即用户所属的组),则可以使用EJP指出的解决方案)
解决办法是改变
while (attributes.hasMore()) {
Attribute attribute = attributes.next();
if (userDN.equalsIgnoreCase((String) attribute.get()))
userRoleNames.add(roleName);
}
到
while(attributes.hasMore()){
Attribute=attributes.next();
if(“member.equalsIgnoreCase(attribute.getID())){
for(NamingEnumeration e=(NamingEnumeration)attribute.getAll();e.hasMore();){
对象v=e.next();
add(v instanceof byte[]?新字符串((byte[])v:v.toString());
}
}
}
while (attributes.hasMore()) {
Attribute attribute = attributes.next();
if ("member".equalsIgnoreCase(attribute.getID())){
for (NamingEnumeration<Object> e = (NamingEnumeration<Object>)attribute.getAll(); e.hasMore(); ){
Object v = e.next();
userRoleNames.add( v instanceof byte[]? new String((byte[])v):v.toString());
}
}
}