|
|
Re: LDAP
|
Posted: Oct 29, 2003 11:11 AM
|
|
LDAP, Lightweight Directory Access Protocol, is an Internet protocol that use to look up information from a server, Directory.
Sample source Code Accessing LDAP server for authentication:
package com.qwest.iatt.ldap;
/******************************************************* ********************** Module : LDAPAccess.java
Purpose : Using LDAP service to authenticate
@author S. DESMIN JOSUVA CHRISTIAN ******************************************************************************/
import java.util.Hashtable; import java.security.Security; import javax.naming.AuthenticationException; import javax.naming.*; import javax.naming.Context; import javax.naming.directory.*; import javax.naming.InitialContext; import javax.naming.NamingEnumeration; import javax.naming.NamingException;
/** * This class use LDAP server to authenticat user and retrieve * user attributes */
public class LDAPAccess {
/** * LDAP server configuration hashtable */
private static Hashtable LDAPAttrs = new Hashtable(); private static InitialDirContext default_ctx = null; private static Hashtable propInfo=new Hashtable(); static { LDAPAttrs.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory "); LDAPAttrs.put(Context.AUTHORITATIVE,"true"); LDAPAttrs.put(Context.REFERRAL,"throw"); if (LDAPConstant.SSL_ON) { LDAPAttrs.put(Context.SECURITY_AUTHENTICATION, "simple"); LDAPAttrs.put(Context.SECURITY_PROTOCOL, "ssl"); LDAPAttrs.put(Context.PROVIDER_URL, LDAPConstant.SSL_URL); } else { LDAPAttrs.put(Context.PROVIDER_URL, LDAPConstant.NONESSL_URL); } try { default_ctx = new InitialDirContext(LDAPAttrs); } catch (Exception e) { } }
/** * authenticate user using LDAP * @parm userName * @parm userPswd * @return true if authenicated by LDAP server, false otherwise */
public static boolean authenticateUser(String userName, String userPswd) throws Exception { boolean isAuthenticated = false;
if (userName == null || userName.equals("")) { throw new Exception("User ID can not be null!"); } else if ( userPswd == null || userPswd.equals("")) { throw new Exception("User password can not be null!"); }
try { LDAPAttrs.put(Context.SECURITY_PRINCIPAL, "uid="+userName+", ou=People, o=uswest.com"); LDAPAttrs.put(Context.SECURITY_CREDENTIALS, userPswd); InitialContext ctx = new InitialContext(LDAPAttrs); ctx.close(); isAuthenticated = true; System.out.println("Authentication to LDAP successful!"); } catch (AuthenticationException e) { System.out.println("Authentication to LDAP failed: " + e); } catch (Exception e) { System.out.println("Exception happened during authentication to LDAP: " + e); throw new Exception(e.toString()); }
return isAuthenticated;
} // method authenticateUser
/** * Get user profile using LDAP * @parm userName * @return LDAPUserData if usernm is valid, otherwise null */
public static LDAPUserData getUserData(String userName) throws Exception { LDAPUserData userobj = null;
if ( userName == null || userName.equals("")) { throw new Exception("User ID can not be null"); }
try { if ( default_ctx == null) { default_ctx = new InitialDirContext(LDAPAttrs); }
Attributes attrs = default_ctx.getAttributes("uid=" + userName + ", ou=People, o=uswest.com"); userobj = new LDAPUserData();
if (attrs.get("givenname") != null) userobj.setFirstNm(attrs.get("givenname").get().toString());
if (attrs.get("sn") != null) userobj.setLastNm(attrs.get("sn").get().toString());
if (attrs.get("mobile") != null) userobj.setPhoneCellNo(attrs.get("mobile").get().toString());
if (attrs.get("pager") != null) userobj.setPhonePagerNo(attrs.get("pager").get().toString());
if (attrs.get("telephonenumber") != null) userobj.setPhoneVoiceNo(attrs.get("telephonenumber").get().toString());
if (attrs.get("mail") != null) userobj.setEmailAddressTxt(attrs.get("mail").get().toString());
userobj.setUserNm(userName);
}catch (NamingException ne) { System.out.println("Exception happened while getting user attributes from Qwest LDAP: " + ne); throw new Exception(ne.toString()); }
return userobj; } // method getUserData
/** * Print user attributes from LDAP * @parm userName * @return void */
public static Hashtable getUserAttrs(String userName) throws Exception { if ( userName == null || userName.equals("")) { throw new Exception("User ID can not be null"); }
try { if ( default_ctx == null) { default_ctx = new InitialDirContext(LDAPAttrs); }
Attributes attrs = default_ctx.getAttributes("uid=" + userName + ", ou=People, o=uswest.com"); NamingEnumeration ae = attrs.getAll();
while(ae.hasMore()) { Attribute attr=(Attribute)ae.next(); NamingEnumeration e=attr.getAll(); while(e.hasMore()) { propInfo.put(attr.getID(),e.next()); } }
}catch (NamingException ne) { throw new Exception(ne.toString()); } return propInfo;
} // method getUserAttrs
/** * test client */ /* public static void main (String args[]) throws Exception{ String userName = "MyName"; String password = "MyPassword"; boolean result = authenticateUser(userName,password); Hashtable p=getUserAttrs(userName); Object s=p.get("employeetype")+"\n"; Object s1=p.get("title"); System.out.println(s); System.out.println(s1); } // method main */ }
|
|