设置ldap securityprincipal值

mhd8tkvw  于 2021-07-09  发布在  Java
关注(0)|答案(2)|浏览(335)

我能够在openldap命令行工具上成功运行以下搜索查询:

ldapsearch -h 1.11.1.1 -b "DC=ff2,DC=in" -s subtree -D "CN=Ldap Bind,OU=Service Accounts,OU=BA,DC=ff2,DC=in" -w G00Pass# sBAAccountName=testAccount

现在我必须在java类中执行它。我做了以下工作:
hashtable env=新hashtable();

env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.PROVIDER_URL, "ldap://1.11.1.1:389");
    env.put(Context.SECURITY_PRINCIPAL, "CN=Ldap Bind,OU=Service Accounts,OU=TECH,DC=ff2,DC=in");
    env.put(Context.SECURITY_CREDENTIALS, "H00Pass#");

    LdapContext context = new InitialLdapContext(env, null);
    // To get only 1000 results at a time.
    context.setRequestControls(
        new Control[]{new PagedResultsControl(1000, Control.CRITICAL)});

    String[] attrs={"CN=Ldap Bind,OU=Service Accounts,OU=TECH,DC=ff2,DC=in"};

    String base = "DC=ff2,DC=in";
    String filter = "(&(objectClass=user)(sAMAccountName=testAccount))";
    SearchControls controls = new SearchControls();
    controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    controls.setReturningAttributes(attrs);
    SearchResult searchResults;
        NamingEnumeration<SearchResult> results =  context.search(base, filter, controls);
        if (results.hasMoreElements()) {
            SearchResult searchResult = (SearchResult) results.nextElement();
            if(results.hasMoreElements()){
                System.err.println("Matched multiple groups for the group with SID: ");
        }else{

            System.out.println( (String)searchResult.getAttributes().get("sAMAccountName").get());
        }
       }

这是给我在空指针异常 searchResult.getAttributes() . 在这里我不知道如何包括 sBAAccountName 过滤器?

uqcuzwp8

uqcuzwp81#

您必须按照以下条件进行搜索:

env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.PROVIDER_URL, "<LDAP HOST>");
env.put(Context.SECURITY_PRINCIPAL, "<LDAP USER LOGIN>");
env.put(Context.SECURITY_CREDENTIALS, "<LDAP USER PASSWORD>");

LdapContext context = new InitialLdapContext(env);
// To get only 1000 results at a time.
context.setRequestControls(
    new Control[]{new PagedResultsControl(1000, Control.CRITICAL))});

String attrs = "<List of attrs to be retrieved for each matching LDAP entry>";
String base = "<Base of the search tree>";
String filter = "<Your filter>";
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
controls.setReturningAttributes(attrs);
SearchResults searchResults;
do {
    searchResults = ctx.search(base, filter, controls);
    while (searchResults.hasMoreElements()) {
        // Process result.
    }
    // Process response controls to get the cookie 
    // and keep searching until it is null.
}
while (cookie is not null);
5w9g7ksd

5w9g7ksd2#

你一定申报了 searchResult 作为成员变量。把那个拿走。然后,您将通过一个编译错误发现,您使用它的位置甚至没有声明,因此没有任何值。把它也拿走。

相关问题