简体   繁体   中英

How to connect with Active Directory without a full CN/DN from Java code

I have implemented method:

public static LdapContext buildContext(String username, String password) {

    LdapContext context = null;

    Hashtable<String, String> env = new Hashtable<String, String>();

    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, username);
    env.put(Context.SECURITY_CREDENTIALS, password);
    env.put(Context.PROVIDER_URL, DOMAIN_URL);

    try {
        context = new InitialLdapContext(env, null);
    } catch (NamingException e) {

    }

    return context;
}

I do not know the full CN/DN string. I only pass the name of a user (fe Tom) and password.

I have no info about groups etc..

Thank you in advance!

Search for the entry using what information you have. The search result will contain the number of entries that matched the search and the DN of each entry that matched, therefore the search should be as restrictive as possible in order to return just the one entry for which you're looking. Then use that DN to BIND to the server,

What Terry said. We have an example of performing Basic JNDI Search with Administration Account

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