Example usage for javax.naming Context SECURITY_CREDENTIALS

List of usage examples for javax.naming Context SECURITY_CREDENTIALS

Introduction

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

Prototype

String SECURITY_CREDENTIALS

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

Click Source Link

Document

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

Usage

From source file:com.marklogic.samplestack.integration.web.LDAPIT.java

@Before
public void setup() throws NamingException {
    env = new Hashtable<String, Object>();
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    if (ldapUsername != null) {
        env.put(Context.SECURITY_PRINCIPAL, ldapUsername);
    }/*from  w w  w. j  a va  2s .  c om*/
    if (ldapPassword != null) {
        env.put(Context.SECURITY_CREDENTIALS, ldapPassword);
    }
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, ldapServer);

    // ensures that objectSID attribute values
    // will be returned as a byte[] instead of a String
    // env.put("java.naming.ldap.attributes.binary", "uid");

    // the following is helpful in debugging errors
    //env.put("com.sun.jndi.ldap.trace.ber", System.err);

    ctx = new InitialLdapContext(env, null);

}

From source file:es.udl.asic.user.OpenLdapDirectoryProvider.java

public void init() {
    try {/* w w w  .j av a 2  s  .c o m*/
        M_log.info("init()");
    } catch (Throwable t) {
        M_log.warn("init(): ", t);
    }

    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, getLdapHost() + ":" + getLdapPort());
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_CREDENTIALS, "secret");
}

From source file:org.aludratest.service.jms.impl.JmsServiceImpl.java

@Override
public void configure(Preferences preferences) {
    ValidatingPreferencesWrapper prefs = new ValidatingPreferencesWrapper(preferences);
    providerUrl = prefs.getRequiredStringValue("providerUrl");

    String initialContextFactory = prefs.getRequiredStringValue("initialContextFactory");
    String connectionFactoryName = prefs.getRequiredStringValue("connectionFactoryJndiName");

    String userName = prefs.getStringValue("jmsUser");
    String password = prefs.getStringValue("jmsPassword");

    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory);
    env.put(Context.PROVIDER_URL, providerUrl);
    if (!StringUtils.isEmpty(userName)) {
        env.put(Context.SECURITY_PRINCIPAL, userName);
    }/*  ww  w  .java 2s .  com*/
    if (!StringUtils.isEmpty(password)) {
        env.put(Context.SECURITY_CREDENTIALS, password);
    }

    try {
        initialContext = new InitialContext(env);
        connectionFactory = (ConnectionFactory) initialContext.lookup(connectionFactoryName);
        if (connectionFactory == null) {
            throw new ConfigurationException("The connection factory could not be found.");
        }

    } catch (NamingException e) {
        throw new TechnicalException("Could not retrieve objects from JNDI context", e);
    }

    action = new JmsActionImpl(connectionFactory, initialContext, userName, password);
}

From source file:org.web4thejob.security.ADAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {

    if (authentication.getName() == null || (String) authentication.getCredentials() == null) {
        throw new BadCredentialsException("");
    }//from   w w w  . j  a  v  a2 s. co  m

    String principal = getPrincipal(authentication.getName());
    String passwd = (String) authentication.getCredentials();

    LdapContext ctx = null;
    try {
        Hashtable<String, Object> env = new Hashtable<String, Object>();
        env.put(Context.INITIAL_CONTEXT_FACTORY, LdapCtxFactory.class.getCanonicalName());
        env.put(Context.SECURITY_AUTHENTICATION, "Simple");
        env.put(Context.SECURITY_PRINCIPAL, principal);
        env.put(Context.SECURITY_CREDENTIALS, passwd);
        env.put(Context.PROVIDER_URL, url);
        ctx = new InitialLdapContext(env, null);
        //LDAP Connection Successful

        UserDetails userDetails = userDetailsService.loadUserByUsername(principal);
        return new UsernamePasswordAuthenticationToken(userDetails, "", userDetails.getAuthorities());
    } catch (NamingException nex) {
        throw new BadCredentialsException("LDAP authentication failed.", nex);
    } catch (UsernameNotFoundException e) {
        throw new BadCredentialsException("UserDetails did not find a valid user for name: " + principal, e);
    } finally {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (Exception ignore) {
            }
        }
    }
}

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 {/*from  w w  w. j a  va2 s  . c om*/
        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:py.una.pol.karaku.util.LDAPUtil.java

private DirContext createInitialDirContext() {

    Map<Object, String> env = new HashMap<Object, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, propertiesUtil.get(LDAP_SERVER_KEY) + "/" + propertiesUtil.get(LDAP_DN_KEY));
    env.put(Context.SECURITY_PRINCIPAL, propertiesUtil.get(LDAP_ADMIN_KEY));
    env.put(Context.SECURITY_CREDENTIALS, propertiesUtil.get(LDAP_ADMIN_PASS_KEY));

    try {/*from w  w w  . j  a  v  a2s  .  c o  m*/
        return new InitialDirContext(new Hashtable<Object, String>(env));

    } catch (NamingException e) {
        throw new KarakuRuntimeException(e.getMessage(), e);
    }

}

From source file:org.projectforge.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 a v  a 2  s. co  m*/
    }
    if (ldapConfig != null && StringUtils.isNotBlank(ldapConfig.getSslCertificateFile()) == true) {
        env.put("java.naming.ldap.factory.socket", "org.projectforge.ldap.MySSLSocketFactory");
    }
    log.info("Trying to connect the LDAP server: url=[" + ldapConfig.getCompleteServerUrl()
            + "], authentication=[" + ldapConfig.getAuthentication() + "], principal=[" + user + "]");
    return env;
}

From source file:org.jboss.test.security.test.SubjectContextUnitTestCase.java

/**
 Access an unchecked method with a valid login that calls the same method
 on another bean using a run-as role.//  ww  w.  j  a v  a2s.co m
        
 @throws Exception
 */
public void testPublicMethod() throws Exception {
    log.debug("+++ testPublicMethod()");
    Properties env = new Properties();
    env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.security.jndi.JndiLoginInitialContextFactory");
    env.setProperty(Context.SECURITY_PRINCIPAL, "jduke");
    env.setProperty(Context.SECURITY_CREDENTIALS, "theduke");
    InitialContext ctx = new InitialContext(env);
    Object obj = ctx.lookup("jacc/Secured");
    obj = PortableRemoteObject.narrow(obj, SecuredServiceRemoteHome.class);
    SecuredServiceRemoteHome home = (SecuredServiceRemoteHome) obj;
    log.debug("Found SecuredServiceRemoteHome");
    SecuredServiceRemote bean = home.create();
    log.debug("Created SecuredServiceRemote");

    Principal callerIdentity = new SimplePrincipal("jduke");
    Principal runAsIdentity = new SimplePrincipal("runAsUser");
    HashSet expectedCallerRoles = new HashSet();
    expectedCallerRoles.add("groupMemberCaller");
    expectedCallerRoles.add("userCaller");
    expectedCallerRoles.add("allAuthCaller");
    expectedCallerRoles.add("webUser");
    HashSet expectedRunAsRoles = new HashSet();
    expectedRunAsRoles.add("identitySubstitutionCaller");
    expectedRunAsRoles.add("extraRunAsRole");
    CallerInfo info = new CallerInfo(callerIdentity, runAsIdentity, expectedCallerRoles, expectedRunAsRoles);
    bean.publicMethod(info);
    bean.remove();
}

From source file:org.eclipselabs.etrack.util.security.ldap.impl.LdapSecurityService.java

void activate(Map<?, ?> configuration) throws NamingException {
    this.url = (String) configuration.get(CONFIG_URL);
    this.baseDN = (String) configuration.get(CONFIG_BASE_DN);
    String managerDN = (String) configuration.get(CONFIG_MANAGER_DN);
    String managerPassword = (String) configuration.get(CONFIG_MANAGER_PASSWORD);

    Hashtable<String, String> environment = new Hashtable<String, String>();
    environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    environment.put(Context.PROVIDER_URL, url);

    if (managerDN != null) {
        environment.put(Context.SECURITY_AUTHENTICATION, "simple");
        environment.put(Context.SECURITY_PRINCIPAL, managerDN);
        environment.put(Context.SECURITY_CREDENTIALS, managerPassword);
    } else//ww  w .  j  a  va 2 s . c  o m
        environment.put(Context.SECURITY_AUTHENTICATION, "none");

    searchContext = new InitialDirContext(environment);
}

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);
        }/* w  w w. j  av  a2 s. c om*/
    }
    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;
}