ldapsearch -H -x -s base -b cn=subschema objectclasses转成java代码
import javax.naming.; import javax.naming.directory.;
public class LDAPSearchExample {
public static void main(String[] args) {
String ldapUrl = "<LDAP_URL>"; // Replace with your LDAP server URL
String baseDN = "cn=subschema"; // Replace with your base DN
String objectClasses = "objectclasses"; // Replace with the attribute you want to search
try {
// Set up the environment for creating the initial context
Hashtable<String, Object> env = new Hashtable<String, Object>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, ldapUrl);
env.put(Context.SECURITY_AUTHENTICATION, "none");
// Create the initial context
DirContext ctx = new InitialDirContext(env);
// Search for the specified attribute
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.BASE_SCOPE);
searchControls.setReturningAttributes(new String[] { objectClasses });
NamingEnumeration<SearchResult> results = ctx.search(baseDN, "(objectclass=*)", searchControls);
// Print out the results
while (results.hasMore()) {
SearchResult result = results.next();
Attributes attrs = result.getAttributes();
Attribute attr = attrs.get(objectClasses);
if (attr != null) {
System.out.println(attr.get());
}
}
// Close the context when we're done
ctx.close();
} catch (NamingException e) {
e.printStackTrace();
}
} }
原文地址: https://www.cveoy.top/t/topic/bZMA 著作权归作者所有。请勿转载和采集!