如何关闭与Active Directory的LDAP连接

时间:2014-09-29 16:40:47

标签: java ldap jndi openldap

我有一个代码可以验证,与Active Directory交互并获取一些信息。但我只想确保关闭连接并将其返回池中。我该如何确保他们?

这是我的代码:

import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
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 LDAPExaminer {

    public static void main(String[] args) {
        LDAPExaminer ldapExaminer = new LDAPExaminer();
        // NOTE: replace theUserName below with the Active Directory/LDAP user whose attribites you want printed.
        ldapExaminer.printUserBasicAttributes("<strong>theUserName</strong>", ldapExaminer.getLdapContext());
    }

    public LdapContext getLdapContext() {
        LdapContext ctx = null;
        try {
            Hashtable env = new Hashtable();
            env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
            env.put(Context.SECURITY_AUTHENTICATION, "Simple");

            // NOTE: replace user@domain.com with a User that is present in your Active Directory/LDAP
            env.put(Context.SECURITY_PRINCIPAL, "<strong>user@domain.com</strong>");
            // NOTE: replace userpass with passwd of this user.
            env.put(Context.SECURITY_CREDENTIALS, "userpass");
            // NOTE: replace ADorLDAPHost with your Active Directory/LDAP Hostname or IP.
            env.put(Context.PROVIDER_URL, "ldap://ActiveDirOrLDAPHost:389");

            System.out.println("Attempting to Connect...");

            ctx = new InitialLdapContext(env, null);
            System.out.println("Connection Successful.");
        } catch (NamingException nex) {
            System.out.println("LDAP Connection: FAILED");
            nex.printStackTrace();
        }
        return ctx;
    }

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

            SearchControls constraints = new SearchControls();
            constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
            // NOTE: The attributes mentioned in array below are the ones that will be retrieved, you can add more.
            String[] attrIDs = { "distinguishedName", "sn", "givenname", "mail", "telephonenumber", "canonicalName",
                    "userAccountControl", "accountExpires" };
            constraints.setReturningAttributes(attrIDs);

            // NOTE: replace DC=domain,DC=com below with your domain info. It is essentially the Base Node for Search.
            NamingEnumeration answer = ctx.search("DC=YourDomain,DC=com", "sAMAccountName=" + username, constraints);

            if (answer.hasMore()) {
                Attributes attrs = ((SearchResult) answer.next()).getAttributes();
                System.out.println(attrs.get("distinguishedName"));
                System.out.println(attrs.get("givenname"));
                System.out.println(attrs.get("sn"));
                System.out.println(attrs.get("mail"));
                System.out.println(attrs.get("telephonenumber"));
                System.out.println(attrs.get("canonicalName"));
                System.out.println(attrs.get("userAccountControl"));
                System.out.println(attrs.get("accountExpires"));
            } else {
                throw new Exception("Invalid User");
            }

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

}

1 个答案:

答案 0 :(得分:0)

一些事情

1)可能重复Java LDAP graceful disconnect

2)尝试在适当的地方添加[在[至少一个捕获和主要]

ctx.close();

如果您要保留此功能,我会再写一个代码来包含你的'main'代码,包括close()。