gpt4 book ai didi

java - 改进 LDAP 的 Java 代码

转载 作者:行者123 更新时间:2023-11-30 07:11:34 26 4
gpt4 key购买 nike

我找到了这个用于 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com