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:it.infn.ct.security.utilities.LDAPUtils.java

private static DirContext getContext() throws NamingException {
    ResourceBundle rb = ResourceBundle.getBundle("ldap");
    Properties env = new Properties();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, rb.getString("url"));
    env.put(Context.SECURITY_PRINCIPAL, rb.getString("rootDN"));
    env.put(Context.SECURITY_AUTHENTICATION, "none");
    return new InitialDirContext(env);
}

From source file:org.jasig.cas.adaptors.ldap.DigestMd5DirContextAuthenticationStrategy.java

/** {@inheritDoc} */
@SuppressWarnings(value = "unchecked")
public void setupEnvironment(final Hashtable env, final String userDn, final String password)
        throws NamingException {

    env.put(Context.SECURITY_AUTHENTICATION, DIGEST_MD5_AUTHENTICATION);
    // userDn should be a bare username for DIGEST-MD5
    env.put(Context.SECURITY_PRINCIPAL, userDn);
    env.put(Context.SECURITY_CREDENTIALS, password);

}

From source file:com.constellio.model.services.users.sync.FastBindConnectionControl.java

@SuppressWarnings("unchecked")
public LDAPFastBind(String ldapurl, Boolean followReferences, boolean activeDirectory) {
    env = new Hashtable();
    //This can make LDAP search slow : http://stackoverflow.com/questions/16412236/how-to-resolve-javax-naming-partialresultexception
    //env.put(Context.REFERRAL, "follow");
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.PROVIDER_URL, ldapurl);
    env.put("java.naming.ldap.attributes.binary", "tokenGroups objectSid");
    if (followReferences) {
        env.put(Context.REFERRAL, "follow");
    }//from   ww  w .  ja v  a2 s .  c  o m

    if (StringUtils.startsWith(ldapurl, "ldaps")) {
        //env.put(Context.SECURITY_PROTOCOL, "ssl");
        env.put("java.naming.ldap.factory.socket",
                "com.constellio.model.services.users.sync.ldaps.DummySSLSocketFactory");
    }

    if (activeDirectory) {
        connCtls = new Control[] { new FastBindConnectionControl() };
    } else {
        connCtls = new Control[] {};
    }

    //first time we initialize the context, no credentials are supplied
    //therefore it is an anonymous bind.      

    /*try {
       ctx = new InitialLdapContext(env, connCtls);
            
    } catch (NamingException e) {
       throw new RuntimeNamingException(e.getMessage());
    }*/
    //FIX de Vincent pour o a q
    try {
        ctx = new InitialLdapContext(env, connCtls);
    } catch (NamingException e) {
        if (activeDirectory) {
            connCtls = new Control[] {};
            try {
                ctx = new InitialLdapContext(env, connCtls);
            } catch (NamingException e2) {
                throw new RuntimeException(e);
            }
        } else {
            throw new RuntimeException(e);
        }
    }
}

From source file:br.com.upic.camel.ldap.LdapEndpoint.java

@Override
protected void onExchange(final Exchange exchange) throws Exception {
    LOG.info("Setting up the context");

    final Hashtable<String, String> conf = new Hashtable<String, String>();

    LOG.debug("Initial Context Factory = " + initialContextFactory);

    conf.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory);

    LOG.debug("Provider URL = " + providerUrl);

    conf.put(Context.PROVIDER_URL, providerUrl);

    LOG.debug("Security Authentication = " + securityAuthentication);

    conf.put(Context.SECURITY_AUTHENTICATION, securityAuthentication);

    final Message in = exchange.getIn();

    final String user = in.getHeader(HEADER_USER, String.class);

    LOG.debug("User = " + user);

    conf.put(Context.SECURITY_PRINCIPAL, user);

    final String password = in.getHeader(HEADER_PASSWORD, String.class);

    LOG.debug("Password = " + password);

    conf.put(Context.SECURITY_CREDENTIALS, password);

    LOG.info("Authenticating in directory");

    final Message out = exchange.getOut();

    try {//from  w w  w  .j av  a2 s . c o m
        new InitialContext(conf);

        out.setBody(true);
    } catch (final AuthenticationException e) {
        LOG.error(e.getMessage(), e);

        out.setBody(false);
    }

}

From source file:it.infn.ct.security.utilities.LDAPUtils.java

private static DirContext getAuthContext(String userCN, String password, boolean dedicatedAdminUser)
        throws NamingException {
    ResourceBundle rb = ResourceBundle.getBundle("ldap");
    Properties env = new Properties();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, rb.getString("url"));
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    if (dedicatedAdminUser) {
        env.put(Context.SECURITY_PRINCIPAL, rb.getString("bindDN"));
        env.put(Context.SECURITY_CREDENTIALS, rb.getString("bindPass"));
    } else {/*from  www .j a  v  a2  s .c o m*/
        env.put(Context.SECURITY_PRINCIPAL, "cn=" + userCN + "," + rb.getString("peopleRoot"));
        env.put(Context.SECURITY_CREDENTIALS, password);
    }

    return new InitialDirContext(env);

}

From source file:io.apiman.gateway.engine.policies.auth.LDAPIdentityValidator.java

/**
 * @see io.apiman.gateway.engine.policies.auth.IIdentityValidator#validate(java.lang.String, java.lang.String, io.apiman.gateway.engine.beans.ServiceRequest, io.apiman.gateway.engine.policy.IPolicyContext, java.lang.Object, io.apiman.gateway.engine.async.IAsyncHandler)
 *//*from w w w .java  2  s .co  m*/
@Override
public void validate(String username, String password, ServiceRequest request, IPolicyContext context,
        LDAPIdentitySource config, IAsyncResultHandler<Boolean> handler) {
    String url = config.getUrl();
    String dn = formatDn(config.getDnPattern(), username, request);

    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); //$NON-NLS-1$
    env.put(Context.PROVIDER_URL, url);

    env.put(Context.SECURITY_AUTHENTICATION, "simple"); //$NON-NLS-1$
    env.put(Context.SECURITY_PRINCIPAL, dn);
    env.put(Context.SECURITY_CREDENTIALS, password);
    try {
        new InitialDirContext(env);
        handler.handle(AsyncResultImpl.create(Boolean.TRUE));
    } catch (AuthenticationException e) {
        handler.handle(AsyncResultImpl.create(Boolean.FALSE));
    } catch (NamingException e) {
        throw new RuntimeException(e);
    }
}

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);
    }/*www  .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 {/*  ww  w  .j  a  v a 2s .  co 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.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 . ja v  a 2s  .c  om

    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:es.udl.asic.user.OpenLdapDirectoryProvider.java

public boolean authenticateUser(String userLogin, UserEdit edit, String password) {
    Hashtable env = new Hashtable();
    InitialDirContext ctx;//  w ww.  ja v a 2 s .  c  o m

    String INIT_CTX = "com.sun.jndi.ldap.LdapCtxFactory";
    String MY_HOST = getLdapHost() + ":" + getLdapPort();
    String cn;
    boolean returnVal = false;

    if (!password.equals("")) {

        env.put(Context.INITIAL_CONTEXT_FACTORY, INIT_CTX);
        env.put(Context.PROVIDER_URL, MY_HOST);
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_CREDENTIALS, "secret");

        String[] returnAttribute = { "ou" };
        SearchControls srchControls = new SearchControls();
        srchControls.setReturningAttributes(returnAttribute);
        srchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);

        String searchFilter = "(&(objectclass=person)(uid=" + escapeSearchFilterTerm(userLogin) + "))";

        try {
            ctx = new InitialDirContext(env);
            NamingEnumeration answer = ctx.search(getBasePath(), searchFilter, srchControls);
            String trobat = "false";

            while (answer.hasMore() && trobat.equals("false")) {

                SearchResult sr = (SearchResult) answer.next();
                String dn = sr.getName().toString() + "," + getBasePath();

                // Second binding
                Hashtable authEnv = new Hashtable();
                try {
                    authEnv.put(Context.INITIAL_CONTEXT_FACTORY, INIT_CTX);
                    authEnv.put(Context.PROVIDER_URL, MY_HOST);
                    authEnv.put(Context.SECURITY_AUTHENTICATION, "simple");
                    authEnv.put(Context.SECURITY_PRINCIPAL, sr.getName() + "," + getBasePath());
                    authEnv.put(Context.SECURITY_CREDENTIALS, password);
                    try {
                        DirContext authContext = new InitialDirContext(authEnv);
                        returnVal = true;
                        trobat = "true";
                        authContext.close();
                    } catch (AuthenticationException ae) {
                        M_log.info("Access forbidden");
                    }

                } catch (NamingException namEx) {
                    M_log.info("User doesn't exist");
                    returnVal = false;
                    namEx.printStackTrace();
                }
            }
            if (trobat.equals("false"))
                returnVal = false;

        } catch (NamingException namEx) {
            namEx.printStackTrace();
            returnVal = false;
        }
    }
    return returnVal;
}