如何在Java代码中为ldap身份验证设置New Security Credential

时间:2014-09-23 06:04:06

标签: java security authentication ldap credentials

Hashtable env = new Hashtable(11); create environment      env.put(Context.INITIAL_CONTEXT_FACTORY,              " com.sun.jndi.ldap.LdapCtxFactory&#34);      env.put(Context.PROVIDER_URL," ldap:// localhost:10389 / ou = system"); url Declaration      env.put(Context.SECURITY_CREDENTIALS," secret"); env.put(Context.SECURITY_AUTHENTICATION," simple");          env.put(Context.SECURITY_PRINCIPAL," ou = 121,ousystem"); For this is Domain          env.put(Context.SECURITY_CREDENTIALS," secret"); set new credential      //搜索和检索DN。

     DirContext ctx = null;
    NamingEnumeration results = null;
    try {
        ctx = new InitialDirContext(env);`cant create obj here`
        SearchControls controls = new SearchControls();
     // Authenticate
          ldap = new InitialLdapContext(env, null);
           } catch (AuthenticationException ae) {
     ae.printStackTrace();
      } catch (NamingException e) {
     e.printStackTrace();
 }

错误:javax.naming.NameNotFoundException:when creating obj for context

1 个答案:

答案 0 :(得分:2)

您的Context.SECURITY_PRINCIPAL似乎不正确,它不能是OU(组织单位),而是CN,UID等。在您的情况下,它可以是cn = 121,ou = ousystem。你需要检查一下。

如果您有LDAP浏览器,例如Apache Directory Studio或Softera LDAP,请尝试使用连接详细信息连接到LDAP服务器。