加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 03:06:51 所属栏目:Java 来源:网络整理
导读:我有一个ldap服务器,我用它来验证tomcat Web应用程序中的用户.我正在使用JNDIRealm,它在上下文文件中配置,这很好用. 我还需要在ldap中搜索用户信息.我已经想出了如何使用“jndi方法”做到这一点,并且我通过使用哈希表创建自己的jndi上下文使其在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方法一起使用,我假设它应该能够配置为资源,对吧?我错过了什么?

解决方法

这个答案有点晚,但可能对其他用户有用.它基于 EJP’s answer.

以下解决方案在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的文档解释了how to add custom resource factories.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读