从tomcat的context.xml文件中将LDAP配置为JNDI资源

时间:2015-10-05 15:25:24

标签: java tomcat ldap jndi

我想通过在Tomcat的7 context.xml中配置为资源的LDAP JNDI进行ldap查找

context.xml中的资源如下:

<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"/>

我还在web.xml中提供了引用。请参考以下内容:

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

我有自定义工厂类&#39; LdapContextFactory&#39;它扩展了ObjectFactory。请参考以下内容:

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;
    }
}

但是当我运行它时,我收到以下错误。

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].

如何解决这个问题?知道我错过了什么吗?

0 个答案:

没有答案