Using javax.naming, can I determine if I am connec

2019-09-09 16:55发布

Using only the javax.naming API, is there some metadata or other trick I can use to determine if I am in fact connected to an Active Directory server or some other type of directory server ?

1条回答
劳资没心,怎么记你
2楼-- · 2019-09-09 17:28

The root DSE may contain attributes that contain information about the directory server's software. However, the root DSE and/or the attributes may not be present or attributes may not be named the same in all directory server implementations. Nevertheless, you can query the DSE and see what it offers for the directory software your app will support. Here's an LDAP search to get the root DSE:

ldapsearch -h HOST -b " " -s base objectclass=*

This assumes that the DSE is associated with an objectclass. The vendor may have a proprietary method for providing the same.

There is this informational RFC 3045; it talks about storing vendor related information in the root DSE. Two attributes that may be populated by the directory server software are vendorname and vendorversion. You can check the existence of these in the root DSE returned by the server(s) you're working with.

Here's a crude Java code to pull those two attributes from the root DSE (using the LDAP provider, that is):

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;    
import javax.naming.directory.SearchResult;

public class RootDSE {
    public static void main(String[] args) throws Exception{
        Hashtable<String, String> jndiParms = new Hashtable<String, String>();

        jndiParms.put(Context.PROVIDER_URL, "ldap://my.ldap.server:389");
        jndiParms.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");

        DirContext ctx = new InitialDirContext(jndiParms);

        String searchBase = "";
        String searchFilter = "(objectclass=*)";

        SearchControls searchCtls = new SearchControls();
        searchCtls.setSearchScope(SearchControls.OBJECT_SCOPE);
        searchCtls.setReturningAttributes(new String[] { "vendorname", "vendorversion" } );

        NamingEnumeration<SearchResult> searchResults = 
            ctx.search(searchBase, searchFilter, searchCtls);

        if (searchResults.hasMore()) {
            SearchResult searchResult = (SearchResult)searchResults.next();
            System.out.println(searchResult.getAttributes());
        }
        else {
            System.err.println("No results");
        }
    }
}
查看更多
登录 后发表回答