<small id='RzxpH'></small><noframes id='RzxpH'>

    <legend id='RzxpH'><style id='RzxpH'><dir id='RzxpH'><q id='RzxpH'></q></dir></style></legend>

    1. <i id='RzxpH'><tr id='RzxpH'><dt id='RzxpH'><q id='RzxpH'><span id='RzxpH'><b id='RzxpH'><form id='RzxpH'><ins id='RzxpH'></ins><ul id='RzxpH'></ul><sub id='RzxpH'></sub></form><legend id='RzxpH'></legend><bdo id='RzxpH'><pre id='RzxpH'><center id='RzxpH'></center></pre></bdo></b><th id='RzxpH'></th></span></q></dt></tr></i><div id='RzxpH'><tfoot id='RzxpH'></tfoot><dl id='RzxpH'><fieldset id='RzxpH'></fieldset></dl></div>
      • <bdo id='RzxpH'></bdo><ul id='RzxpH'></ul>
    2. <tfoot id='RzxpH'></tfoot>

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

      时间:2023-10-14
    3. <tfoot id='srsMu'></tfoot>

      <small id='srsMu'></small><noframes id='srsMu'>

          <bdo id='srsMu'></bdo><ul id='srsMu'></ul>

              • <i id='srsMu'><tr id='srsMu'><dt id='srsMu'><q id='srsMu'><span id='srsMu'><b id='srsMu'><form id='srsMu'><ins id='srsMu'></ins><ul id='srsMu'></ul><sub id='srsMu'></sub></form><legend id='srsMu'></legend><bdo id='srsMu'><pre id='srsMu'><center id='srsMu'></center></pre></bdo></b><th id='srsMu'></th></span></q></dt></tr></i><div id='srsMu'><tfoot id='srsMu'></tfoot><dl id='srsMu'><fieldset id='srsMu'></fieldset></dl></div>
                <legend id='srsMu'><style id='srsMu'><dir id='srsMu'><q id='srsMu'></q></dir></style></legend>

                  <tbody id='srsMu'></tbody>
              • 本文介绍了尝试在 Tomcat 中将 LDAP 配置为 JNDI 资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

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

                I have an ldap server that I'm using to authenticate users within a tomcat web application. I'm using the JNDIRealm and it's configured within a context file and this works great.

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

                I'll also need to search the ldap for user information. I've figured out how to do this with the "jndi method" and I have it working fine outside of tomcat by creating my own jndi context using a hashtable. However, instead of configuring the jndi properties in code, I'd like to create a JNDI Rsource in my context file right next to the Realm configuration.

                我想我会做这样的事情:

                I'm thinking I would do something like this:

                <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 告诉我无法创建资源,要么当我尝试使用以下内容对其进行初始化时:

                But either tomcat tells me the resource can't be created or when I try to initialize it with something like this:

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

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

                Tomcat tells me the "Cannot create resource instance". I've also added the correct resource-ref in my web.xml file, so I don't think that's the problem.

                由于 LDAP 与 JNDI 方法一起使用,我假设它应该能够配置为资源,对吧?我错过了什么?

                Since LDAP is being used with the JNDI method I'm assuming it should be able to be configured as a Resource, right? What am I missing?

                推荐答案

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

                This answer is a bit late, but probably it'll be useful for other users. It's based on EJP's answer.

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

                The following solution was tested on Apache Tomcat 7.
                If you need, you can replace LdapContext with DirContext.

                创建一个实现 ObjectFactory 实例化一个 LdapContext:

                Create a class which implements ObjectFactory to instantiate a 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 实例:

                Define your resource

                Add the following to your context.xml, referencing the factory and defining the values to create a LdapContext instance:

                <?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 上面创建的用于读取这些新属性/值.

                If you need to add more attributes/values to your resource, consider updating your ObjectFactory created above to read these new attributes/values.

                在你需要的地方注入你的资源:

                Inject your resource wherever you need:

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

                或者查一下:

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

                查看更多

                Apache Tomcat 的文档解释了如何添加自定义资源工厂.

                这篇关于尝试在 Tomcat 中将 LDAP 配置为 JNDI 资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何在 Java 中针对此 LDAP 进行 LDAP 搜索/身份验证 下一篇:Spring 安全切换到 Ldap 身份验证和数据库权限

                相关文章

              • <i id='cGbWZ'><tr id='cGbWZ'><dt id='cGbWZ'><q id='cGbWZ'><span id='cGbWZ'><b id='cGbWZ'><form id='cGbWZ'><ins id='cGbWZ'></ins><ul id='cGbWZ'></ul><sub id='cGbWZ'></sub></form><legend id='cGbWZ'></legend><bdo id='cGbWZ'><pre id='cGbWZ'><center id='cGbWZ'></center></pre></bdo></b><th id='cGbWZ'></th></span></q></dt></tr></i><div id='cGbWZ'><tfoot id='cGbWZ'></tfoot><dl id='cGbWZ'><fieldset id='cGbWZ'></fieldset></dl></div>
                <tfoot id='cGbWZ'></tfoot>

                <legend id='cGbWZ'><style id='cGbWZ'><dir id='cGbWZ'><q id='cGbWZ'></q></dir></style></legend>

              • <small id='cGbWZ'></small><noframes id='cGbWZ'>

                      <bdo id='cGbWZ'></bdo><ul id='cGbWZ'></ul>