简体   繁体   中英

How can set New Security Credential for ldap Authentication in Java Code

Hashtable env = new Hashtable(11); create environment env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); 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 // Search and retrieve 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();
 }

Error : javax.naming.NameNotFoundException: when creating obj for context

Your Context.SECURITY_PRINCIPAL seems to be incorrect , it can't be an OU (Org Unit) but a CN , UID , etc. In your case it can be cn=121,ou=ousystem. You need to check it out.

If you have an LDAP Browser ,say Apache Directory Studio or Softera LDAP , try connecting to your LDAP server with the connection details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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