Page 2 of 2 First 12
  • Jump to page:
    #16
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2008
    Posts
    4
    Rep Power
    0
    Hello Winston,
    I had the same doubt which u had. So, please post me the solution.Its urgent Mr.Winston. My boss has given me the task of accessing the information in Active Directory using JAVA programming. So, can you please help me regarding this? My job is at stake here.
    It would be grateful, if you could reply to the email id as soon as possible.
  2. #17
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2008
    Posts
    4
    Rep Power
    0

    Hello Winston


    Originally Posted by winston_pinto
    Hello All,

    I want to know whether its possible to connect to Active Directory Services through Java and able to retreive the users and groups from Active Directory Services. If its possible can i get the code.

    thanks
    Winston
    Hello Winston,
    I had the same doubt which u had. So, please post me the solution.Its urgent Mr.Winston. My boss has given me the task of accessing & modifying the information in Active Directory using JAVA programming. So, can you please help me regarding this? My job is at stake here.
    It would be grateful, if you could reply to the email id as soon as possible.
  4. #18
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2008
    Location
    India
    Posts
    2
    Rep Power
    0

    Thumbs up It's obvious possible, Nothing is impossible in this world using Java Language.


    Originally Posted by winston_pinto
    Hello All,

    I want to know whether its possible to connect to Active Directory Services through Java and able to retreive the users and groups from Active Directory Services. If its possible can i get the code.

    thanks
    Winston
    ya my friend, Windston, Its possible , by JNDI lookup, you have only LDAP configured into you Microsoft ADS.

    for complete code and help please visit my blog or sen me buzz on my Email or you can visit my Blog

    this code is working and not a single error. ya some improvement needed based on your requirements. Please free to buzz me when you need help.

    Enjoy !!! the power of OpenSource with Java

    Comments on this post

    • tetsuyamasamune agrees
  6. #19
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2008
    Location
    India
    Posts
    2
    Rep Power
    0
    my friend even you can use ADS server information using java program.

    for complete code and detail, visit my blog, where you can find whole code.

    Comments on this post

    • tetsuyamasamune agrees : I like your blog, and it was helpful, but it didn't seem to work for me.
  8. #20
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2008
    Location
    US
    Posts
    1
    Rep Power
    0

    Complete frustration wtih LDAP and AD


    I am new to LDAP and AD. Please help me understand what I am doing wrong.

    I have used the above code and modified to my own server, domain, etc.

    It seems I am missing something. I am trying to non-anonymously connect to the AD. So, in detail:

    1 - I have a jsp and a servlet. I have a class with the login for ldap. Servlet calls ldap to connect to AD.
    2 - In my ldap class, I have the credentials supplied, and I am searching for a username and a password. As of right now, I have creds supplied for testing. I would like to eventually be able to go all the way to non-anon.
    3 - Problem: username and password are hard-coded in and matched in the page prompt. I get an error saying username is null. I have debugged and, sure enough, it's null. It should be auto-set considering it was hardcoded in.
    4 - another problem is the ldap bind. There is a problem binding since I can't access the active directory.

    Here is the code I have for the LDAP class. Please help me.





    Code:
    private transient String userLogin;
    	private transient String userPassword;
    	User user;
    
    	Hashtable<String, String> envGC = new Hashtable<String, String>();
    	Hashtable<String, String> envDC = new Hashtable<String, String>();
    
    	public LDAPCrap() throws NamingException {
    
    		// envGC = new Hashtable();
    		// envDC = new Hashtable();
    
    		String urlGC = "ldap://ssc-gc-01.mycompany.com:3268";
    		String urlDC = "ldap://ssc-dc-01.ssc.mycompany.com:389";
    
    		envGC.put(Context.INITIAL_CONTEXT_FACTORY,
    				"com.sun.jndi.ldap.LdapCtxFactory");
    		envDC.put(Context.INITIAL_CONTEXT_FACTORY,
    				"com.sun.jndi.ldap.LdapCtxFactory");
    
    		envGC.put(Context.PROVIDER_URL, urlGC);
    		envDC.put(Context.PROVIDER_URL, urlDC);
    	}
    
    	public void setUserLogin(String uid) {
    		this.userLogin = uid;
    	}
    
    	public void setUserPass(String password) {
    		this.userPassword = password;
    	}
    
    	public boolean isAuth() throws NamingException {
    
    		DirContext ctxGC = new InitialDirContext(envGC);
    		DirContext ctxDC = new InitialDirContext(envDC);
    
    		boolean auth = false;
    		try {
    
    			ctxDC.getNameInNamespace();
    			ctxGC.getNameInNamespace();
    
    			// String userDN = getDN(this.userLogin);
    
    			envGC.put(Context.SECURITY_AUTHENTICATION, "simple");
    			envGC.put(Context.SECURITY_PRINCIPAL,
    					"cn=username,cn=Users,dc=ssc,dc=mycompany,dc=com");
    			// envGC.put(Context.SECURITY_PRINCIPAL, "cn=" + this.userLogin
    			// + "cn=Users,dc=ssc,dc=mycompany,dc=com");
    			envGC.put(Context.SECURITY_CREDENTIALS, this.userPassword);
    
    			envDC.put(Context.SECURITY_AUTHENTICATION, "simple");
    			envDC.put(Context.SECURITY_PRINCIPAL,
    					"cn=username,cn=Users,dc=ssc,dc=mycompany,dc=com");
    			// envDC.put(Context.SECURITY_PRINCIPAL, "cn=" + this.userLogin
    			// + "cn=Users,dc=ssc,dc=mycompany,dc=com");
    			envDC.put(Context.SECURITY_CREDENTIALS, "password");
    
    			envDC.put(Context.REFERRAL, "follow");
    			envGC.put(Context.REFERRAL, "follow");
    
    			auth = true;
    
    		} catch (AuthenticationException e) {
    
    			auth = false;
    		} catch (NamingException e) {
    			auth = false;
    			System.out.println("Error in Authentication " + e);
    		}
    
    		String base = "";
    		int totalResults = 0;
    		String filter = "(objectclass=*)";
    
    		ctxDC.bind(userLogin, user);
    		ctxGC.bind(userLogin, user);
    
    		SearchControls controls = new SearchControls();
    
    		// may be a scope change
    		controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    
    		NamingEnumeration<?> answer = ctxGC.search(base, filter, controls);
    
    		// ... process attributes ...
    
    		while (answer.hasMoreElements()) {
    
    			SearchResult sr = (SearchResult) answer.next();
    			Attributes attrs = sr.getAttributes();
    
    			System.out.println("RootDSE: " + sr.getName());
    
    			if (attrs != null) {
    
    				try {
    
    					System.out.println(" Naming Context: "
    							+ attrs.get("defaultNamingContext").get());
    
    					System.out.println(" Schema Context: "
    							+ attrs.get("schemaNamingContext").get());
    
    					System.out.println(" DNS: "
    							+ attrs.get("dnsHostName").get());
    
    					System.out.println(" Server Name: "
    							+ attrs.get("serverName").get());
    
    					System.out
    							.println(" name(GC) Context: "
    
    							+ attrs.get("givenName").get() + ""
    									+ attrs.get("sn").get());
    
    					System.out.println(" mail(GC) Context: "
    							+ attrs.get("mail").get());
    
    				} catch (NullPointerException e) {
    					System.err
    							.println("Problem listing attributes from Global Catalog: "
    									+ e);
    				}
    			}
    
    			Attributes DCattrs = ctxDC.getAttributes(sr.getName());
    			try {
    				System.out.println(" Web(DC_):"
    						+ DCattrs.get("wWWHomePage").getID());
    				System.out.println(" Fax(DC):"
    						+ DCattrs.get("facsimileTelphoneNumber").getID());
    				// } catch (NamingException e) {
    				//
    				// System.out.println("Problem retrieving RootDSE: " + e);
    
    			} catch (NullPointerException e) {
    				System.err.println("Problem listing attributes from Domain "
    						+ " Controller:" + e);
    			}
    
    			try {
    				System.out.println("Total Results:" + totalResults);
    				ctxDC.unbind(userLogin);
    				ctxGC.unbind(userLogin);
    				ctxDC.close();
    				ctxGC.close();
    			} catch (Exception e) {
    				System.out.println("FAILED: " + e.getLocalizedMessage());
    
    			}
    
    		}
    		return (auth);
    	}
    Any direction is appreciated.
  10. #21
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    15
    Rep Power
    0

    To interact with ad using java


    I want to know how to interact with active directory server (retrieving user names, group policy names) using java.If there is any solution send me the code.

    Thanks
    B.Prakash
  12. #22
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    15
    Rep Power
    0

    To interact with ad using java


    I want to know how to interact with active directory server (retrieving user names, group policy names) using java.If there is any solution send me the code.

    Thanks
    B.Prakash
  14. #23
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    1
    Rep Power
    0

    try this


    import java.util.Hashtable;
    import javax.naming.ldap.*;
    import javax.naming.directory.*;
    import javax.naming.*;
    import javax.net.ssl.*;
    import java.io.*;

    public class newuser
    {
    public static void main (String[] args)
    {

    Hashtable env = new Hashtable();
    String adminName = "CN=Administrator,CN=Users,DC=antipodes,DC=com";
    String adminPassword = "XXXXXXX";
    String userName = "CN=Albert Einstein,OU=Research,DC=antipodes,DC=com";
    String groupName = "CN=All Research,OU=Research,DC=antipodes,DC=com";

    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, "ldap://localhost:389");

    try {

    // Create the initial directory context
    LdapContext ctx = new InitialLdapContext(env,null);

    // Create attributes to be associated with the new user
    Attributes attrs = new BasicAttributes(true);

    //These are the mandatory attributes for a user object
    //Note that Win2K3 will automagically create a random
    //samAccountName if it is not present. (Win2K does not)
    attrs.put("objectClass","user");
    attrs.put("samAccountName","AlbertE");
    attrs.put("cn","Albert Einstein");

    //These are some optional (but useful) attributes
    attrs.put("giveName","Albert");
    attrs.put("sn","Einstein");
    attrs.put("displayName","Albert Einstein");
    attrs.put("description","Research Scientist");
    attrs.put("userPrincipalName","AlbertE@antipodes.com");
    attrs.put("mail","relativity@antipodes.com");
    attrs.put("telephoneNumber","999 123 4567");

    //some useful constants from lmaccess.h
    int UF_ACCOUNTDISABLE = 0x0002;
    int UF_PASSWD_NOTREQD = 0x0020;
    int UF_PASSWD_CANT_CHANGE = 0x0040;
    int UF_NORMAL_ACCOUNT = 0x0200;
    int UF_DONT_EXPIRE_PASSWD = 0x10000;
    int UF_PASSWORD_EXPIRED = 0x800000;

    //Note that you need to create the user object before you can
    //set the password. Therefore as the user is created with no
    //password, user AccountControl must be set to the following
    //otherwise the Win2K3 password filter will return error 53
    //unwilling to perform.

    attrs.put("userAccountControl",Integer.toString(UF_NORMAL_ACCOUNT + UF_PASSWD_NOTREQD + UF_PASSWORD_EXPIRED+ UF_ACCOUNTDISABLE));


    // Create the context
    Context result = ctx.createSubcontext(userName, attrs);
    System.out.println("Created disabled account for: " + userName);

    //now that we've created the user object, we can set the
    //password and change the userAccountControl
    //and because password can only be set using SSL/TLS
    //lets use StartTLS

    StartTlsResponse tls = (StartTlsResponse)ctx.extendedOperation(new StartTlsRequest());
    tls.negotiate();

    //set password is a ldap modfy operation
    //and we'll update the userAccountControl
    //enabling the acount and force the user to update ther password
    //the first time they login
    ModificationItem[] mods = new ModificationItem[2];

    //Replace the "unicdodePwd" attribute with a new value
    //Password must be both Unicode and a quoted string
    String newQuotedPassword = "\"Password2000\"";
    byte[] newUnicodePassword = newQuotedPassword.getBytes("UTF-16LE");

    mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("unicodePwd", newUnicodePassword));
    mods[1] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("userAccountControl",Integer.toString(UF_NORMAL_ACCOUNT + UF_PASSWORD_EXPIRED)));

    // Perform the update
    ctx.modifyAttributes(userName, mods);
    System.out.println("Set password & updated userccountControl");


    //now add the user to a group.

    try {
    ModificationItem member[] = new ModificationItem[1];
    member[0]= new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("member", userName));

    ctx.modifyAttributes(groupName,member);
    System.out.println("Added user to group: " + groupName);

    }
    catch (NamingException e) {
    System.err.println("Problem adding user to group: " + e);
    }
    //Could have put tls.close() prior to the group modification
    //but it seems to screw up the connection or context ?
    tls.close();
    ctx.close();

    System.out.println("Successfully created User: " + userName);

    }
    catch (NamingException e) {
    System.err.println("Problem creating object: " + e);
    }

    catch (IOException e) {
    System.err.println("Problem creating object: " + e); }
    }
    }
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo