简体   繁体   中英

How to retrieve “windows active directory - attributes id's ” in java?

I have searched a lot to get the answer for my question. But I can't .

What I have got in search :

public class RetrieveUserAttributes {

    public static void main(String[] args) {
    RetrieveUserAttributes retrieveUserAttributes = new RetrieveUserAttributes();
    retrieveUserAttributes.getUserBasicAttributes("anand", retrieveUserAttributes.getLdapContext());
    }


    public LdapContext getLdapContext(){
    LdapContext ctx = null;
    try{
        Hashtable<String, String> env = new Hashtable<String, String>();
        env.put(Context.INITIAL_CONTEXT_FACTORY,
            "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.SECURITY_PRINCIPAL, "anand@tstdmn.com");
        env.put(Context.SECURITY_CREDENTIALS, "password@123");
        env.put(Context.PROVIDER_URL, "ldap://192.168.100.182:389");
        ctx = new InitialLdapContext(env, null);
        System.out.println("Connection Successful.");
    }catch(NamingException nex){
        System.out.println("LDAP Connection: FAILED");
    }
    return ctx;
    }

    private void getUserBasicAttributes(String username, LdapContext ctx) {
    try {

        SearchControls constraints = new SearchControls();
        constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
        String searchFilter = "(objectClass=person)";

        String[] attrIDs = { "distinguishedName","sn","givenname","mail", "telephonenumber","lockoutThreshold", "lockoutDuration", "minPwdAge","maxPwdAge", "minPwdLength","pwdLastSet"};
        constraints.setReturningAttributes(attrIDs);

        NamingEnumeration answer = ctx.search(searchBase, searchFilter, constraints);
        if (answer.hasMore()) {
        Attributes attrs = ((SearchResult) answer.next()).getAttributes();

        for(String obj : attrIDs){
            System.out.println(obj+" : "+ attrs.get(obj));
        }


        }else{
        throw new Exception("Invalid User");
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    }

}

The keys that I gave here is fully static .

I need to retrieve the all the attributes in the 'General , Account , Address' tabs dynamically in the 'AD' picture below.

在此处输入图片说明

Hope I will get a good solution.

We have identified the Attributes as they appear from LDAP: http://ldapwiki.willeke.com/wiki/MMC%20General%20Tab

-jim

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