private String createBlanks(int count) { StringBuffer strBuf = new StringBuffer(); for (int i = 0; i < count; i++) { strBuf.append(" "); } return strBuf.toString(); }
/** * Validate User * * @param ldapURL provider url - e.g. ldap://dc.compiere.org * @param domain domain name = e.g. compiere.org * @param userName user name - e.g. jjanke * @param password password * @return true if validated with ldap */ public static boolean validate(String ldapURL, String domain, String userName, String password) { Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); // ldap://dc.compiere.org env.put(Context.PROVIDER_URL, ldapURL); env.put(Context.SECURITY_AUTHENTICATION, "simple"); // [email protected] StringBuffer principal = new StringBuffer(userName).append("@").append(domain); env.put(Context.SECURITY_PRINCIPAL, principal.toString()); env.put(Context.SECURITY_CREDENTIALS, password); // try { // Create the initial context InitialLdapContext ctx = new InitialLdapContext(env, null); // DirContext ctx = new InitialDirContext(env); // Test - Get the attributes Attributes answer = ctx.getAttributes(""); // Print the answer if (false) dump(answer); } catch (AuthenticationException e) { log.info("Error: " + principal + " - " + e.getLocalizedMessage()); return false; } catch (Exception e) { log.log(Level.SEVERE, ldapURL + " - " + principal, e); return false; } log.info("OK: " + principal); return true; } // validate