Java 如何将附加搜索查询应用于LDAP

Java 如何将附加搜索查询应用于LDAP,java,active-directory,ldap,Java,Active Directory,Ldap,我在本地机器上有一个通过SSL的LDAP服务器,并从应用程序的同一台机器将其连接起来。以下是连接LDAP服务器的代码: // Specify the search scope final String returnedAtts[] = { ldapSettingModel.getDepartment(), ldapSettingModel.getEmailId(), ldapSettingModel.getLocation(), ldapS

我在本地机器上有一个通过SSL的LDAP服务器,并从应用程序的同一台机器将其连接起来。以下是连接LDAP服务器的代码:

// Specify the search scope
    final String returnedAtts[] = { ldapSettingModel.getDepartment(),
            ldapSettingModel.getEmailId(), ldapSettingModel.getLocation(),
            ldapSettingModel.getRole(), ldapSettingModel.getDispName() };
    String searchFilter = "(&(objectClass=*)(" + ldapSettingModel.getLoginId() + "=" + user
            + ")" + ldapSettingModel.getFilter() + ")";

    String adPrincipal = user;
    if (CATAppConstants.ACTIVE_DIRECTORY_WIN_2000_ATTR.equals(ldapSettingModel.getLoginId())) {

        // with domain
        if (user.indexOf("\\") > 0) {
            user = user.substring(user.indexOf("\\") + 1);
            searchFilter = "(&(objectClass=*)(" + ldapSettingModel.getLoginId() + "=" + user
                    + ")" + ldapSettingModel.getFilter() + ")";
        } else {
            String[] pDN = ldapSettingModel.getBaseDNName().split(",");
            for (String dn : pDN) {
                if (dn.indexOf("dc=") == 0) {
                    adPrincipal = dn.substring(3) + "\\" + adPrincipal;
                    break;
                }
            }
        }

    }

    // Create the search controls
    SearchControls searchCtls = new SearchControls();
    searchCtls.setReturningAttributes(returnedAtts);
    searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    String ldapProtocol = "ldap" + (ldapSettingModel.getSslFlg() == 1 ? "s" : "");
    String providerUrl = ldapProtocol + "://" + ldapSettingModel.getHost() + ":"
            + ldapSettingModel.getPortNo();
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, providerUrl);
    if (ldapSettingModel.getSslFlg() == 1) {
        env.put(Context.SECURITY_PROTOCOL, "ssl");
        env.put("java.naming.ldap.factory.socket",
                "com.shift.tcm.serviceCust.authenticationManagement.CustomSSLSocketFactory");
    }
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    switch (ldapSettingModel.getLdapType()) {
    case CATAppConstants.DIRECTORY_TYPE_IS_ACTIVE_DIRECTORY:
        env.put(Context.SECURITY_PRINCIPAL, adPrincipal);
        break;
    default:
        env.put(Context.SECURITY_PRINCIPAL, ldapSettingModel.getLoginId() + "=" + user + ","
                + ldapSettingModel.getBaseDNName());
        break;
    }

    env.put(Context.SECURITY_CREDENTIALS, requestPassword);
    env.put("com.sun.jndi.ldap.connect.timeout", CATAppConstants.LDAP_CONNECT_TIMEOUT + "");

    LdapContext ctxGC = null;
    MstUserModel userDetails = null;

    // This is the actual Authentication piece. Will throw
    // javax.naming.AuthenticationException
    // if the users password is not correct. Other exceptions may
    // include IO (server not found) etc.
    ctxGC = new InitialLdapContext(env, null);

    // Now try a simple search and get some attributes as defined in
    // returnedAtts
    NamingEnumeration<SearchResult> answer = ctxGC.search(ldapSettingModel.getBaseDNName(),
            searchFilter, searchCtls);

    userDetails = getUserDetailsFromLDAP(answer, ldapSettingModel, checkDetailsOnly,
            currentRoleName);
    userDetails.setLoginId(user);
//指定搜索范围
最后一个字符串returnedAtts[]={ldapSettingModel.getDepartment(),
ldapSettingModel.getEmailId(),ldapSettingModel.getLocation(),
ldapSettingModel.getRole(),ldapSettingModel.getDispName()};
String searchFilter=“(&(objectClass=*)(“+ldapSettingModel.getLoginId()+”=“+user
+“”“+ldapSettingModel.getFilter()+”;
字符串adPrincipal=用户;
if(CATAppConstants.ACTIVE\u DIRECTORY\u WIN\u 2000\u ATTR.equals(ldapSettingModel.getLoginId())){
//有域名
if(user.indexOf(“\\”)>0){
user=user.substring(user.indexOf(“\\”)+1);
searchFilter=“(&(objectClass=*)(“+ldapSettingModel.getLoginId()+”=“+user
+“”“+ldapSettingModel.getFilter()+”;
}否则{
字符串[]pDN=ldapSettingModel.GetBasedName().split(“,”);
用于(字符串dn:pDN){
如果(dn.indexOf(“dc=”)==0){
adPrincipal=dn.子串(3)+“\\”+adPrincipal;
打破
}
}
}
}
//创建搜索控件
SearchControls searchCtls=新的SearchControls();
searchCtls.设置ReturningAttributes(returnedAtts);
searchCtls.setSearchScope(SearchControls.SUBTREE_范围);
字符串ldapProtocol=“ldap”+(ldapSettingModel.getSslFlg()==1?“:”);
字符串提供程序URL=ldapProtocol+“:/”+ldapSettingModel.getHost()+“:”
+ldapSettingModel.getPortNo();
Hashtable env=新的Hashtable();
put(Context.INITIAL\u Context\u工厂,“com.sun.jndi.ldap.LdapCtxFactory”);
env.put(Context.PROVIDER\u URL,providerUrl);
if(ldapSettingModel.getSslFlg()==1){
环境保护协议(Context.SECURITY_PROTOCOL,ssl);
put(“java.naming.ldap.factory.socket”,
“com.shift.tcm.serviceCust.authenticationManagement.customsslssocketfactory”);
}
环境put(Context.SECURITY_认证,“simple”);
开关(ldapSettingModel.getLdapType()){
case CATAppConstants.DIRECTORY\u TYPE\u是\u ACTIVE\u目录:
环境保护局(环境安全局局长、副局长);
打破
违约:
env.put(Context.SECURITY_PRINCIPAL,ldapSettingModel.getLoginId()+“=”+user+“,”
+ldapSettingModel.getBasedName());
打破
}
env.put(Context.SECURITY_凭证、requestPassword);
put(“com.sun.jndi.ldap.connect.timeout”,CATAppConstants.ldap\u connect\u timeout+”);
LdapContext ctxGC=null;
MstUserModel userDetails=null;
//这是实际的身份验证部分。会扔
//javax.naming.AuthenticationException
//如果用户密码不正确。其他例外情况可能会发生
//包括IO(未找到服务器)等。
ctxGC=新的InitialLdapContext(env,null);
//现在尝试一个简单的搜索,并获得中定义的一些属性
//返回的数据
NamingEnumeration answer=ctxGC.search(ldapSettingModel.GetBasedName(),
searchFilter,searchCtls);
userDetails=getUserDetailsFromLDAP(答案、ldapSettingModel、checkDetailsOnly、,
罗勒烯胺);
userDetails.setLoginId(用户);
在我尝试使用
ldapSettingModel.getFilter()
添加搜索过滤器之前,一切正常。通常,如果没有它,搜索筛选器将包含以下LDAP服务器的
(&(objectClass=*)(uid=vaibhav))

// Specify the search scope
    final String returnedAtts[] = { ldapSettingModel.getDepartment(),
            ldapSettingModel.getEmailId(), ldapSettingModel.getLocation(),
            ldapSettingModel.getRole(), ldapSettingModel.getDispName() };
    String searchFilter = "(&(objectClass=*)(" + ldapSettingModel.getLoginId() + "=" + user
            + ")" + ldapSettingModel.getFilter() + ")";

    String adPrincipal = user;
    if (CATAppConstants.ACTIVE_DIRECTORY_WIN_2000_ATTR.equals(ldapSettingModel.getLoginId())) {

        // with domain
        if (user.indexOf("\\") > 0) {
            user = user.substring(user.indexOf("\\") + 1);
            searchFilter = "(&(objectClass=*)(" + ldapSettingModel.getLoginId() + "=" + user
                    + ")" + ldapSettingModel.getFilter() + ")";
        } else {
            String[] pDN = ldapSettingModel.getBaseDNName().split(",");
            for (String dn : pDN) {
                if (dn.indexOf("dc=") == 0) {
                    adPrincipal = dn.substring(3) + "\\" + adPrincipal;
                    break;
                }
            }
        }

    }

    // Create the search controls
    SearchControls searchCtls = new SearchControls();
    searchCtls.setReturningAttributes(returnedAtts);
    searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    String ldapProtocol = "ldap" + (ldapSettingModel.getSslFlg() == 1 ? "s" : "");
    String providerUrl = ldapProtocol + "://" + ldapSettingModel.getHost() + ":"
            + ldapSettingModel.getPortNo();
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, providerUrl);
    if (ldapSettingModel.getSslFlg() == 1) {
        env.put(Context.SECURITY_PROTOCOL, "ssl");
        env.put("java.naming.ldap.factory.socket",
                "com.shift.tcm.serviceCust.authenticationManagement.CustomSSLSocketFactory");
    }
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    switch (ldapSettingModel.getLdapType()) {
    case CATAppConstants.DIRECTORY_TYPE_IS_ACTIVE_DIRECTORY:
        env.put(Context.SECURITY_PRINCIPAL, adPrincipal);
        break;
    default:
        env.put(Context.SECURITY_PRINCIPAL, ldapSettingModel.getLoginId() + "=" + user + ","
                + ldapSettingModel.getBaseDNName());
        break;
    }

    env.put(Context.SECURITY_CREDENTIALS, requestPassword);
    env.put("com.sun.jndi.ldap.connect.timeout", CATAppConstants.LDAP_CONNECT_TIMEOUT + "");

    LdapContext ctxGC = null;
    MstUserModel userDetails = null;

    // This is the actual Authentication piece. Will throw
    // javax.naming.AuthenticationException
    // if the users password is not correct. Other exceptions may
    // include IO (server not found) etc.
    ctxGC = new InitialLdapContext(env, null);

    // Now try a simple search and get some attributes as defined in
    // returnedAtts
    NamingEnumeration<SearchResult> answer = ctxGC.search(ldapSettingModel.getBaseDNName(),
            searchFilter, searchCtls);

    userDetails = getUserDetailsFromLDAP(answer, ldapSettingModel, checkDetailsOnly,
            currentRoleName);
    userDetails.setLoginId(user);

我试图允许用户使用
ldapSettingModel.getFilter()
添加额外的搜索筛选器,我尝试使用以下值搜索:

  • (cn=India):最终的
    searchFilter
    ->(&(objectClass=*)(uid=vaibhav)(cn=India))
  • (objectClass=postalAddress)(cn=India):最终的
    searchFilter
    ->(&(objectClass=*)(uid=vaibhav)(objectClass=postalAddress)(cn=India))
  • 我不确定是否正确应用了附加的搜索筛选器,但是
    namingumeration答案
    从我的
    getUserDetailsFromLDAP()
    方法返回null。如果我做错了什么,请指导我,因为我对LDAP非常陌生

    (&(objectClass=*)(uid=vaibhav)(cn=India))
    
    这没有道理
    cn
    是“通用名称”的缩写。您没有通用名为“India”的用户

    应该是

    (&(objectClass=*)(uid=vaibhav)(postalAddress=India))
    
    事实上,当你在寻找用户时,它可能是

    (&(objectClass=inetOrgPerson)(uid=vaibhav)(postalAddress=India))
    

    您提到的第二个备选方案就更没有意义了。

    Ohh,感谢您提供的快速解决方案,实际上我想测试我为添加额外的searchFilter而编写的代码是否有效。但由于我对它的工作原理知之甚少,所以我随机尝试了一些东西。再次感谢您的解释。。。!