Commits

Micha Kops committed a399f28

LDAP / JNDI Tutorial Sources added

Comments (0)

Files changed (4)

ldap-dev-tutorial/.classpath

+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" path="src"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
+	<classpathentry kind="output" path="bin"/>
+</classpath>

ldap-dev-tutorial/.project

+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>ldap-dev-tutorial</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+	</natures>
+</projectDescription>

ldap-dev-tutorial/doc/users-import.ldif

+dn: dc=example,dc=com
+objectClass: domain
+objectClass: top
+dc: example
+
+dn: ou=Users,dc=example,dc=com
+objectClass: organizationalUnit
+objectClass: top
+ou: Users
+
+dn: ou=Groups,dc=example,dc=com
+objectClass: organizationalUnit
+objectClass: top
+ou: Groups
+
+dn: cn=Micha Kops,ou=Users,dc=example,dc=com
+objectClass: inetOrgPerson
+objectClass: organizationalPerson
+objectClass: person
+objectClass: top
+cn: Micha Kops
+sn: Kops
+uid: mkops
+userPassword:: abcdefg
+
+dn: cn=Santa Claus,ou=Users,dc=example,dc=com
+objectClass: inetOrgPerson
+objectClass: organizationalPerson
+objectClass: person
+objectClass: top
+cn: Santa Claus
+sn: Claus
+uid: sclaus
+userPassword:: abcdefg
+
+dn: cn=John Steinbeck,ou=Users,dc=example,dc=com
+objectClass: inetOrgPerson
+objectClass: organizationalPerson
+objectClass: person
+objectClass: top
+cn: John Steinbeck
+sn: Steinbeck
+uid: jsteinbeck
+userPassword:: abcdefg

ldap-dev-tutorial/src/com/hascode/tutorial/ldap/LdapQuery.java

+package com.hascode.tutorial.ldap;
+
+import java.util.Hashtable;
+
+import javax.naming.Context;
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+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 LdapQuery {
+	public static void main(String[] args) throws NamingException {
+		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://127.0.0.1:10389/");
+		env.put(Context.SECURITY_AUTHENTICATION, "simple");
+		env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
+		env.put(Context.SECURITY_CREDENTIALS, "secret");
+		DirContext ctx = new InitialDirContext(env);
+
+		SearchControls searchControls = new SearchControls();
+		searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
+		NamingEnumeration<SearchResult> enumeration = ctx.search("ou=Users,dc=example,dc=com", "(objectClass=person)", searchControls);
+		while (enumeration.hasMore()) {
+			SearchResult result = enumeration.next();
+			Attributes attrs = result.getAttributes();
+			System.out.println(String.format("User found.. %s, %s", attrs.get("cn"), attrs.get("uid")));
+		}
+	}
+}