我有一个简单的java应用程序正在执行通常的ldap操作。我看到LDAP的验证方法比LDAP慢10倍。
这是正常的吗?
是否有可能改善表现?
import java.util.*;
import javax.naming.*;
import javax.naming.directory.*;
import javax.naming.ldap.*;
class Ldap {
private LdapContext ctx;
private String url;
public Ldap(String url) {
this.url = url;
}
public void authenticate(String user, String pass) throws NamingException {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, url);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, user);
env.put(Context.SECURITY_CREDENTIALS, pass);
ctx = new InitialLdapContext(env, null);
}
}
public class App {
static DirContext ldapContext;
public static void main (String[] args) throws NamingException {
long before = System.currentTimeMillis();
Ldap ldap = new Ldap(args[0]);
for(int i = 0; i < 100; ++i) {
ldap.authenticate("username", "password");
}
long after = System.currentTimeMillis();
System.out.println("Time:" + (after-before) + " msec");
}
}
结果:
$ java -jar ldap.jar ldap://ldapserver.hq.k.grp:389
Time:739 msec
$ java -jar ldap.jar ldaps://ldapserver.hq.k.grp:636
Time:7546 msec
当我在我的应用程序中转储线程时,我可以看到它总是在同一个地方等待:
"main" #1 prio=5 os_prio=0 tid=0x00000000026be000 nid=0x20a0 in Object.wait() [0x0000000002ade000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000d6524678> (a com.sun.jndi.ldap.LdapRequest)
at java.lang.Object.wait(Unknown Source)
at com.sun.jndi.ldap.Connection.readReply(Unknown Source)
- locked <0x00000000d6524678> (a com.sun.jndi.ldap.LdapRequest)
at com.sun.jndi.ldap.LdapClient.ldapBind(Unknown Source)
- locked <0x00000000d64f9048> (a com.sun.jndi.ldap.LdapClient)
at com.sun.jndi.ldap.LdapClient.authenticate(Unknown Source)
- locked <0x00000000d64f9048> (a com.sun.jndi.ldap.LdapClient)
at com.sun.jndi.ldap.LdapCtx.connect(Unknown Source)
at com.sun.jndi.ldap.LdapCtx.<init>(Unknown Source)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(Unknown Source)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(Unknown Source)
at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(Unknown Source)
at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(Unknown Source)
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.init(Unknown Source)
at javax.naming.ldap.InitialLdapContext.<init>(Unknown Source)
at com.nagra.Ldap.authenticate(App.java:29)
at com.nagra.App.main(App.java:45)
其他LDAP操作(如get group in group)对于LDAPS来说要慢2倍。