0

I am new to LDAP. And I did the authentication part using LDAP INITIAL_CONTEXT_FACTORY ("com.sun.jndi.ldap.LdapCtxFactory"). Code of what I did is as follows.

    String url = ldap_url;
    String uname = request.getUsername();
    String pwd = request.getPassword();
    boolean authentication = false;
    boolean error = true;
    String msg, attributes, search;
    String ldapDn = String.format("%s%s", searchFilter,uname);

    // create env for initial context
    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, ldapDn);
    env.put(Context.SECURITY_CREDENTIALS, pwd);

    try {
        DirContext ctx = new InitialDirContext(env);
        authentication = true;
        error = false;

        ctx.close();

    } catch (Exception e) {

    } finally {

        if (!error) {
            msg = "Login success!!!";
        } else {
            msg = "Authentication failed!";
        }
    }

Now I need to fetch the "employee_id" which returns with the LDAP result. I went through few examples in the internet and I could not make it happen. It will be great if anyone can suggest me a better way to search this attribute.

Sajith Wijerathne
  • 119
  • 1
  • 3
  • 10
  • Reading this might help https://docs.spring.io/spring-ldap/docs/1.3.2.RELEASE/reference/html/dirobjectfactory.html – EricLavault Dec 10 '19 at 17:28
  • https://stackoverflow.com/questions/2172831/how-do-a-ldap-search-authenticate-against-this-ldap-in-java using this I was able to fix my issue. – Sajith Wijerathne Dec 13 '19 at 07:54

0 Answers0