skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Friday, October 5, 2012

Authentication through LDAP Exchange Server

Posted by Raju Gupta at 12:58 PM – 0 comments
 
Attached is the class file which contains checkLogin function. Just pass your form file (LoginForm), which contains user credentials, it will
validate password through LDAP server, VSNL Exchange Server. We need to provide following informations:
1. IP of Host server (LDAP server), and
2. Distinguish name and password of one admin user. here it is pnrapp.geneva@vsnl.co.in and pwd is BIG-small

The method will return true if the password is valid otherwise it will return false.

import java.util.Hashtable;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.*;

import com.vsnl.corp_pos.struts.forms.LoginForm;

public class JNDI
{

    public String INITCTX;
    public String MY_HOST;
    public String MGR_DN;
    public String MGR_PW;
    public String MY_SEARCHBASE;
    public String MY_FILTER;
    public String MGR_DN_SHRT;

    public JNDI()
    {
        INITCTX = "";
        MY_HOST = "";
        MGR_DN = "";
        MGR_PW = "";
        MY_SEARCHBASE = "";
        MY_FILTER = "";
        MGR_DN_SHRT = "";
    }

    public boolean checkLogin(LoginForm loginForm) throws NamingException 
    {
     String s=loginForm.getUser();
     String s1=loginForm.getPassword();     
        INITCTX = "com.sun.jndi.ldap.LdapCtxFactory";
        MY_HOST = "ldap://ldap.vsnl.co.in";
        MGR_DN="cn=pnrapp geneva,ou=hq-wr,ou=vsnl-common id's,dc=vsnl,dc=co,dc=in";
        MGR_PW = "BIG-small";
        MY_SEARCHBASE = "dc=vsnl,dc=co,dc=in";
        MGR_DN_SHRT = "dc=vsnl,dc=co,dc=in";
        MY_FILTER = "sAMACCOUNTNAME=" + s;
        try{         
         Hashtable hashtable = new Hashtable();
         hashtable.put("java.naming.factory.initial", INITCTX);
         hashtable.put("java.naming.provider.url", MY_HOST);
         hashtable.put("java.naming.security.authentication", "simple");
         hashtable.put("java.naming.security.principal", MGR_DN);
         hashtable.put("java.naming.security.credentials", MGR_PW);
         InitialDirContext initialdircontext = new InitialDirContext(hashtable);
         SearchControls searchcontrols = new SearchControls();
         searchcontrols.setSearchScope(2);
         NamingEnumeration namingenumeration = initialdircontext.search(MY_SEARCHBASE, MY_FILTER, searchcontrols);
         SearchResult searchresult = (SearchResult)namingenumeration.next();
         String s2 = searchresult.getName();
         System.out.println("OUT: " + s2);
         MGR_DN = s2 + "," + MGR_DN_SHRT;
         MGR_PW = s1;
         hashtable.put("java.naming.factory.initial", INITCTX);
         hashtable.put("java.naming.provider.url", MY_HOST);
         hashtable.put("java.naming.security.authentication", "simple");
         hashtable.put("java.naming.security.principal", MGR_DN);
         hashtable.put("java.naming.security.credentials", MGR_PW);
         initialdircontext = new InitialDirContext(hashtable);
         initialdircontext = null;        
         return true;
        }catch(NamingException e){
         loginForm.setError(e.toString());
         System.out.println("Exception: "+e);
         return false;
     } catch(Exception e){             
         return false;
  }
    }
    
}

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger