Results 1 to 2 of 2
  1. #1
    Brad is offline Member
    Join Date
    Feb 2012
    Posts
    23
    Rep Power
    0

    Default Using JNDI to create AD groups

    I found this chunk of code online somewhere and I am trying to get it to work in our companies environment. All I am trying to do is create AD groups from a Java application. The problem is that the code wants an LDAP URL. We have an alias that we use to access our LDAP environment that is in the format of "ldap.mycompany.com". I tried using the alias, but I get an error because its not in a URL format. Any ideas on how to fix this? I also noticed that Hashtable is detracted. What would be a good replacement for this? Hashmap? Thanks.

    Java Code:
    import java.util.Hashtable;
    import javax.naming.Context;
    import javax.naming.NamingException;
    import javax.naming.directory.Attributes;
    import javax.naming.directory.BasicAttributes;
    import javax.naming.ldap.InitialLdapContext;
    import javax.naming.ldap.LdapContext;
    import javax.swing.JOptionPane;
    
    public class CreateGroup {
    
        public int adgroup(String groupname, String description) {
            int error =0;
            Hashtable env = new Hashtable();
    
            String adminName = "CN=myname,OU=Users,OU=Accounts,DC=cs,DC=mycompany,DC=net";
            String adminPassword = "xxxxxx";
            String ldapURL = "ldap.mycompany.com:389";
            String group = groupname + ",OU=Tool_Access,OU=Application_Groups,DC=gs,DC=mycompany,DC=net";
            env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    
            //set security credentials, note using simple cleartext authentication
            env.put(Context.SECURITY_AUTHENTICATION, "simple");
            env.put(Context.SECURITY_PRINCIPAL, adminName);
            //env.put(Context.SECURITY_CREDENTIALS, adminPassword);
    
            //connect to my domain controller
            env.put(Context.PROVIDER_URL, ldapURL);
    
            try {
                // Create the initial directory context
                LdapContext ctx = new InitialLdapContext(env, null);
    
                // Create attributes to be associated with the new group 
                Attributes attrs = new BasicAttributes(true);
                attrs.put("objectClass", "group");
                attrs.put("samAccountName", groupname);
                attrs.put("cn", groupname);
                attrs.put("description", description);
    
                //group types
                int ADS_GROUP_TYPE_GLOBAL_GROUP = 0x0002;
                int ADS_GROUP_TYPE_DOMAIN_LOCAL_GROUP = 0x0004;
                int ADS_GROUP_TYPE_LOCAL_GROUP = 0x0004;
                int ADS_GROUP_TYPE_UNIVERSAL_GROUP = 0x0008;
                int ADS_GROUP_TYPE_SECURITY_ENABLED = 0x80000000;
    
                attrs.put("groupType", Integer.toString(ADS_GROUP_TYPE_DOMAIN_LOCAL_GROUP + ADS_GROUP_TYPE_SECURITY_ENABLED));
    
                // Create the context
                Context result = ctx.createSubcontext(group, attrs);
                System.out.println("Created group: " + group);
    
                ctx.close();
    
            } catch (NamingException ex) {
                JOptionPane.showMessageDialog(null,
                        "Could not create Active Directory group:" + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                error =1;
            }
          return error;
        }
    }

  2. #2
    siva8033 is offline Member
    Join Date
    Mar 2016
    Posts
    1
    Rep Power
    0

    Default Re: Using JNDI to create AD groups

    Quote Originally Posted by Brad View Post
    I found this chunk of code online somewhere and I am trying to get it to work in our companies environment. All I am trying to do is create AD groups from a Java application. The problem is that the code wants an LDAP URL. We have an alias that we use to access our LDAP environment that is in the format of "ldap.mycompany.com". I tried using the alias, but I get an error because its not in a URL format. Any ideas on how to fix this? I also noticed that Hashtable is detracted. What would be a good replacement for this? Hashmap? Thanks.

    Java Code:
    import java.util.Hashtable;
    import javax.naming.Context;
    import javax.naming.NamingException;
    import javax.naming.directory.Attributes;
    import javax.naming.directory.BasicAttributes;
    import javax.naming.ldap.InitialLdapContext;
    import javax.naming.ldap.LdapContext;
    import javax.swing.JOptionPane;
    
    public class CreateGroup {
    
        public int adgroup(String groupname, String description) {
            int error =0;
            Hashtable env = new Hashtable();
    
            String adminName = "CN=myname,OU=Users,OU=Accounts,DC=cs,DC=mycompany,DC=net";
            String adminPassword = "xxxxxx";
            String ldapURL = "ldap.mycompany.com:389";
            String group = groupname + ",OU=Tool_Access,OU=Application_Groups,DC=gs,DC=mycompany,DC=net";
            env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    
            //set security credentials, note using simple cleartext authentication
            env.put(Context.SECURITY_AUTHENTICATION, "simple");
            env.put(Context.SECURITY_PRINCIPAL, adminName);
            //env.put(Context.SECURITY_CREDENTIALS, adminPassword);
    
            //connect to my domain controller
            env.put(Context.PROVIDER_URL, ldapURL);
    
            try {
                // Create the initial directory context
                LdapContext ctx = new InitialLdapContext(env, null);
    
                // Create attributes to be associated with the new group 
                Attributes attrs = new BasicAttributes(true);
                attrs.put("objectClass", "group");
                attrs.put("samAccountName", groupname);
                attrs.put("cn", groupname);
                attrs.put("description", description);
    
                //group types
                int ADS_GROUP_TYPE_GLOBAL_GROUP = 0x0002;
                int ADS_GROUP_TYPE_DOMAIN_LOCAL_GROUP = 0x0004;
                int ADS_GROUP_TYPE_LOCAL_GROUP = 0x0004;
                int ADS_GROUP_TYPE_UNIVERSAL_GROUP = 0x0008;
                int ADS_GROUP_TYPE_SECURITY_ENABLED = 0x80000000;
    
                attrs.put("groupType", Integer.toString(ADS_GROUP_TYPE_DOMAIN_LOCAL_GROUP + ADS_GROUP_TYPE_SECURITY_ENABLED));
    
                // Create the context
                Context result = ctx.createSubcontext(group, attrs);
                System.out.println("Created group: " + group);
    
                ctx.close();
    
            } catch (NamingException ex) {
                JOptionPane.showMessageDialog(null,
                        "Could not create Active Directory group:" + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                error =1;
            }
          return error;
        }
    }

    Hello,

    Code look good.. Even i am looking for the same requirement of adding groups in AD. Could you please help me if above code is worked for you. If not please share me the correct link. Many thanks !

    Cheers
    Siva,

Similar Threads

  1. Create 2 groups in expandable listview
    By krishnaveni in forum Android
    Replies: 1
    Last Post: 08-14-2012, 09:44 AM
  2. Replies: 0
    Last Post: 09-12-2011, 08:02 AM
  3. how to sort jtable with row groups
    By mordjah in forum AWT / Swing
    Replies: 0
    Last Post: 10-25-2010, 05:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •