1

I want to do ldap lookup through LDAP JNDI configured as a Resource in Tomcat's 7 context.xml

Resource in context.xml is given below:

<Resource name="ldap1" 
        auth="Container"
        singleton="false"   
        type="javax.naming.ldap.LdapContext" 
        factory="com.nitesh.common.ldap.LdapContextFactory"
        java.naming.factory.initial="com.sun.jndi.ldap.LdapCtxFactory"
        java.naming.provider.url="ldap://localhost:389"
        java.naming.security.authentication="simple"
        java.naming.security.credentials="credentials"
        java.naming.security.principal="username"/>

I have also provided the reference in web.xml. Please refer below:

<resource-ref>
    <res-ref-name>ldap1</res-ref-name>
    <res-type>javax.naming.ldap.LdapContext</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

I have custom factory class 'LdapContextFactory' which extends ObjectFactory. Please refer below:

public class LdapContextFactory implements ObjectFactory {

    private static final Logger LOGGER = Logger.getLogger(LdapContextFactory.class);

    @Override
    public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment)
            throws Exception {

        Hashtable<Object, Object> env = new Hashtable<Object, Object>();
        Reference reference = (Reference) obj;
        Enumeration<RefAddr> references = reference.getAll();

        while (references.hasMoreElements()) {

            RefAddr address = references.nextElement();
            String type = address.getType();
            String content = (String) address.getContent();

            switch (type) {

            case Context.INITIAL_CONTEXT_FACTORY:
                env.put(Context.INITIAL_CONTEXT_FACTORY, content);
                break;

            case Context.PROVIDER_URL:
                env.put(Context.PROVIDER_URL, content);
                break;

            case Context.SECURITY_AUTHENTICATION:
                env.put(Context.SECURITY_AUTHENTICATION, content);
                break;

            case Context.SECURITY_PRINCIPAL:
                env.put(Context.SECURITY_PRINCIPAL, content);
                break;

            case Context.SECURITY_CREDENTIALS:
                env.put(Context.SECURITY_CREDENTIALS, content);
                break;

            default:
                break;
            }
        }
        LdapContext context = new InitialLdapContext(env, null);
        return context;
    }
}

But When I run this, I get the following error.

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ldapLookup': Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name [ldap1] is not bound in this Context. Unable to find [ldap1].

How to resolve this? Any idea what I am missing?

nits
  • 43
  • 11

0 Answers0