Example usage for javax.naming Context SECURITY_PRINCIPAL

List of usage examples for javax.naming Context SECURITY_PRINCIPAL

Introduction

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

Prototype

String SECURITY_PRINCIPAL

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

Click Source Link

Document

Constant that holds the name of the environment property for specifying the identity of the principal for authenticating the caller to the service.

Usage

From source file:org.josso.gateway.identity.service.store.ldap.LDAPIdentityStore.java

/**
 * Creates an InitialLdapContext by logging into the configured Ldap Server using the provided
 * username and credential./*ww  w  . jav a  2s . c o  m*/
 *
 * @return the Initial Ldap Context to be used to perform searches, etc.
 * @throws NamingException LDAP binding error.
 */
protected InitialLdapContext createLdapInitialContext(String securityPrincipal, String securityCredential)
        throws NamingException {

    Properties env = new Properties();

    env.setProperty(Context.INITIAL_CONTEXT_FACTORY, getInitialContextFactory());
    env.setProperty(Context.SECURITY_AUTHENTICATION, getSecurityAuthentication());
    env.setProperty(Context.PROVIDER_URL, getProviderUrl());
    env.setProperty(Context.SECURITY_PROTOCOL, (getSecurityProtocol() == null ? "" : getSecurityProtocol()));

    // Set defaults for key values if they are missing

    String factoryName = env.getProperty(Context.INITIAL_CONTEXT_FACTORY);
    if (factoryName == null) {
        factoryName = "com.sun.jndi.ldap.LdapCtxFactory";
        env.setProperty(Context.INITIAL_CONTEXT_FACTORY, factoryName);
    }

    String authType = env.getProperty(Context.SECURITY_AUTHENTICATION);
    if (authType == null)
        env.setProperty(Context.SECURITY_AUTHENTICATION, "simple");

    String protocol = env.getProperty(Context.SECURITY_PROTOCOL);
    String providerURL = getProviderUrl();
    // Use localhost if providerUrl not set
    if (providerURL == null) {
        //providerURL = "ldap://localhost:" + ((protocol != null && protocol.equals("ssl")) ? "636" : "389");
        if (protocol != null && protocol.equals("ssl")) {
            // We should use Start TLS extension?
            providerURL = "ldaps://localhost:636";
        } else {
            providerURL = "ldap://localhost:389";
        }
    }

    env.setProperty(Context.PROVIDER_URL, providerURL);
    env.setProperty(Context.SECURITY_PRINCIPAL, securityPrincipal);
    env.put(Context.SECURITY_CREDENTIALS, securityCredential);

    // always follow referrals transparently
    env.put(Context.REFERRAL, "follow");

    // Logon into LDAP server
    if (logger.isDebugEnabled())
        logger.debug("Logging into LDAP server, env=" + env);

    InitialLdapContext ctx = new InitialLdapContext(env, null);

    if (logger.isDebugEnabled())
        logger.debug("Logged into LDAP server, " + ctx);

    return ctx;
}

From source file:nl.nn.adapterframework.ldap.LdapSender.java

/**
 * Retrieves the DirContext from the JNDI environment and sets the <code>providerURL</code> back to <code>ldapProviderURL</code> if specified.
 * @throws ParameterException /*from   ww w.ja  v  a  2 s .  c o  m*/
 * 
 */
protected synchronized DirContext loopkupDirContext(Map paramValueMap)
        throws NamingException, ParameterException {
    DirContext dirContext;
    if (jndiEnv == null) {
        Hashtable newJndiEnv = getJndiEnv();
        //newJndiEnv.put("com.sun.jndi.ldap.trace.ber", System.err);//ldap response in log for debug purposes
        if (getLdapProviderURL() != null) {
            //Overwriting the (realm)providerURL if specified in configuration
            newJndiEnv.put("java.naming.provider.url", getLdapProviderURL());
        }
        if (principalParameterFound) {
            newJndiEnv.put(Context.SECURITY_PRINCIPAL, paramValueMap.get("principal"));
            newJndiEnv.put(Context.SECURITY_CREDENTIALS, paramValueMap.get("credentials"));
        }
        if (isUsePooling()) {
            // Enable connection pooling
            newJndiEnv.put("com.sun.jndi.ldap.connect.pool", "true");
            //see http://java.sun.com/products/jndi/tutorial/ldap/connect/config.html 
            //            newJndiEnv.put("com.sun.jndi.ldap.connect.pool.maxsize", "20" );
            //            newJndiEnv.put("com.sun.jndi.ldap.connect.pool.prefsize", "10" );
            //            newJndiEnv.put("com.sun.jndi.ldap.connect.pool.timeout", "300000" );
        } else {
            // Disable connection pooling
            newJndiEnv.put("com.sun.jndi.ldap.connect.pool", "false");
        }
        if (log.isDebugEnabled())
            log.debug("created environment for LDAP provider URL [" + newJndiEnv.get("java.naming.provider.url")
                    + "]");
        dirContext = (DirContext) new InitialDirContext(newJndiEnv);
        if (!principalParameterFound) {
            jndiEnv = newJndiEnv;
        }
    } else {
        dirContext = (DirContext) new InitialDirContext(jndiEnv);
    }
    return dirContext;
    //      return (DirContext) dirContextTemplate.lookup("");    // return copy to be thread-safe
}

From source file:org.akaza.openclinica.controller.SystemController.java

public HashMap<String, Object> getLdapModule(StudyBean studyBean) {
    String enabled = CoreResources.getField("ldap.enabled");
    String ldapHost = CoreResources.getField("ldap.host");
    String username = CoreResources.getField("ldap.userDn");
    String password = CoreResources.getField("ldap.password");

    String result = "";
    Properties env = new Properties();

    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, ldapHost);
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, username); // replace with user DN
    env.put(Context.SECURITY_CREDENTIALS, password);

    DirContext ctx = null;/* w  w w  .j av  a2 s . c om*/
    try {
        ctx = new InitialDirContext(env);
        result = "ACTIVE";
    } catch (Exception e) {
        result = "INACTIVE";
    }

    HashMap<String, String> mapMetadata = new HashMap<>();
    mapMetadata.put("ldap.host", ldapHost);

    HashMap<String, Object> mapWebService = new HashMap<>();
    mapWebService.put("enabled", enabled.equalsIgnoreCase("true") ? "True" : "False");
    mapWebService.put("status", result);
    mapWebService.put("metadata", mapMetadata);

    HashMap<String, Object> mapModule = new HashMap<>();
    mapModule.put("Ldap", mapWebService);

    return mapModule;
}