java / / 2023. 3. 31. 07:58

java로 active directory에서 사용자 정보 조회

아래 정보(사용자, 도메인 등)를 변경하고 호출하면 된다.

public static void main(String[] args) throws NamingException {
        try {
            String userId = "hong";
            String domain = "example.com";
            String password = "비밀번호";
            String searchBase = "OU=company,DC=example,DC=com";
            String ldapUrl = "ldap://dc.example.com:389";

            // set properties for our connection and provider
            Properties properties = new Properties();
            properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
            properties.put(Context.PROVIDER_URL, ldapUrl);
            properties.put(Context.REFERRAL, "follow");
            properties.put(Context.SECURITY_AUTHENTICATION, "simple");
            properties.put(Context.SECURITY_PRINCIPAL, userId + "@" + domain);
            properties.put(Context.SECURITY_CREDENTIALS, password);

            InitialDirContext context = new InitialDirContext(properties);
            SearchControls searchControls = new SearchControls();
            searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
            //searchControls.setReturningAttributes(null);
            searchControls.setReturningAttributes(
                new String[] {"userPrincipalName", "distinguishedName", "mail", "displayName"}
            );
            String searchFilter = "sAMAccountName=" + userId;
            NamingEnumeration<SearchResult> answer = context.search(searchBase, searchFilter, searchControls);

            while (answer.hasMore()) {
                SearchResult sr = answer.next();
                System.out.println("-------------");
                Attributes attrs = sr.getAttributes();
                NamingEnumeration<String> ids = attrs.getIDs();
                while (ids.hasMore()) {
                    String id = ids.next();
                    Attribute idattr = attrs.get(id);
                    System.out.print("[" + id + "] ");
                    for (int ix = 0; ix < idattr.size(); ++ix) {
                        if (ix > 0) System.out.print(", ");
                        System.out.print(idattr.get(ix));
                    }
                    System.out.println();
                }
            }

            context.close();
        } catch (AuthenticationException e) {
            String msg = e.getMessage();
            if (msg.indexOf("data 525") > 0) {
                System.out.println("사용자를 찾을 수 없음.");
            } else if (msg.indexOf("data 773") > 0) {
                System.out.println("사용자는 암호를 재설정해야합니다.");
            } else if (msg.indexOf("data 52e") > 0) {
                System.out.println("ID와 비밀번호가 일치하지 않습니다.확인 후 다시 시도해 주십시오.");
            } else if (msg.indexOf("data 533") > 0) {
                System.out.println("입력한 ID는 비활성화 상태 입니다.");
            } else if(msg.indexOf("data 532") > 0){
                System.out.println("암호가 만료되었습니다.");
            } else if(msg.indexOf("data 701") > 0){
                System.out.println("AD에서 계정이 만료됨");
            } else {
                System.out.println("정상!");
            }
        }
    }
반응형
  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유