Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java LDAP Api 1

Status
Not open for further replies.

fatcodeguy

Programmer
Feb 25, 2002
281
CA
Hi,

Is there a way to communicate with Active Directory in Java? Does a Java LDAP API of some kind exist?

Thanks!!
 
Connecting to LDAP is easy via Java... here is a test class I wrote ages ago ... you will need to know your DC,OU attributes etc though, the host and port your LDAP server is running on etc :

Code:
import javax.naming.directory.*;
import javax.naming.*;
import java.util.*;

public class ActDir {

	public static void main(String args[]) throws Exception {

		// java ActDir exdc03 389

		Hashtable env = new Hashtable();
		DirContext ctx;

		String port = args[1];
		String host = args[0];
		String user = "corp\\username";
		String passwd = "password";

		env.put(Context.SECURITY_AUTHENTICATION,"simple");
		env.put(Context.SECURITY_PRINCIPAL, user);
		env.put(Context.SECURITY_CREDENTIALS, passwd);

		//env.put("java.naming.ldap.version", "3");
		//env.put(Context.REFERRAL, "follow");

		String url = new String("ldap://"+host+":"+port);
		env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
		env.put(Context.PROVIDER_URL,url);

		System.err.println("Connecting to " +url);
		ctx = new InitialDirContext(env);

		//ctx.bind(user, ctx, null);

		System.err.println("Connected to " +url);

		// Read supportedSASLMechanisms from root DSE
		Attributes attrs = ctx.getAttributes(url,new String[]{"supportedSASLMechanisms"});
		System.err.println(attrs);

		Attributes answer = ctx.getAttributes("OU=People,OU=Exeter,DC=corp,DC=acme,DC=net");

		// Specify the attributes to match
		// Ask for objects that has a surname ("sn") attribute with
		// the value "Geisel" and the "mail" attribute
		Attributes matchAttrs = new BasicAttributes(true); // ignore attribute name case
		matchAttrs.put(new BasicAttribute("sn", "Keeping"));
		matchAttrs.put(new BasicAttribute("mail"));

		// Search for objects that have those matching attributes
		NamingEnumeration ne = ctx.search("OU=People,OU=Exeter,DC=corp,DC=acme,DC=net", matchAttrs);

		while (ne.hasMoreElements()) {
			System.out.println(ne.next());
		}


	ctx.close();
	}
}

--------------------------------------------------
Free Database Connection Pooling Software
 
Thanks.

Here's the other thing. I'm working on a web app that will use Active Directory to authenticate users. So when a user logs into windows in the morning, access the app, the page(s) authenticate against the info that active directory deposits on your local pc.

Is it possible to get this information (like username,password or whatever attributes)?

Thanks again!!
 
You can authenticate a user against AD if they give you the user name - but you cannot (without running an applet or ActiveX control) get the logged in (to the poota) user name from a web page.

--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top