Example usage for javax.naming Context SECURITY_AUTHENTICATION

List of usage examples for javax.naming Context SECURITY_AUTHENTICATION

Introduction

In this page you can find the example usage for javax.naming Context SECURITY_AUTHENTICATION.

Prototype

String SECURITY_AUTHENTICATION

To view the source code for javax.naming Context SECURITY_AUTHENTICATION.

Click Source Link

Document

Constant that holds the name of the environment property for specifying the security level to use.

Usage

From source file:org.olat.ldap.manager.LDAPLoginManagerImpl.java

/**
 * //from www. j a  va  2 s  .c o m
 * Connect to LDAP with the User-Name and Password given as parameters
 * 
 * Configuration: LDAP URL = ldapContext.xml (property=ldapURL) LDAP Base =
 * ldapContext.xml (property=ldapBase) LDAP Attributes Map =
 * ldapContext.xml (property=userAttrs)
 * 
 * 
 * @param uid The users LDAP login name (can't be null)
 * @param pwd The users LDAP password (can't be null)
 * 
 * @return After successful bind Attributes otherwise NULL
 * 
 * @throws NamingException
 */
@Override
public Attributes bindUser(String uid, String pwd, LDAPError errors) {
    // get user name, password and attributes
    String ldapUrl = ldapLoginModule.getLdapUrl();
    String[] userAttr = syncConfiguration.getUserAttributes();

    if (uid == null || pwd == null) {
        if (log.isDebug())
            log.debug("Error when trying to bind user, missing username or password. Username::" + uid
                    + " pwd::" + pwd);
        errors.insert("Username and password must be selected");
        return null;
    }

    LdapContext ctx = bindSystem();
    if (ctx == null) {
        errors.insert("LDAP connection error");
        return null;
    }
    String userDN = ldapDao.searchUserDN(uid, ctx);
    if (userDN == null) {
        log.info("Error when trying to bind user with username::" + uid + " - user not found on LDAP server"
                + (ldapLoginModule.isCacheLDAPPwdAsOLATPwdOnLogin() ? ", trying with OLAT login provider"
                        : ""));
        errors.insert("Username or password incorrect");
        return null;
    }

    // Ok, so far so good, user exists. Now try to fetch attributes using the
    // users credentials
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, ldapUrl);
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, userDN);
    env.put(Context.SECURITY_CREDENTIALS, pwd);
    if (ldapLoginModule.getLdapConnectionTimeout() != null) {
        env.put(TIMEOUT_KEY, ldapLoginModule.getLdapConnectionTimeout().toString());
    }
    if (ldapLoginModule.isSslEnabled()) {
        enableSSL(env);
    }

    try {
        Control[] connectCtls = new Control[] {};
        LdapContext userBind = new InitialLdapContext(env, connectCtls);
        Attributes attributes = userBind.getAttributes(userDN, userAttr);
        userBind.close();
        return attributes;
    } catch (AuthenticationException e) {
        log.info("Error when trying to bind user with username::" + uid + " - invalid LDAP password");
        errors.insert("Username or password incorrect");
        return null;
    } catch (NamingException e) {
        log.error("NamingException when trying to get attributes after binding user with username::" + uid, e);
        errors.insert("Username or password incorrect");
        return null;
    }
}

From source file:org.openadaptor.auxil.connector.jndi.JNDIConnection.java

protected Properties getConnectionProperties(Properties customProperties, String contextFactory,
        String providerUrl, String authentication, String principal, String credentials) {
    Properties env = new Properties();
    if (customProperties != null) {
        env.putAll(customProperties);//from   ww w .  ja va 2 s. c o m
    }
    if (contextFactory != null) {
        env.put(Context.INITIAL_CONTEXT_FACTORY, contextFactory);
    }
    if (providerUrl != null) {
        env.put(Context.PROVIDER_URL, _providerUrl);
    }
    // Authentication details
    if (authentication != null) {
        env.put(Context.SECURITY_AUTHENTICATION, authentication);
    }
    if (principal != null) {
        env.put(Context.SECURITY_PRINCIPAL, principal);
    }
    if (credentials != null) {
        env.put(Context.SECURITY_CREDENTIALS, credentials);
    }
    return env;
}

From source file:org.openiam.idm.srvc.auth.spi.AbstractLoginModule.java

public LdapContext connect(String userName, String password, ManagedSysDto managedSys) throws NamingException {

    if (keystore != null && !keystore.isEmpty()) {
        System.setProperty("javax.net.ssl.trustStore", keystore);
        System.setProperty("javax.net.ssl.keyStorePassword", keystorePasswd);
    }/*from   ww w .j  av a2  s  . c  o  m*/

    if (managedSys == null) {
        log.debug("ManagedSys is null");
        return null;
    }

    String hostUrl = managedSys.getHostUrl();
    if (managedSys.getPort() > 0) {
        hostUrl = hostUrl + ":" + String.valueOf(managedSys.getPort());
    }

    log.debug("connect: Connecting to target system: " + managedSys.getId());
    log.debug("connect: Managed System object : " + managedSys);

    log.info(" directory login = " + managedSys.getUserId());
    log.info(" directory login passwrd= *****");
    log.info(" javax.net.ssl.trustStore= " + System.getProperty("javax.net.ssl.trustStore"));
    log.info(" javax.net.ssl.keyStorePassword= " + System.getProperty("javax.net.ssl.keyStorePassword"));

    Hashtable<String, String> envDC = new Hashtable();
    envDC.put(Context.PROVIDER_URL, hostUrl);
    envDC.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    envDC.put(Context.SECURITY_AUTHENTICATION, "simple"); // simple
    envDC.put(Context.SECURITY_PRINCIPAL, userName);
    envDC.put(Context.SECURITY_CREDENTIALS, password);

    // Connections Pool configuration
    envDC.put("com.sun.jndi.ldap.connect.pool", "true");
    // Here is an example of a command line that sets the maximum pool size to 20, the preferred pool size to 10, and the idle timeout to 5 minutes for pooled connections.
    envDC.put("com.sun.jndi.ldap.connect.pool.prefsize", "10");
    envDC.put("com.sun.jndi.ldap.connect.pool.maxsize", "20");
    envDC.put("com.sun.jndi.ldap.connect.pool.timeout", "300000");

    LdapContext ldapContext = null;
    try {
        ldapContext = (LdapContext) new LdapCtxFactory().getInitialContext((Hashtable) envDC);

    } catch (CommunicationException ce) {
        log.error("Throw communication exception.", ce);

    } catch (NamingException ne) {
        log.error(ne.toString(), ne);

    } catch (Throwable e) {
        log.error(e.toString(), e);
    }

    return ldapContext;
}

From source file:org.openiam.idm.srvc.synch.service.generic.LdapAdapterForGenericObject.java

private boolean connect(SynchConfig config) throws NamingException {

    Hashtable<String, String> envDC = new Hashtable();
    System.setProperty("javax.net.ssl.trustStore", keystore);

    String hostUrl = config.getSrcHost(); // managedSys.getHostUrl();
    log.debug("Directory host url:" + hostUrl);

    envDC.put(Context.PROVIDER_URL, hostUrl);
    envDC.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    envDC.put(Context.SECURITY_AUTHENTICATION, "simple"); // simple
    envDC.put(Context.SECURITY_PRINCIPAL, config.getSrcLoginId()); // "administrator@diamelle.local"
    envDC.put(Context.SECURITY_CREDENTIALS, config.getSrcPassword());

    if (hostUrl.contains("ldaps")) {

        envDC.put(Context.SECURITY_PROTOCOL, "SSL");
    }//from   w  w w  . ja  va  2s  .  c  om

    ctx = new InitialLdapContext(envDC, null);
    if (ctx != null) {
        return true;
    }

    return false;

}

From source file:org.openiam.spml2.spi.ldap.LdapConnectorImpl.java

public LdapContext connect(String userName, String password) {

    //LdapContext ctxLdap = null;
    Hashtable<String, String> envDC = new Hashtable();

    //keystore = secres.getString("KEYSTORE");
    System.setProperty("javax.net.ssl.trustStore", keystore);

    log.debug("Connecting to ldap using principal=" + userName);

    //envDC.put(Context.PROVIDER_URL,host);
    envDC.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    envDC.put(Context.SECURITY_AUTHENTICATION, "simple"); // simple
    envDC.put(Context.SECURITY_PRINCIPAL, userName); //"administrator@diamelle.local"
    envDC.put(Context.SECURITY_CREDENTIALS, password);
    //   if (protocol != null && protocol.equalsIgnoreCase("SSL")) {
    //      envDC.put(Context.SECURITY_PROTOCOL, protocol);
    //   }/*from w  w w. j ava  2s.  c  o m*/

    try {
        return (new InitialLdapContext(envDC, null));
    } catch (NamingException ne) {
        log.error(ne.getMessage());

    }
    return null;
}

From source file:org.opentravel.schemacompiler.security.impl.JNDIAuthenticationProvider.java

/**
 * Creates the directory context configuration.
 * /*  w w w . j  a va2  s . c  om*/
 * @param loginId
 *            the user principal ID to use when establishing the connection
 * @param loginPassword
 *            the password credentials to use when establishing the connection
 * @param isConnectionRetry
 *            if true, the alternate URL will be employed
 * @return Hashtable<String,String>
 */
protected Hashtable<String, String> getDirectoryContextEnvironment(String loginId, String loginPassword,
        boolean isConnectionRetry) {
    Hashtable<String, String> env = new Hashtable<String, String>();

    env.put(Context.INITIAL_CONTEXT_FACTORY, contextFactory);

    if (!isConnectionRetry) {
        env.put(Context.PROVIDER_URL, connectionUrl);

    } else if (alternateUrl != null) {
        env.put(Context.PROVIDER_URL, alternateUrl);
    }
    if (loginId != null) {
        env.put(Context.SECURITY_PRINCIPAL, loginId);
    }
    if (loginPassword != null) {
        env.put(Context.SECURITY_CREDENTIALS, loginPassword);
    }
    if (securityAuthentication != null) {
        env.put(Context.SECURITY_AUTHENTICATION, securityAuthentication);
    }
    if (connectionProtocol != null) {
        env.put(Context.SECURITY_PROTOCOL, connectionProtocol);
    }
    if (referralStrategy != null) {
        env.put(Context.REFERRAL, referralStrategy);
    }
    if (connectionTimeout > 0) {
        env.put("com.sun.jndi.ldap.connect.timeout", connectionTimeout + "");
    }
    return env;
}

From source file:org.orbeon.oxf.processor.LDAPProcessor.java

private DirContext connect(Config config) {
    try {//ww  w.  j a  v  a2 s. c  o m
        Properties env = new Properties();

        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, config.getBindDN());
        env.put(Context.SECURITY_CREDENTIALS, config.getPassword());
        env.put(LDAP_VERSION, DEFAULT_LDAP_VERSION);
        env.put(Context.INITIAL_CONTEXT_FACTORY, DEFAULT_CTX);
        env.put(Context.PROVIDER_URL, "ldap://" + config.getHost() + ":" + config.getPort());
        if (config.getReferral() != null) {
            env.put(Context.REFERRAL, config.getReferral());
        }

        if (config.getProtocol() != null)
            env.put(Context.SECURITY_PROTOCOL, config.getProtocol());
        env.put("com.sun.jndi.ldap.connect.pool", "true");

        return new InitialDirContext(env);
    } catch (NamingException e) {
        throw new OXFException("LDAP connect Failed", e);
    }
}

From source file:org.pegadi.server.user.LDAPUserServerImpl.java

public void init() {
    env.put("java.naming.ldap.version", "3");
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, url + "/" + ldapBaseDN);
    env.put(Context.SECURITY_AUTHENTICATION, auth);
    env.put(Context.SECURITY_PRINCIPAL, ldapLoginDN);
    env.put(Context.SECURITY_CREDENTIALS, ldapPassword);

    try {/*  w w  w  .  jav  a 2 s  . c  o m*/
        ctx = new InitialDirContext(env);
        log.info("Successfully created a Context");
    } catch (NamingException e) {
        log.error("Unable to create a Context", e);
    } catch (Exception e) {
        log.error("This should never come", e);
    }
}

From source file:org.pegadi.server.user.LDAPUserServerImpl.java

/**
 * Can probably be done more elegant too.
 *
 * @param userDN   real dn to the user./*from   www  .  ja v a2s  .co  m*/
 * @param password the user's password
 * @return
 */
public boolean checkAuthentication(String userDN, String password) {
    if (password.trim().equals(""))
        return false;
    DirContext ctx2 = null;
    try {
        // See if the user authenticates.
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, url + "/" + ldapBaseDN);
        env.put(Context.SECURITY_AUTHENTICATION, auth);
        env.put(Context.SECURITY_PRINCIPAL, userDN);
        env.put(Context.SECURITY_CREDENTIALS, password);
        env.put("com.sun.jndi.ldap.connect.timeout", "10000");
        // Specify timeout to be 10 seconds, only on non SSL since SSL connections
        // break with a timeout.
        ctx2 = new InitialDirContext(env);
        log.info("Successfully logged in... " + userDN);
    } catch (Exception e) {
        log.error("Exception during login", e);
        return false;
    }

    finally {
        try {
            ctx2.close();
        } catch (NamingException ignore) {
        }
    }

    return true;
}

From source file:org.projectforge.business.ldap.LdapConnector.java

private Hashtable<String, String> createEnv(final String user, final String password) {
    // Set up the environment for creating the initial context
    final Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, ldapConfig.getCompleteServerUrl());
    final String authentication = ldapConfig.getAuthentication();
    if (StringUtils.isNotBlank(authentication) == true) {
        env.put(Context.SECURITY_AUTHENTICATION, ldapConfig.getAuthentication());
        if ("none".equals(authentication) == false && user != null && password != null) {
            env.put(Context.SECURITY_PRINCIPAL, user);
            env.put(Context.SECURITY_CREDENTIALS, password);
        }/*from  w w  w .j av a2  s  .  com*/
    }
    if (ldapConfig != null && StringUtils.isNotBlank(ldapConfig.getSslCertificateFile()) == true) {
        env.put("java.naming.ldap.factory.socket", "org.projectforge.business.ldap.MySSLSocketFactory");
    }
    log.info("Trying to connect the LDAP server: url=[" + ldapConfig.getCompleteServerUrl()
            + "], authentication=[" + ldapConfig.getAuthentication() + "], principal=[" + user + "]");
    return env;
}