繁体   English   中英

尝试在Tomcat中将LDAP配置为JNDI资源

[英]Trying to configure LDAP as JNDI Resource in Tomcat

我有一个ldap服务器,我正在使用该服务器对tomcat Web应用程序中的用户进行身份验证。 我使用的是JNDIRealm,它在上下文文件中配置,因此效果很好。

我还需要在ldap中搜索用户信息。 我已经弄清楚了如何使用“ jndi方法”做到这一点,并且通过使用哈希表创建自己的jndi上下文,使其在tomcat之外可以正常工作。 但是,我不想在代码中配置jndi属性,而是想在Realm配置旁边的上下文文件中创建JNDI Rsource。

我想我会做这样的事情:

<Resource 
  name="ldap"
  auth="Container"
  type="com.sun.jndi.ldap.LdapCtxFactory"
  java.naming.factory.initial="com.sun.jndi.ldap.LdapCtxFactory"
  java.naming.provider.url="ldap://localhost:389"
  java.naming.security.authentication="simple"
  java.naming.security.principal="uid=rjcarr,dc=example"
  java.naming.security.credentials="abc123"
/>

但是要么tomcat告诉我无法创建资源,要么当我尝试使用以下方式初始化资源时:

Context initctx = new InitialContext();
DirContext ctx = (DirContext) initctx.lookup("java:comp/env/ldap");

Tomcat告诉我“无法创建资源实例”。 我还在我的web.xml文件中添加了正确的resource-ref,所以我认为这不是问题。

因为LDAP与JNDI方法一起使用,所以我假设它应该能够配置为Resource,对吗? 我想念什么?

这个答案有点晚,但是可能对其他用户有用。 它基于EJP的答案

以下解决方案已在Apache Tomcat 7上进行了测试。
如果需要,可以用DirContext替换LdapContext

创建一个ObjectFactory

创建一个实现ObjectFactory的类以实例化LdapContext

public class LdapContextFactory implements ObjectFactory {

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

定义您的资源

将以下内容添加到context.xml ,引用工厂并定义值以创建LdapContext实例:

<?xml version="1.0" encoding="UTF-8"?>
<Context>
    ...
    <Resource name="ldap/LdapResource" auth="Container"
        type="javax.naming.ldap.LdapContext"
        factory="com.company.LdapContextFactory"
        singleton="false" 
        java.naming.factory.initial="com.sun.jndi.ldap.LdapCtxFactory"
        java.naming.provider.url="ldap://127.0.0.1:389"
        java.naming.security.authentication="simple"
        java.naming.security.principal="username"
        java.naming.security.credentials="password" />
</Context>

如果需要向资源中添加更多属性/值,请考虑更新上面创建的ObjectFactory以读取这些新属性/值。

使用你的资源

在任何需要的地方注入资源:

@Resource(name = "ldap/LdapResource")
private LdapContext bean;

或查询一下:

Context initialContext = new InitialContext();
LdapContext ldapContext = (LdapContext)
    initialContext.lookup("java:comp/env/ldap/LdapResource");

看更多

Apache Tomcat的文档介绍了如何添加自定义资源工厂

你正在编造。 Tomcat资源的类型必须是实现javax.naming.spi.ObjectFactory的类。 有关自定义资源,请参见Tomcat文档。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM