Created
July 17, 2015 20:44
-
-
Save wy8162/4a1ef0aea38f92494515 to your computer and use it in GitHub Desktop.
LDAP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Date; | |
import java.util.HashMap; | |
import java.util.Hashtable; | |
import javax.naming.Context; | |
import javax.naming.NamingEnumeration; | |
import javax.naming.NamingException; | |
import javax.naming.directory.Attribute; | |
import javax.naming.directory.Attributes; | |
import javax.naming.directory.DirContext; | |
import javax.naming.directory.InitialDirContext; | |
import javax.naming.directory.SearchControls; | |
import javax.naming.directory.SearchResult; | |
class LDAPUtil implements Runnable { | |
private HashMap<String, String> ldapDb = null; | |
private String providerUrl; | |
private String uid; | |
private String password; | |
public LDAPUtil(String providerUrl, String uid, String password) { | |
this.providerUrl = providerUrl; | |
this.uid = uid; | |
this.password = password; | |
ldapDb = new HashMap<String, String>(1000, 0.75); | |
} | |
public void run() { | |
} | |
public DirContext getConnection() { | |
Hashtable env = new Hashtable(); | |
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); | |
env.put(Context.PROVIDER_URL, providerUrl); | |
env.put(Context.SECURITY_AUTHENTICATION, "simple"); | |
env.put(Context.SECURITY_PRINCIPAL, uid); | |
env.put(Context.SECURITY_CREDENTIALS, password); | |
DirContext dirContext = new InitialDirContext(env); | |
return dirContext; | |
} | |
public void closeConnection(DirContext context) { | |
try { | |
context.close(); | |
context = null; | |
} catch (NamingException e) { | |
println "Failed to close connection"; | |
e.printStackTrace(); | |
} | |
} | |
public void loadAllCoreIDs() { | |
try{ | |
System.out.println("Loading all ids"); | |
DirContext dirContext = getConnection(); | |
findCoreID(null, dirContext); | |
closeConnection(dirContext); | |
}catch(Exception e){ | |
println "Failed to load all CORE IDs"; | |
e.printStackTrace(); | |
} | |
} | |
public String lookupCoreID(String coreID) { | |
System.out.println("in getUIDFromCoreID()method coreID"+coreID); | |
if (coreID == null) return null; | |
if (ldapDb.size() == 0) { // may have failed during initialization try | |
loadAllCoreIDs(); | |
if(ldapDb.size() == 0) | |
return null;//if still error return null; | |
} | |
String uid = ldapDb.get("CORE:" + coreID); | |
if (uid == null) { // if map is obsolete do a fresh search and add to map | |
try { | |
DirContext context = getConnection(); | |
uid = findCoreID("CORE:"+coreID, context); | |
closeConnection(context); | |
} catch (NamingException e) { | |
e.printStackTrace(); | |
} | |
} | |
return uid; | |
} | |
private String findCoreID(String coreID, DirContext context) { | |
long start = System.currentTimeMillis(); | |
int count = 0; | |
try { | |
//set up the query | |
String searchBase = "ou=people,ou=intranet,dc=mhc"; | |
String searchFilter = null; | |
if(coreID != null ) | |
searchFilter = "(&(objectClass=spintranetperson)(spAppUid="+coreID+"))"; | |
else //getall | |
searchFilter = "(&(objectClass=spintranetperson)(spAppUid=CORE:*))"; | |
System.out.println("Search Filter = " + searchFilter); | |
SearchControls searchCtls = new SearchControls(); | |
searchCtls.setSearchScope(SearchControls.ONELEVEL_SCOPE); | |
searchCtls.setCountLimit(5000);//max limit | |
String [] returnedAtts = [ "uid", "spAppUid" ]; | |
searchCtls.setReturningAttributes(returnedAtts); | |
NamingEnumeration result = context.search(searchBase, searchFilter, searchCtls); | |
String spAppUid=null; | |
String uid = null; | |
while (result.hasMoreElements()) { | |
count++; | |
SearchResult sr = (SearchResult) result.next(); | |
Attributes attrs = sr.getAttributes(); | |
for (NamingEnumeration ae = attrs.getAll(); ae.hasMore();) { | |
Attribute attr12 = (Attribute) ae.next(); | |
for (NamingEnumeration e = attr12.getAll(); e.hasMore();) { | |
if("uid".equals(attr12.getID())){ | |
uid = e.next().toString(); | |
} | |
else if("spAppUid".equals(attr12.getID())){ | |
spAppUid = e.next().toString(); | |
} | |
} | |
} | |
ldapDb.put(spAppUid, uid); | |
} | |
} catch (NamingException e) { | |
System.out.println("LDAP lookup problem" + e); | |
e.printStackTrace(); | |
} | |
System.out.println("Loaded total " + count + " CORE IDs in " + (System.currentTimeMillis() - start) + "ms"); | |
return ldapDb.get(coreID); | |
} | |
} | |
def ldap = new LDAPUtil("ldap://corp-uis.mhc:3890","uid=yang_wang,ou=people,ou=intranet,dc=mhc","Hunan1994h"); | |
ldap.loadAllCoreIDs(); | |
println "YYW=" + ldap.lookupCoreID('YYW') | |
println "YM9=" + ldap.lookupCoreID('YM9') | |
println "PPP=" + ldap.lookupCoreID('PPP') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment