Created
September 13, 2020 16:20
-
-
Save nipunthathsara/835b28193b02961a5d456e14b0e54881 to your computer and use it in GitHub Desktop.
Create OUs in LDAP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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