Java > LDAP > Login to LDAP Directory as Anonymous User
Login to LDAP Directory as Anonymous User
LDAP (Lightweight Directory Access Protocol) is based on X.500 standard.
Its a hierarchical data structure with Entries organized in a tree like structure
called Directory Information Tree (DIT).
The following example shows how to login to LDAP Directory as Anonymous user.
This may not work with all LDAP servers as some LDAP servers doesn't allow Anonymous logins.
//Setup the environment to login as anonymous user
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://localhost:389/dc=test,dc=com");
DirContext dirContext = null;
try {
dirContext = new InitialDirContext(environment);
System.out.println("Connection established as Anonymous user");
} catch (NamingException e) {
e.printStackTrace();
} finally { if (dirContext != null) { try {
dirContext.close();
} catch (Exception e) {
}
}
}