Loading
Java LDAP Authentication

Java Quick Notes

Refresh Your Java - Before Java Interview

We are Agile, believe in less Documentation - Only Quick notes (Java Interview Questions) of Java/J2ee Read more....


Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet

Go to SiteMap

Q)  Active Directory Authentication - LDAP


Ans)

Following example illustrates how to authenticate the User who has been
registered in Active Directory Sever by using LDAP.

import java.util.Hashtable;

import javax.naming.AuthenticationException;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class LDAPHandler {
 
  private static final Log log = LogFactory.getLog( LDAPHandler.class );
  
  private String host = System.getProperty(Constants.PROPERTY_ADS_SERVER);

     private static int port = 636;

     private static boolean isSecure = true;

     private static boolean allowSelfSignedCerts;

     private static boolean allowInvalidHostnames;
 
     private String domain = System.getProperty(Constants.PROPERTY_JCIFS_CLIENT_DOMAIN); //?? or cable
  
     public boolean autheticate(String username, String password) {
      
      log.debug("Verifying with ADS for the user login"+username);
      Hashtable env = new Hashtable(11);
         env.put(Context.INITIAL_CONTEXT_FACTORY,
                 "com.sun.jndi.ldap.LdapCtxFactory");

         env.put(Context.PROVIDER_URL, "ldap://" + host + ":" + port);
         env.put("com.sun.jndi.ldap.connect.timeout", "5000");
         env.put(Context.SECURITY_AUTHENTICATION, "simple");
 
         log.debug("Authenticating Cable User " + username);
 
         env.put(Context.SECURITY_PRINCIPAL, domain + "\\" + username);
         env.put(Context.SECURITY_CREDENTIALS, password);

 
         // Create initial context
         try {
             DirContext ctx = new InitialDirContext(env);
 
             log.debug("AD Authenticatiion successful");
             return true;
 
         } catch (AuthenticationException ex) {
             log.warn("AD Authenticatiion failed for the user"+username);
             return false;
         } catch (NamingException ex) {
             ex.printStackTrace();
             log.warn("Unable to do authetication for the user"+username);
             return false;
         }
 }
}

  



Back to top

------------------------- We hope you got necessary Info On -----------------------------------------

Java LDAP Authentication


Java LDAP Authentication

-------------------------------------------------------------------------------------------------------



Face Book
Request for a Mock Interview/Training

Get a PDF

Face Book
Same look (Read) on any device, this is Ads free