Below is an example...
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Vector;
import javax.naming.AuthenticationException;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
public class LDAP {
private String ldapURL = "";
private boolean ssl = false;
private String curDN = "";
private DirContext ctx = null;
public LDAP(String url) {
this(url, false);
}
public LDAP(String url, Boolean ssl) {
this(url, ssl.booleanValue());
}
public LDAP(String url, boolean ssl) {
this.ldapURL = url;
this.ssl = ssl;
}
public void close() throws Exception {
this.ctx.close();
}
public Map search(String base, String filter) throws Exception {
Map rtn = new HashMap();
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration results = this.ctx.search(base, filter,
constraints);
while (results.hasMore()) {
Map tmp = new HashMap();
SearchResult sr = (SearchResult) results.next();
Attributes attrs = sr.getAttributes();
tmp.put("dn", sr.getName() + base);
NamingEnumeration ne = attrs.getAll();
while (ne.hasMoreElements()) {
Attribute attr = (Attribute) ne.next();
Vector values = new Vector();
for (NamingEnumeration ve = attr.getAll();
ve.hasMore();)
values.add(ve.next());
if (values.size() > 1)
tmp.put(attr.getID(), values);
else
tmp.put(attr.getID(),
values.firstElement());
}
rtn.put(sr.getName(), tmp);
}
return rtn;
}
public void bind(String dn) throws Exception {
this.curDN = dn;
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, this.ldapURL);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
if (this.ssl)
env.put(Context.SECURITY_PROTOCOL, "ssl");
this.ctx = new InitialDirContext(env);
}
public boolean auth(String username, String password, String dn)
throws Exception {
if (username == null || password == null)
return false;
this.curDN = dn;
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, this.ldapURL);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, username);
env.put(Context.SECURITY_CREDENTIALS, password);
if (this.ssl)
env.put(Context.SECURITY_PROTOCOL, "ssl");
try {
this.ctx = new InitialDirContext(env);
} catch (AuthenticationException e) {
return false;
}
return true;
}
public Map getEntry() throws Exception {
return this.getEntry(this.curDN);
}
public Map getEntry(String dn) throws Exception {
Map results = new HashMap();
Attributes answer = this.ctx.getAttributes(dn);
for (NamingEnumeration ae = answer.getAll(); ae.hasMore();)
{
Attribute attr = (Attribute) ae.next();
Vector values = new Vector();
for (NamingEnumeration ve = attr.getAll();
ve.hasMore();)
values.add(ve.next());
if (values.size() > 1)
results.put(attr.getID(), values);
else
results.put(attr.getID(),
values.firstElement());
}
return results;
}
}
-Matt
> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf
> Of Justin Balog
> Sent: Monday, October 04, 2004 4:45 PM
> To: '[EMAIL PROTECTED]'
> Subject: [CFCDev] LDAP and Java
>
>
> Howdy,
>
> I currently use the NTAuth java class wrapped up in a cfc to authenticate
> against an NT domain (compliments of Rob Rusher, thanks Rob). We now have
> AD going up, and was wondering if anyone has used native java to
> authenticate against and AD. I know CFLDAP exists, but I was just curious
> where I might learn how to do it with JNDI?
>
> Thanks much,
>
> Justin
>
> ----------------------------------------------------------
> You are subscribed to cfcdev. To unsubscribe, send an email
> to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev'
> in the message of the email.
>
> CFCDev is run by CFCZone (www.cfczone.org) and supported
> by Mindtool, Corporation (www.mindtool.com).
>
> An archive of the CFCDev list is available at www.mail-
> archive.com/[EMAIL PROTECTED]
----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email
to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev'
in the message of the email.
CFCDev is run by CFCZone (www.cfczone.org) and supported
by Mindtool, Corporation (www.mindtool.com).
An archive of the CFCDev list is available at www.mail-archive.com/[EMAIL PROTECTED]