Skip to content

Instantly share code, notes, and snippets.

@nipunthathsara
Created September 13, 2020 16:20
Show Gist options
  • Save nipunthathsara/835b28193b02961a5d456e14b0e54881 to your computer and use it in GitHub Desktop.
Save nipunthathsara/835b28193b02961a5d456e14b0e54881 to your computer and use it in GitHub Desktop.
Create OUs in LDAP
package org.example;
import javax.naming.Context;
import javax.naming.NamingException;
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;
import java.util.Hashtable;
/**
* Hello world!
*/
public class App {
public static void main(String[] args) {
System.out.println("Hello World!");
Hashtable<String, String > environment = new Hashtable<String, String >();
environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
environment.put(Context.SECURITY_AUTHENTICATION, "simple");
environment.put(Context.REFERRAL, "follow");
environment.put(Context.PROVIDER_URL, "ldap://localhost:10389");
environment.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
environment.put(Context.SECURITY_CREDENTIALS, "admin");
DirContext ctx;
try{
ctx = new InitialDirContext(environment);
Attributes attrs = new BasicAttributes(true); // case-ignore
Attribute objclass = new BasicAttribute("objectclass");
objclass.add("top");
objclass.add("organizationalUnit");
attrs.put(objclass);
ctx.createSubcontext("ou=test1,ou=Users,dc=wso2,dc=org", attrs);
} catch (NamingException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment