Java/JNDI LDAP/Directory
Содержание
- 1 Adding a Binding with Attributes to the Directory
- 2 Authenticating to the Directory
- 3 Cancelling a Directory Search
- 4 Creating a Directory Entry
- 5 Creating an Initial Context to a Directory
- 6 Getting an Attribute"s Schema from the Directory
- 7 Getting an Object"s Schema from the Directory
- 8 Modifying an Object"s Attributes in the Directory
- 9 Performing a Basic Directory Search
- 10 Reading an Object"s Attributes from the Directory
- 11 Registering for Namespace Changes in the Directory
- 12 Registering for Object Changes in the Directory
- 13 Searching a Subtree in the Directory
- 14 Searching the Directory by Using a Search Filter
Adding a Binding with Attributes to the Directory
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
public class Main {
public static void main(String[] argv) throws Exception {
Attributes attrs = new BasicAttributes(true);
Attribute objclass = new BasicAttribute("objectclass");
objclass.add("top");
objclass.add("extensible");
attrs.put(objclass);
Object obj = "yourObject";
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "yourURL");
DirContext ctx = new InitialDirContext(env);
ctx.bind("cn=Sample", obj, attrs);
}
}
Authenticating to the Directory
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
public class Main {
public static void main(String[] argv) throws Exception {
String url = "ldap://localhost/o=JNDITutorial";
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, url);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "userDN");
env.put(Context.SECURITY_CREDENTIALS, "secret");
DirContext ctx = new InitialDirContext(env);
}
}
Cancelling a Directory Search
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchResult;
public class Main {
public static void main(String[] argv) throws Exception {
Attributes matchAttrs = new BasicAttributes(true);
matchAttrs.put(new BasicAttribute("sn", "YourName"));
matchAttrs.put(new BasicAttribute("mail"));
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "yourURL");
DirContext ctx = new InitialDirContext(env);
NamingEnumeration e = ctx.search("ou=People", matchAttrs);
if (e.hasMore()) {
SearchResult entry = (SearchResult) e.next();
// Abandon rest of results
e.close();
}
}
}
Creating a Directory Entry
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
public class Main {
public static void main(String[] argv) throws Exception {
Attributes attrs = new BasicAttributes(true);
Attribute objclass = new BasicAttribute("objectclass");
objclass.add("top");
objclass.add("extensible");
attrs.put(objclass);
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "yourURL");
DirContext ctx = new InitialDirContext(env);
Context entry = ctx.createSubcontext("cn=Sample", attrs);
}
}
Creating an Initial Context to a Directory
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
public class Main {
public static void main(String[] argv) throws Exception {
String url = "ldap://localhost/o=JNDITutorial";
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, url);
DirContext ctx = new InitialDirContext(env);
}
}
Getting an Attribute"s Schema from the Directory
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
public class Main {
public static void main(String[] argv) throws Exception {
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "yourURL");
DirContext ctx = new InitialDirContext(env);
// Get an attribute of that type
Attributes attrs = ctx.getAttributes("cn=YourName, ou=People", new String[] { "cn" });
Attribute cnAttr = attrs.get("cn");
// Get its attribute definition
DirContext cnSchema = cnAttr.getAttributeDefinition();
// Get cnSchema"s attributes
Attributes cnAttrs = cnSchema.getAttributes("");
}
}
Getting an Object"s Schema from the Directory
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
public class Main {
public static void main(String[] argv) throws Exception {
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "yourURL");
DirContext ctx = new InitialDirContext(env);
DirContext tedClasses = ctx.getSchemaClassDefinition("cn=YourName, ou=People");
NamingEnumeration e = tedClasses.search("", null);
while (e.hasMore()) {
DirContext entry = (DirContext) e.next();
System.out.println(entry);
}
}
}
Modifying an Object"s Attributes in the Directory
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.ModificationItem;
public class Main {
public static void main(String[] argv) throws Exception {
ModificationItem[] mods = new ModificationItem[3];
mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("mail","g@w.ru"));
mods[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("number","5555"));
mods[2] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, new BasicAttribute("jpeg"));
String url = "ldap://localhost/o=JNDITutorial";
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, url);
DirContext ctx = new InitialDirContext(env);
ctx.modifyAttributes("cn=Name, ou=People", mods);
}
}
Performing a Basic Directory Search
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchResult;
public class Main {
public static void main(String[] argv) throws Exception {
String[] attrIDs = { "sn", "number", "value", "mail" };
Attributes matchAttrs = new BasicAttributes(true);
matchAttrs.put(new BasicAttribute("sn", "YourName"));
matchAttrs.put(new BasicAttribute("mail"));
Object obj = "yourObject";
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "yourURL");
DirContext ctx = new InitialDirContext(env);
NamingEnumeration e = ctx.search("ou=People", matchAttrs, attrIDs);
while (e.hasMore()) {
SearchResult entry = (SearchResult) e.next();
System.out.println(entry.getName());
}
}
}
Reading an Object"s Attributes from the Directory
import java.util.Hashtable;
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;
public class Main {
public static void main(String[] argv) throws Exception {
String url = "ldap://localhost/o=JNDITutorial";
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, url);
DirContext ctx = new InitialDirContext(env);
String[] attrIDs = { "sn", "number", "value", "mail" };
Attributes answer = ctx.getAttributes("cn=yourName, ou=People", attrIDs);
NamingEnumeration e = answer.getAll();
while (e.hasMore()) {
Attribute attr = (Attribute) e.next();
System.out.println(attr.getID());
}
}
}
Registering for Namespace Changes in the Directory
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.event.EventContext;
import javax.naming.event.NamespaceChangeListener;
import javax.naming.event.NamingEvent;
import javax.naming.event.NamingExceptionEvent;
import javax.naming.event.NamingListener;
public class Main {
public static void main(String[] argv) throws Exception {
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "YourURL");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "userDN");
env.put(Context.SECURITY_CREDENTIALS, "secret");
EventContext ctx = (EventContext) (new InitialContext(env).lookup("ou=People"));
NamingListener listener = new SampleNCListener();
ctx.addNamingListener("cn=John", EventContext.ONELEVEL_SCOPE, listener);
}
}
class SampleNCListener implements NamespaceChangeListener {
public SampleNCListener() {
}
public void objectAdded(NamingEvent evt) {
System.out.println(evt.getNewBinding().getName());
}
public void objectRemoved(NamingEvent evt) {
System.out.println(evt.getOldBinding().getName());
}
public void objectRenamed(NamingEvent evt) {
System.out.println(evt.getOldBinding().getName());
}
public void namingExceptionThrown(NamingExceptionEvent evt) {
System.out.println(evt.getException().getExplanation());
}
}
Registering for Object Changes in the Directory
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.directory.SearchControls;
import javax.naming.event.EventDirContext;
import javax.naming.event.NamingEvent;
import javax.naming.event.NamingExceptionEvent;
import javax.naming.event.NamingListener;
import javax.naming.event.ObjectChangeListener;
public class Main {
public static void main(String[] argv) throws Exception {
String url = "ldap://localhost/o=JNDITutorial";
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, url);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "userDN");
env.put(Context.SECURITY_CREDENTIALS, "secret");
EventDirContext ctx = (EventDirContext) (new InitialContext(env).lookup("ou=People"));
NamingListener listener = new SampleObjListener();
SearchControls ctls = new SearchControls();
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String filter = "(mail=*)";
ctx.addNamingListener("cn=YourName", filter, ctls, listener);
}
}
class SampleObjListener implements ObjectChangeListener {
public SampleObjListener() {
}
public void objectChanged(NamingEvent evt) {
System.out.println(evt.getNewBinding().getName());
System.out.println(evt.getOldBinding().getName());
}
public void namingExceptionThrown(NamingExceptionEvent evt) {
System.out.println(evt.getException());
}
}
Searching a Subtree in the Directory
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
public class Main {
public static void main(String[] argv) throws Exception {
SearchControls ctls = new SearchControls();
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String filter = "(&(sn=YourName)(mail=*))";
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "yourURL");
DirContext ctx = new InitialDirContext(env);
NamingEnumeration e = ctx.search("", filter, ctls);
while (e.hasMore()) {
SearchResult entry = (SearchResult) e.next();
System.out.println(entry.getName());
}
}
}
Searching the Directory by Using a Search Filter
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
public class Main {
public static void main(String[] argv) throws Exception {
String[] attrIDs = { "sn", "number", "value", "mail" };
SearchControls ctls = new SearchControls();
ctls.setReturningAttributes(attrIDs);
String filter = "(&(sn=YourName)(mail=*))";
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "yourURL");
DirContext ctx = new InitialDirContext(env);
NamingEnumeration e = ctx.search("ou=People", filter, ctls);
while (e.hasMore()) {
SearchResult entry = (SearchResult) e.next();
System.out.println(entry.getName());
}
}
}