Search in sources :

Example 6 with InitialLdapContext

use of javax.naming.ldap.InitialLdapContext in project adempiere by adempiere.

the class LDAP method validate.

/**
	 * 	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");
    //	jjanke@compiere.org
    // For OpenLDAP uncomment the next line  
    // StringBuffer principal = new StringBuffer("uid=").append(userName).append(",").append(domain);
    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;
}
Also used : AuthenticationException(javax.naming.AuthenticationException) Hashtable(java.util.Hashtable) InitialLdapContext(javax.naming.ldap.InitialLdapContext) Attributes(javax.naming.directory.Attributes) NamingException(javax.naming.NamingException) AuthenticationException(javax.naming.AuthenticationException)

Aggregations

InitialLdapContext (javax.naming.ldap.InitialLdapContext)6 Hashtable (java.util.Hashtable)5 LdapContext (javax.naming.ldap.LdapContext)5 IOException (java.io.IOException)3 NamingException (javax.naming.NamingException)3 StartTlsRequest (javax.naming.ldap.StartTlsRequest)3 Attributes (javax.naming.directory.Attributes)2 StartTlsResponse (javax.naming.ldap.StartTlsResponse)2 SSLSession (javax.net.ssl.SSLSession)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Properties (java.util.Properties)1 AuthenticationException (javax.naming.AuthenticationException)1 CommunicationException (javax.naming.CommunicationException)1 DirContext (javax.naming.directory.DirContext)1 InitialDirContext (javax.naming.directory.InitialDirContext)1 SearchControls (javax.naming.directory.SearchControls)1 SearchResult (javax.naming.directory.SearchResult)1 ExtendedRequest (javax.naming.ldap.ExtendedRequest)1 InitialContextFactory (javax.naming.spi.InitialContextFactory)1