开发者

Extracting multi-valued attribute from LDAP groupOfUniqueNames

开发者 https://www.devze.com 2023-03-10 01:14 出处:网络
I\'m trying to read all members who belong to group defined in LDAP as groupOfUniqueNames. String url = \"ldap://blah.blah.address:389/dc=foo,dc=bar\";

I'm trying to read all members who belong to group defined in LDAP as groupOfUniqueNames.

String url = "ldap://blah.blah.address:389/dc=foo,dc=bar";
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, url);

DirContext ctx = new InitialDirContext(env);
String[] attrIDs = {"uniqueMember"};
SearchControls cons = new SearchControls();

Attributes answer = ctx.getAttributes("cn=testgroup", attrIDs);

NamingEnumeration e = answer.getAll();
while (e.hasMore()) {
    BasicAttribute attr = (BasicAttribute) e.next();
    System.out.println(attr.getID() + ": " + attr.get());
}

However, this code prints

uniqueMember:

as if it didn't read all values of unique member.

Using Apache Directory Studio 开发者_C百科I can see that this attribute has 3 values (one of which is empty). How can I access all of them?

If this matters I am using OpenLDAP.


I managed to solve this issue using JLDAP

LDAPConnection conn = new LDAPConnection();
conn.connect("blah.blah.address", 389);

String[] attrIDs = {"uniqueMember"};
LDAPSearchResults search = conn.search("dc=foo,dc=bar",
                                       LDAPConnection.SCOPE_ONE,
                                       "cn=testgroup", attrIDs, false);

while(search.hasMore()) {
    LDAPEntry entry = search.next();
    for (String string : entry.getAttribute("uniqueMember").getStringValueArray()) {
        System.out.println(string);
    }
} 
0

精彩评论

暂无评论...
验证码 换一张
取 消