Search in sources :

Example 26 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project cachecloud by sohutv.

the class LoginUtil method passportCheck.

public static boolean passportCheck(String username, String password) {
    //LDAP登陆地址
    String ldapUrl = ConstUtils.LDAP_URL;
    if (StringUtils.isBlank(ldapUrl)) {
        logger.warn("ldap url is empty!!");
        return true;
    }
    if (ConstUtils.IS_DEBUG) {
        logger.warn("isDebug=true return");
        return true;
    }
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put("java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory");
    env.put("java.naming.provider.url", ldapUrl);
    env.put("java.naming.security.authentication", "simple");
    env.put("java.naming.security.principal", username + ConstUtils.EMAIL_SUFFIX);
    env.put("java.naming.security.credentials", password);
    DirContext ctx = null;
    try {
        ctx = new InitialDirContext(env);
        if (ctx != null) {
            return true;
        }
    } catch (Exception e) {
        logger.error("username {} passportCheck: " + e.getMessage(), username, e);
    } finally {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }
    }
    return false;
}
Also used : Hashtable(java.util.Hashtable) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) InitialDirContext(javax.naming.directory.InitialDirContext)

Example 27 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project OpenAM by OpenRock.

the class Step4 method getLdapHostAndPort.

// Method to get hostname and port number with the
// provided Domain Name for Active Directory user data store.
private String[] getLdapHostAndPort(String domainName) throws NamingException, IOException {
    if (!domainName.endsWith(".")) {
        domainName += '.';
    }
    DirContext ictx = null;
    // The resource record type A is defined in RFC 1035. 
    try {
        Hashtable env = new Hashtable();
        env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
        ictx = new InitialDirContext(env);
        Attributes attributes = ictx.getAttributes(domainName, new String[] { "A" });
        Attribute attrib = attributes.get("A");
        if (attrib == null) {
            throw new NamingException();
        }
    } catch (NamingException e) {
        // throw exception.
        throw e;
    }
    // then look for the LDAP server
    String serverHostName = null;
    String serverPortStr = null;
    final String ldapServer = "_ldap._tcp." + domainName;
    try {
        // Attempting to resolve ldapServer to SRV record.
        // This is a mechanism defined in MSDN, querying 
        // SRV records for _ldap._tcp.DOMAINNAME.
        // and get host and port from domain.
        Attributes attributes = ictx.getAttributes(ldapServer, new String[] { "SRV" });
        Attribute attr = attributes.get("SRV");
        if (attr == null) {
            throw new NamingException();
        }
        String[] srv = attr.get().toString().split(" ");
        String hostNam = srv[3];
        serverHostName = hostNam.substring(0, hostNam.length() - 1);
        if ((serverHostName != null) && serverHostName.length() > 0) {
            getContext().setSessionAttribute(SessionAttributeNames.USER_STORE_HOST, serverHostName);
        }
        serverPortStr = srv[2];
    } catch (NamingException e) {
        // throw exception.
        throw e;
    }
    // try to connect to LDAP port to make sure this machine 
    // has LDAP service
    int serverPort = Integer.parseInt(serverPortStr);
    if ((serverPort > 0) && (serverPort < 65535)) {
        getContext().setSessionAttribute(SessionAttributeNames.USER_STORE_PORT, serverPortStr);
    }
    try {
        new Socket(serverHostName, serverPort).close();
    } catch (IOException e) {
        throw e;
    }
    String[] hostAndPort = new String[2];
    hostAndPort[0] = serverHostName;
    hostAndPort[1] = serverPortStr;
    return hostAndPort;
}
Also used : Attribute(javax.naming.directory.Attribute) Hashtable(java.util.Hashtable) Attributes(javax.naming.directory.Attributes) NamingException(javax.naming.NamingException) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) InitialDirContext(javax.naming.directory.InitialDirContext) IOException(java.io.IOException) Socket(java.net.Socket)

Example 28 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project wildfly by wildfly.

the class ExternalContextBindingTestCase method testWithActualLDAPContext.

private void testWithActualLDAPContext(boolean withCache) throws Exception {
    InitialContext ctx = null;
    InitialDirContext ldapContext1 = null;
    InitialDirContext ldapContext2 = null;
    try {
        ctx = new InitialContext();
        String initialDirContext = withCache ? "java:global/ldap-cache" : "java:global/ldap";
        LOGGER.debug("looking up " + initialDirContext + " ....");
        ldapContext1 = (InitialDirContext) ctx.lookup(initialDirContext);
        ldapContext2 = (InitialDirContext) ctx.lookup(initialDirContext);
        Assert.assertNotNull(ldapContext1);
        Assert.assertNotNull(ldapContext2);
        if (withCache) {
            Assert.assertSame(ldapContext1, ldapContext2);
        } else {
            Assert.assertNotSame(ldapContext1, ldapContext2);
        }
        LOGGER.debug("acquired external LDAP context: " + ldapContext1.toString());
        LdapCtx c = (LdapCtx) ldapContext1.lookup("dc=jboss,dc=org");
        c = (LdapCtx) c.lookup("ou=People");
        Attributes attributes = c.getAttributes("uid=jduke");
        Assert.assertTrue(attributes.get("description").contains("awesome"));
        // resource injection
        LookupEjb ejb = (LookupEjb) ctx.lookup("java:module/LookupEjb");
        Assert.assertNotNull(ejb);
        c = ejb.getLdapCtx();
        Assert.assertNotNull(c);
        c = (LdapCtx) c.lookup("ou=People");
        attributes = c.getAttributes("uid=jduke");
        Assert.assertTrue(attributes.get("description").contains("awesome"));
    } finally {
        if (ctx != null) {
            ctx.close();
        }
        if (ldapContext1 != null) {
            ldapContext1.close();
        }
        if (ldapContext2 != null) {
            ldapContext2.close();
        }
    }
}
Also used : Attributes(javax.naming.directory.Attributes) LdapCtx(com.sun.jndi.ldap.LdapCtx) InitialDirContext(javax.naming.directory.InitialDirContext) InitialContext(javax.naming.InitialContext)

Example 29 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project Activiti by Activiti.

the class LDAPConnectionUtil method createDirectoryContext.

public static InitialDirContext createDirectoryContext(LDAPConfigurator ldapConfigurator, String principal, String credentials) {
    Properties properties = new Properties();
    properties.put(Context.INITIAL_CONTEXT_FACTORY, ldapConfigurator.getInitialContextFactory());
    properties.put(Context.PROVIDER_URL, ldapConfigurator.getServer() + ":" + ldapConfigurator.getPort());
    properties.put(Context.SECURITY_AUTHENTICATION, ldapConfigurator.getSecurityAuthentication());
    properties.put(Context.SECURITY_PRINCIPAL, principal);
    properties.put(Context.SECURITY_CREDENTIALS, credentials);
    if (ldapConfigurator.getCustomConnectionParameters() != null) {
        for (String customParameter : ldapConfigurator.getCustomConnectionParameters().keySet()) {
            properties.put(customParameter, ldapConfigurator.getCustomConnectionParameters().get(customParameter));
        }
    }
    InitialDirContext context;
    try {
        context = new InitialDirContext(properties);
    } catch (NamingException e) {
        LOGGER.warn("Could not create InitialDirContext for LDAP connection : " + e.getMessage());
        throw new ActivitiException("Could not create InitialDirContext for LDAP connection : " + e.getMessage(), e);
    }
    return context;
}
Also used : ActivitiException(org.activiti.engine.ActivitiException) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext) Properties(java.util.Properties)

Example 30 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project Activiti by Activiti.

the class LDAPTemplate method execute.

public <T> T execute(LDAPCallBack<T> ldapCallBack) {
    InitialDirContext initialDirContext = null;
    try {
        initialDirContext = LDAPConnectionUtil.creatDirectoryContext(ldapConfigurator);
    } catch (Exception e) {
        LOGGER.info("Could not create LDAP connection : " + e.getMessage(), e);
    }
    T result = ldapCallBack.executeInContext(initialDirContext);
    LDAPConnectionUtil.closeDirectoryContext(initialDirContext);
    return result;
}
Also used : InitialDirContext(javax.naming.directory.InitialDirContext)

Aggregations

InitialDirContext (javax.naming.directory.InitialDirContext)37 NamingException (javax.naming.NamingException)18 DirContext (javax.naming.directory.DirContext)18 Hashtable (java.util.Hashtable)17 Attributes (javax.naming.directory.Attributes)9 IOException (java.io.IOException)8 Attribute (javax.naming.directory.Attribute)7 Properties (java.util.Properties)5 Socket (java.net.Socket)4 SearchResult (javax.naming.directory.SearchResult)4 UnknownHostException (java.net.UnknownHostException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 NamingEnumeration (javax.naming.NamingEnumeration)2 SearchControls (javax.naming.directory.SearchControls)2 InitialLdapContext (javax.naming.ldap.InitialLdapContext)2 LdapContext (javax.naming.ldap.LdapContext)2 JndiUtils.getInitialDirContext (com.facebook.presto.server.security.util.jndi.JndiUtils.getInitialDirContext)1 AuthenticationFailedException (com.google.gerrit.server.account.AuthenticationFailedException)1 LdapCtx (com.sun.jndi.ldap.LdapCtx)1