简体   繁体   English

JNDI LDAP在同一InitialDirContext对象中创建2个连接

[英]JNDI LDAP create 2 connection In the the same InitialDirContext object

I need to create 1 ldap connexion (I use a application account ) and from this connection i need to create other connexion (user connection) from check if the uid and password is ok. 我需要创建1个ldap connexion(我使用一个应用程序帐户),并且需要从此连接中通过检查uid和密码是否正常来创建其他connexion(用户连接)。

Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, ldapServerUrl);
env.put(Context.SECURITY_AUTHENTICATION, "none");

SearchControls searchCtrls = new SearchControls();
searchCtrls.setReturningAttributes(new String[] {});
searchCtrls.setSearchScope(SearchControls.SUBTREE_SCOPE);

String filter = "(&(cn=" + identifier + "))";

DirContext ctx = null;
ctx = new InitialDirContext(env);
NamingEnumeration<SearchResult> answer = ctx.search(
   ldapBaseDN, filter, searchCtrls);

String fullDN = null;
if (answer.hasMore()) {
    fullDN = answer.next().getNameInNamespace();

    ctx.close();
    ctx = null;

    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, fullDN);
    env.put(Context.SECURITY_CREDENTIALS, password);

    ctx = new InitialDirContext(env);
    // here I must create the user connection for check if the uid and password is good.

    return true;
}

Thank you. 谢谢。

After you find the user in the LDAP DIT you should then modify tne environment to contain the user's DN and password and then issue LdapContext.reconnect() using the same Context . 在LDAP DIT中找到用户之后,您应该修改环境以包含用户的DN和密码,然后使用相同的Context发出LdapContext.reconnect()

You don't need to create a separate 'physical' connection. 您无需创建单独的“物理”连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM