Java Reference
In-Depth Information
protected String factory = “com.sun.jndi.ldap.LdapCtxFactory”;
protected String serverName = “yourServerName”;
protected int port = 389;
protected String securityAuthentication = “simple”;
protected String dirManager = “cn=Directory Manager”;
protected String dirManagerPw = “directory”;
11
All of these attributes have matching accessor methods so their values can be changed however
you initialize them with some default values. The factory specifies the LDAP Context factory
class that you would like to use to bind your Context s. The serverName is the host machine of
the LDAP server. The standard port used by an LDAP server is port 389. The other three
attributes deal with authenticating with the LDAP server upon binding. You have the username
and password to bind with, and the authentication method. The standard setup of a LDAP
server allows anonymous connections. This means that if there is no password specified the
server tries to bind the client anonymously. Most servers will only allow searching anony-
mously. To change the data in some way requires a valid username and password. You are
hard-coding a username and password into the class as an example, however this would not
provide any security for an actual application.
The following code contains a method to collect all of the information needed for binding:
protected Hashtable getEnvironment() {
Hashtable env = new Hashtable();
// generate LDAP host URL
StringBuffer url = new StringBuffer(“ldap://”);
url.append(serverName).append(“:”).append(port);
// set Context Factory class
env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, factory);
// set host URL
env.put(javax.naming.Context.PROVIDER_URL, url.toString());
// Only set authentication information if there is a username
// present
if ((dirManager != null) && (dirManager.trim().length() > 0)) {
env.put(javax.naming.Context.SECURITY_AUTHENTICATION,
securityAuthentication);
env.put(javax.naming.Context.SECURITY_PRINCIPAL, dirManager);
env.put(javax.naming.Context.SECURITY_CREDENTIALS, dirManagerPw);
}
return env;
}
 
Search WWH ::




Custom Search