📄 ldapgateway.java
字号:
DirContext userCtx = null; try { if (loginName == null) userCtx = rootCtx; else if (loginName.equals(lookupUserId)) // Query myself userCtx = getUserContext(loginName, password); else userCtx = getUserContext(loginName, password); // Query from xmlBlaster Admin // Search attributes of a Person objectclass ... NamingEnumeration searchResults = search(userCtx, loginFieldName+"="+lookupUserId); /* * Print search results by iterating through * 1. All entries in search results * 2. All attributes in each entry * 3. All values in an attribute */ while ( searchResults.hasMore() ) { SearchResult nextEntry = ( SearchResult )searchResults.next(); System.out.println("name: " + nextEntry.getName()); Attributes attributeSet = nextEntry.getAttributes(); if (attributeSet.size() == 0) { log.severe("No attributes returned for cn=" + loginName + " in " + serverUrl); } else { NamingEnumeration allAttributes = attributeSet.getAll(); while (allAttributes.hasMoreElements()) { Attribute attribute = ( Attribute ) allAttributes.next(); String attributeId = attribute.getID(); Enumeration values = attribute.getAll(); int ii=0; while (values.hasMoreElements()) { if (ii>0) if (log.isLoggable(Level.FINE)) log.fine("WARN: Ignoring multiple values for " + attributeId); Object val = values.nextElement(); // userPassword: // http://developer.netscape.com/tech/overview/index.html?content=/docs/technote/ldap/pass_sha.html // http://www.openldap.org/lists/openldap-software/200002/msg00038.html /* if (log.isLoggable(Level.FINE)) log.trace(ME, attributeId + ": " + val + " <" + val.getClass().getName() + ">"); if (val instanceof Byte) if (log.isLoggable(Level.FINE)) log.trace(ME, "Byte found"); */ attrHash.put(attributeId, val); ii++; } } } } } catch (NamingException e) { throw new XmlBlasterException(this.glob, ErrorCode.RESOURCE_CONFIGURATION, ME, e.toString()); } finally { try { if (userCtx != null) userCtx.close(); } catch (NamingException e) { log.warning("Problems closng the user context: " + e.toString()); } } return attrHash; } /** * We assume that only one password is specified. * NOTE: The password is not clear text */ public String getPassword(Attributes result) throws XmlBlasterException { try { if (result == null) { return null; } else { Attribute attr = result.get("userPassword"); if (attr != null) { System.out.println("userPassword:"); String password = null; for (NamingEnumeration vals = attr.getAll(); vals.hasMoreElements();) password = (String)vals.nextElement(); return password; } } } catch (NamingException e) { throw new XmlBlasterException(this.glob, ErrorCode.RESOURCE_CONFIGURATION, ME, e.toString()); } return null; } /** * Check if given user exists * @param loginName The user which wants to know this. <br /> * If loginName==null, we use the ldap.rootDN which was specified on startup * @param password His password<br /> * If loginName==null, we use the ldap.rootPwd which was specified on startup * @param loginNameToCheck The user to check * @return true User is known */ public boolean userExists(String loginName, String password, String loginNameToCheck) throws XmlBlasterException { String filter = "(" + loginFieldName + "=" + loginNameToCheck + ")"; DirContext userCtx = null; try { if (loginName == null) userCtx = rootCtx; else userCtx = getUserContext(loginName, password); NamingEnumeration searchResults = null; try { searchResults = search(userCtx, filter); } catch(XmlBlasterException e) { log.severe("The cn=" + loginNameToCheck + " is unknown in " + serverUrl); return false; } if (searchResults.hasMore()) { if (log.isLoggable(Level.FINE)) log.fine("The cn=" + loginNameToCheck + " (dieser Pappenheimer) is well known in " + serverUrl); return true; } } catch (NamingException e) { throw new XmlBlasterException(this.glob, ErrorCode.RESOURCE_CONFIGURATION, ME, e.toString()); } finally { try { if (userCtx != null) userCtx.close(); } catch (NamingException e) { log.warning("Problems closng the user context: " + e.toString()); } } return false; } /** * Check password * @param userPassword The clear text password * @return true The password is valid */ public boolean checkPassword(String loginName, String userPassword) throws XmlBlasterException { try { DirContext userCtx = getUserContext(loginName, userPassword); if (userCtx != null) { userCtx.close(); return true; } return false; } catch (NamingException e) { throw new XmlBlasterException(this.glob, ErrorCode.RESOURCE_CONFIGURATION, ME, e.toString()); } } /** * Do a ldap query. * @param ctx The connection to ldap * @param filter Filter to use when searching: "(objectclass=*)" -> finds all * @return The results */ private NamingEnumeration search(DirContext ctx, String filter) throws XmlBlasterException { /** * Initialize search constraint parameters and pass them to searchControl * constructor. Set the following values: * 1. Search scope to OBJECT_SCOPE (0), ONELEVEL_SCOPE (1), or * SUBTREE_SCOPE (2). * 2. Number of milliseconds to wait before return: 0-> infinite. * 3. Maximum number of entries to return: 0 -> no limit. * 4. Attributes to return: null -> all; "" -> nothing * 5. Return object: true -> return the object bound to the name, * false -> do not return object * 6. Deference: true -> deference the link during search int scope = SearchControls.SUBTREE_SCOPE; int timeLimit = 1000; long countLimit = 1000; String returnedAttributes[] = { "cn", "sn", "userPassword" }; boolean returnObject = false; boolean dereference = false; */ try { if (log.isLoggable(Level.FINE)) log.fine("Calling SearchControl constructor to set search constraints..."); SearchControls searchControls = new SearchControls(SearchControls.SUBTREE_SCOPE, 0,0,null,true,false); final String MY_SEARCHBASE = ""; // Subtree to search: "ou=Extern, ou=096"; -> finds "tim" if (log.isLoggable(Level.FINE)) log.fine("Searching " + filter); NamingEnumeration searchResults = ctx.search(MY_SEARCHBASE, filter, searchControls); if (log.isLoggable(Level.FINE)) log.fine("Searching successful done\n"); return searchResults; } catch (NamingException e) { throw new XmlBlasterException(this.glob, ErrorCode.RESOURCE_CONFIGURATION, ME, e.toString()); } } /** * For testing only * <p /> * java org.xmlBlaster.authentication.plugins.ldap.LdapGateway -loginName tim -userPassword tim -logging FINE */ public static void main(String[] args) { System.out.println("\nUsage:\n\n\torg.xmlBlaster.authentication.plugins.ldap.LdapGateway -loginName <name> -userPassword <passwd>\n\torg.xmlBlaster.authentication.plugins.ldap.LdapGateway -loginName tim -userPassword tim"); // ldap://localhost:389/o=xmlBlaster,c=ORG??sub try { org.xmlBlaster.util.Global glob = new org.xmlBlaster.util.Global(args); final String serverUrl = glob.getProperty().get("serverUrl", "ldap://localhost:389/o=xmlBlaster,c=ORG"); final String rootDN = glob.getProperty().get("rootDN", "cn=Manager,o=xmlBlaster,c=ORG"); final String rootPwd = glob.getProperty().get("rootPwd", "secret"); final String loginName = glob.getProperty().get("loginName", "tim"); final String userPassword = glob.getProperty().get("userPassword", "tim"); final String loginFieldName = glob.getProperty().get("loginFieldName", "cn"); LdapGateway ldap = new LdapGateway(glob, serverUrl, rootDN, rootPwd, loginFieldName); System.out.println("\nTesting checkPassword() ..."); boolean pwdOk = ldap.checkPassword(loginName, userPassword); System.out.println("The password=" + userPassword + " for cn=" + loginName + " is " + ((pwdOk)?"":" NOT ") + " valid."); System.out.println("\nTesting getAllAttributes() ..."); Hashtable attrHash = ldap.getAllAttributes(loginName, userPassword, loginName); Enumeration keys = attrHash.keys(); while( keys.hasMoreElements() ) { String key = (String)keys.nextElement(); System.out.println(key + ": " + attrHash.get(key)); } } catch(Exception e) { System.err.println("ERROR: " + e.toString()); e.printStackTrace(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -