출처1 : https://stackoverflow.com/questions/8551809/how-to-connect-with-java-into-active-directory

출처2 : http://soft91.tistory.com/8




Active Directory 연동할 일이 생겨서 자바에서 AD 데이터를 가져오는 소스를 찾아보았다. 

searchBase 항목은 domain 항목을 나누어서 넣어주면 될 것 같다.



package AD;

 

import java.util.HashMap;

import java.util.Hashtable;

import java.util.Map;

 

import javax.naming.Context;

import javax.naming.NamingEnumeration;

import javax.naming.NamingException;

import javax.naming.directory.Attribute;

import javax.naming.directory.Attributes;

import javax.naming.directory.SearchControls;

import javax.naming.directory.SearchResult;

import javax.naming.ldap.InitialLdapContext;

import javax.naming.ldap.LdapContext;

 

public class ADTest {

 

    public static void main(String[] args) {

 

        String id = "userId";

        String pw = "passWord";

        String domain = "motolies.com";

        String server = "LDAP://127.0.0.1:389";

 

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

        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");

        env.put(Context.PROVIDER_URL, server);

        env.put(Context.SECURITY_AUTHENTICATION, "simple");

        env.put(Context.SECURITY_PRINCIPAL, id + "@" + domain);

        env.put(Context.SECURITY_CREDENTIALS, pw);

 

        LdapContext ctx;

 

        try {

            ctx = new InitialLdapContext(env, null);

 

            System.out.println("Connection Success!");

 

            String _filterName = "이름*";

            String returnedAtts[] = { "cn", "name", "sAMAccountName" };

           

            String searchBase = "dc=motolies,dc=com";

            String searchFilter = "(&(objectClass=user)(givenname=" + _filterName + "))";

 

            // Create the search controls

            SearchControls searchCtls = new SearchControls();

            // 가져올 항목을 필터링 하려면 여기

            searchCtls.setReturningAttributes(returnedAtts);

            // Specify the search scope

            searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);

 

            NamingEnumeration answer = ctx.search(searchBase, searchFilter, searchCtls);

            while (answer.hasMoreElements()) {

                SearchResult sr = (SearchResult) answer.next();

                Attributes attrs = sr.getAttributes();

                Map amap = null;

                if (attrs != null) {

                    amap = new HashMap();

                    NamingEnumeration ne = attrs.getAll();

                    while (ne.hasMore()) {

                        Attribute attr = (Attribute) ne.next();

                        amap.put(attr.getID(), attr.get());

                    }

                    ne.close();

                }

               

                amap.forEach((k, v) -> {

                    System.out.format("key : %s, value : %s%n", k, v);

                });

            }

        } catch (NamingException e) {

            // TODO Auto-generated catch block

            System.out.println("Connection Fail!");

            e.printStackTrace();

        }

    }

}

 



Posted by motolies

댓글을 달아 주세요