How to retrieve all LDAP attributes definition on

2019-07-24 21:26发布

问题:

I'm working with ldap and want to retrieve all Ldap Attribute fields that defined on Ldap server. I just need list of attribute field only not the value. The result should be a list like this:

['mailNickname',
 'publicDelegatesBL',
 'logonCount',
 'cn',
 'countryCode',
 'dSCorePropagationData',
 'objectClass',
 # ...
'telephoneNumber',
'physicalDeliveryOfficeName',
'name',
'memberOf',
'codePage',
'userAccountControl',
'msExchMDBRulesQuota',
'lastLogon',
'protocolSettings',
'uSNChanged',
'sn',
'msExchVersion',
'mDBUseDefaults',
'givenName',
'msExchMailboxGuid',
'lastLogoff']

Is there way to do this?

回答1:

Instead of making the LDAP return all the attributes for you it would be advisable to set the array of attributes your are interested through SearchControl.



回答2:

You can enumerate all attributes of specific object (i.e. user in your case) and add them into a list as

// Get all the attributes of named object
Attributes attrs = ctx.getAttributes("cn=User1,ou=People");
List<String> l = new ArrayList<String>();
for (NamingEnumeration ae = attrs.getAll(); ae.hasMore();) {
    Attribute attr = (Attribute) ae.next();
    l.add(attr.getID()); 
}

Example: http://www.java2s.com/Code/Java/JNDI-LDAP/howtoretrieveallattributesofanamedobject.htm



回答3:

Depending on the LDAP Server Implementation, the LDAP schema, which contains all the Attributes (and ObjectClasses) defined can be obtained using several methods as described at: http://ldapwiki.com/wiki/LDAP%20Query%20For%20Schema

If you only wanted the Attributes. Try something like:

ldapsearch -h yourLDAPDNS  -b "cn=schema" -s base -D cn=admin,ou=...,dc=yourdomain,dc=com -w secretpassword "(objectclass=*)" attributeTypes 


回答4:

You can use the getSchema() and get the Schema of tree root of your LDAP

DirContext schema = yourLDAPctx.getSchema("");

then you can also choose which all attributes of a class you want from the Schema

DirContext personSchema = (DirContext)schema.lookup("ClassDefinition/<name of the objectClass>");

You can refer this link for it..It will tell you in more detail

http://www.cs.binghamton.edu/~steflik/cs328/jndi/tutorial/ldap/schema/object.html



标签: java ldap