我希望使用JNDI从LDAP服务器获取所有可识别名称(DN)的列表。我可以使用以下代码获取基本DN:
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://" + ldapServer + ":" + ldapPort);
env.put(Context.REFERRAL, "follow");
if(sslEnabled) {
env.put("java.naming.ldap.factory.socket", TrustAllSSLSocketFactory.class.getName());
}
// Create the LDAP context
LdapContext context = new InitialLdapContext(env, null);
String base = "";
String filter = "(objectclass=*)";
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.OBJECT_SCOPE);
// Search the directory for retrieving namingContexts attribute
// which contains all the base DNs values
NamingEnumeration<SearchResult> results = context.search(base, filter, controls);
List<String> namingContextsList = new ArrayList<String>();
// Process attributes
if(results.hasMore()) {
Attributes attrs = results.next().getAttributes();
if (attrs != null) {
Attribute namingContexts = attrs.get("namingContexts");
NamingEnumeration enumeration = namingContexts.getAll();
while(enumeration.hasMore()) {
namingContextsList.add((String) enumeration.next());
}
}
}
System.out.println(namingContextsList);
请您帮助以类似的方式获取所有可能的DN?
答案 0 :(得分:0)
只需将OBJECT_SCOPE更改为SUBTREE_SCOPE。
你知道,这都是记录在案的。
答案 1 :(得分:0)
以下代码适用于我:(请注意,您需要提供凭据才能执行此操作,属性名称为&#34; distinguishedName&#34;)
String ldapServer = "192.168.0.11";
String ldapPort = "389";
String principal = "CN=user";
String password = "password";
Hashtable<String,String> environment = new Hashtable<String,String>();
environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
environment.put(Context.PROVIDER_URL, "ldap://" + ldapServer + ":" + ldapPort);
environment.put(Context.SECURITY_AUTHENTICATION, "simple");
environment.put(Context.SECURITY_PRINCIPAL, principal);
environment.put(Context.SECURITY_CREDENTIALS, password);
environment.put(Context.REFERRAL, "follow");
environment.put("com.sun.jndi.ldap.connect.pool", "true");
// Create the LDAP context
LdapContext context = new InitialLdapContext(environment, null);
String baseDN = "DC=domain,DC=com" // Put your base DN here
String filter = "(objectclass=*)";
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
//controls.setSearchScope(SearchControls.ONELEVEL_SCOPE); // Use this for first level DNs only
NamingEnumeration<SearchResult> results = context.search(baseDN, filter, controls);
List<String> searchDNsList = new ArrayList<String>();
try {
// Process attributes
while(results.hasMore()) {
Attributes attrs = results.next().getAttributes();
if (attrs != null) {
Attribute distinguisedNames = attrs.get("distinguishedName");
if(distinguisedNames != null) {
NamingEnumeration enumeration = distinguisedNames.getAll();
while(enumeration.hasMore()) {
String searchDN = (String) enumeration.next();
searchDNsList.add(searchDN);
}
}
}
}
} catch(Exception ex) {
ex.printStackTrace();
}
System.out.println(searchDNsList);