java - 改进 LDAP 的 Java 代码

标签 java ldap jndi openldap

我找到了用于 LDAP 连接的 Java 代码。

package javaapplication2;

import java.util.Properties;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

public class SearchLDAP {

    public static void main(String[] args) {
        // The search base is the level in the hierarchy
        // that our search will start at. Here was use ""
        // which indicates the very root of the directory.
        String base = "";
        // LDAP filters are sort of like a WHERE clause. It
        // is constructed in a standard way based on LDAP
        // standards. The search here is a simple one that
        // says to return any entry with an objectclass value.
        // Since all entries must contain an objectclass, all
        // entries will be returned.
        String filter = "(objectclass=*)";
        // Here we set some connection properties for JNDI.
        Properties env = new Properties();
        // The Sun provider is the most widely used JNDI
        // provider and comes with Java 1.3+
        env.put(DirContext.INITIAL_CONTEXT_FACTORY,
                "com.sun.jndi.ldap.LdapCtxFactory");
        // The provider URL is an LDAP URL that tells JNDI
        // where it will need to connect to.
        env.put(DirContext.PROVIDER_URL, "ldap://localhost:389");
        try {
            // Here we create a DirContext object using
            // the environment we setup above. This
            // object will be used to communicate with
            // the server.
            DirContext dc = new InitialDirContext(env);
            // Above we mentioned the filter and base.
            // Another important part of the search criteria
            // is the scope. There are three scopes: base (this
            // entry only), onelevel (the direct children of this
            // entry), and subtree (this entry and all of its
            // decendents in the tree). In JNDI, OBJECT_SCOPE
            // indicates a base search.
            SearchControls sc = new SearchControls();
            sc.setSearchScope(SearchControls.OBJECT_SCOPE);
            NamingEnumeration ne = null;
            // Here we actually perform the search.
            ne = dc.search(base, filter, sc);
            // We cycle through the NamingEnumeration
            // that is returned by the search.
            while (ne.hasMore()) {
                // Retrieve the result as a SearchResult
                // and print it (not very pretty). There are
                // methods for extracting the attributes and
                // values without printing, as well.
                SearchResult sr = (SearchResult) ne.next();
                System.out.println(sr.toString() + "\n");
            }
            // Here we unbind from the LDAP server.
            dc.close();
        } catch (NamingException nex) {
            // A number of exceptions are subclassed from
            // NamingException. In a real application you'd
            // probably want to handle many of them
            // differently.
            System.err.println("Error: " + nex.getMessage());
        }
    }
}

你能帮我改进这段代码吗?我可以通过一个连接将连接池用于多个搜索请求吗?还有是否有任何标准技术可以提高 LDAP 搜索性能?我可以打开与 LDAP 服务器的无限连接并保持打开状态吗?

最佳答案

Can you help me how I can improve this code?

您没有关闭 NamingEnumeration。finally block 中将其关闭以确保其关闭。关闭finally block 中的Context 以确保其已关闭。遗憾的是,这些类没有实现 AutoCloseable,因此您无法使用 try()。

Can I use connection pool for many search requests using one connection?

是的。 JNDI LDAP 提供程序可以为您做到这一点。只需将系统属性 com.sun.jndi.ldap.connect.pool 设置为 true 即可。有关联的属性:请参阅 JNDI LDAP 提供程序文档。

And also is there any standard technique to improve LDAP search performance?

确保您要搜索的属性已在 LDAP 服务器上建立索引。

Can I open a infinite connection to the LDAP server and keep it open?

这不是一个好主意。最好使用连接池。见上文。

关于java - 改进 LDAP 的 Java 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21032415/

相关文章:

java - 在 JPanel [][] 表上使用 mouseListener 单击

linux - 使用 Active Directory 的 Subversion 服务器

deployment - 跨多个 Tomcat 实例维护 JNDI

java - Maven - 从多个模块构建一个基于 java/servlet 的 war 文件

java - 在 Java 中使用 for-each 循环时,对象变量是否会创建对象的实例?

java - 如何修复条目的 objectClasses 中未声明的属性 sn

java - 将 jboss 迁移到 tomcat - javax.naming.NamingException

java - 如何将 log4j2 JMSAppender 与 ActiveMQ 结合使用

java - 在 Android 游戏中使用 Surface View 时,我应该如何接收文本输入

node.js - 在权限管理方面,Active Directory 是否仍能与 Node.js 模块相媲美?