Showing posts with label LDAP. Show all posts
Showing posts with label LDAP. Show all posts

Thursday, June 19, 2014

Java - Authenticate users in Active directory and using LDAP

First lets me try to explain you in brief what is Active directory and LDAP

Active Directory is a database based system that provides authentication, directory, policy, and other services in a Windows environment

LDAP (Lightweight Directory Access Protocol) is an application protocol for querying and modifying items in directory service providers like Active Directory, which supports a form of LDAP.

In short, Active Directory is a directory services database and LDAP is one of the protocols you can use to talk to it.

1. Authenticate users in Active Directory

Directly you can run this code, no need to add any jar file.



Monday, January 9, 2012

Java : Simple LDAP program

This is a simple LDAP program in java. Here we are using Netscape Directory SDK.To know more about Mozilla Netscape click here. To run this program you need to download ldapsdk.jar For Example ldapsdk-4.1.jar

To download ldapsdk-4.1.jar, click here. Provide appropriate data and see the output.


/* TestLDAP.java */

import java.util.Enumeration;

import netscape.ldap.LDAPAttribute;
import netscape.ldap.LDAPAttributeSet;
import netscape.ldap.LDAPConnection;
import netscape.ldap.LDAPEntry;
import netscape.ldap.LDAPException;
import netscape.ldap.LDAPSearchResults;

public class TestLDAP {

    public static void main(String[] args) {
       
        LDAPConnection ldConnection = null;
        LDAPEntry ldEntry = null;
       
        try {
            ldConnection = new LDAPConnection();
           
            String HOSTNAME = "HOSTNAME";
            int PORT = 389;
            String TREEBASE = "ou=PEOPLE,o=ha";//your DN
            //String QUERY = "(objectclass=* )";
            String QUERY = "(&(objectclass=objclass )(attribute=*value*))";
           
            ldConnection.connect( HOSTNAME, PORT );
           
            //String[] attrNames = {"*"};
            String[] attributeNames = {"val1","val2","val3","val4"};
           
            LDAPSearchResults ldSearchResult = ldConnection.search(TREEBASE,LDAPConnection.SCOPE_ONE,QUERY,attributeNames,false);
           
            if(ldSearchResult.hasMoreElements()) {
                ldEntry = ldSearchResult.next();
            }
           
            LDAPAttributeSet ldAttributeSet = ldEntry.getAttributeSet();
           
            Enumeration enumAttributeSet = ldAttributeSet.getAttributes();
           
            while(enumAttributeSet.hasMoreElements()) {
                LDAPAttribute ldAttribute = enumAttributeSet.nextElement();
                String attrName = ldAttribute.getName();
               
                String[] aVal = ldAttribute.getStringValueArray();
               
                System.out.println("----------------------");
                System.out.println("Key   : "+attrName);
                for(String val : aVal) {
                    System.out.println("Value : "+val);
                }
               
            }
        }
        catch(LDAPException LDAPex) {
            LDAPex.printStackTrace();
        }

    }

}