2012-12-24 228 views
2
  1. 我在Java LDAP身份驗證(AD)中遇到問題。我不理解這個LDAP身份驗證如何發生

下面的代碼是,他們試圖讓InitialDirContext用的用戶名和密碼,LDAP服務器的所有細節,如果它基於拋出一個異常上的返回碼決定。LDAP身份驗證查詢

private void doSimpleAuthentication() { 
     try { 
      String hostURL = "ldap://" + hostName + ":" + port + "/"; 
      env.put(Context.PROVIDER_URL, hostURL); 
      env.put(Context.SECURITY_AUTHENTICATION, "simple"); 

      String principal = userName + "@" + domain; 

      // First connect to LDAP Server using Directory Manager credentials 

      DirContext ctx = connectToDirServer(principal, password); 
      returnCode = VALID_USER; 

      if (warningPeriod >= 0) { 
       String filter = "(sAMAccountName=" + userName + ")"; 
       SearchControls ctrl = new SearchControls(); 
       ctrl.setSearchScope(SearchControls.SUBTREE_SCOPE); 
       NamingEnumeration results = ctx.search(baseDn, filter, ctrl); 

       if (results != null && results.hasMoreElements()) { 
        SearchResult result = (SearchResult) results.next(); 
        String attPrincipal = (result).getName() + "," + baseDn; 

        if ((!isPwdNeverExpires(result)) && isPasswordNearingExpiry(ctx, attPrincipal)) { 
         returnCode = PASSWORD_NEARING_EXPIRY; 
        } 
       } 
      } 
      if (ctx != null) ctx.close(); 
     } catch (CommunicationException e) { 
      errorMessage = e.getMessage(); 
      errorStackTrace = AgsUtil.convertToString(e); 
      returnCode = SERVER_NOT_AVAILABLE; 
     } catch (Exception e) { 
      errorMessage = e.getMessage(); 
      errorStackTrace = AgsUtil.convertToString(e); 
      returnCode = UNKNOWN_ERROR; 
      if (errorMessage.indexOf("525") != -1 || errorMessage.indexOf("successful bind must be completed") != -1) { 
       returnCode = USER_NOT_FOUND; 
      } else if (errorMessage.indexOf("52e") != -1) { 
       returnCode = INVALID_PASSWORD; 
      } else if (errorMessage.indexOf("701") != -1 || errorMessage.indexOf("532") != -1 || errorMessage.indexOf("773") != -1) { 
       returnCode = PASSWORD_EXPIRED; 
      } else if (errorMessage.indexOf("533") != -1) { 
       returnCode = USER_DISABLED; 
      } else if (errorMessage.indexOf("775") != -1) { 
       returnCode = USER_LOCKOUT; 
      } else if (errorMessage.indexOf("530") != -1) { 
       returnCode = LOGON_DENIED_AT_THIS_TIME; 
      } 
     } 
    } 
public DirContext connectToDirServer(String distinguishedName, String password) throws Exception { 
     env.put(Context.SECURITY_PRINCIPAL, distinguishedName); 
     env.put(Context.SECURITY_CREDENTIALS, password); 
     return new InitialDirContext(env); 
    } 

2.有哪些我在顧客的地方,在面對如果LDAP(AD)未找到用戶它返回的狀態代碼爲「INVALID_USERID_PASSWORD」而不是「USER_NOT_FOUND」的問題。客戶要求我提供以下信息

將查詢與應用程序對ldap服務器進行比較,或者以其他術語,請求字符串作爲ldap查詢傳遞的是什麼。 請參閱以下關於LDAP查詢的一些信息。

您可以參考這裏一個參考鏈接樣本..

http://www.google.com/support/enterprise/static/postini/docs/admin/en/dss_admin/prep_ldap.html

如何進行,我不覺得這些查詢是如何使用的任何信息。用上面的代碼。

回答

3

當BIND請求中的條目不存在時,LDAP目錄服務器可能會響應invalid password的結果代碼。它可以防止攻擊者知道是否存在條目。

+0

Hi Terry,在進一步調查問題時,我們發現客戶已將服務器從2003升級到2008 R2,這是唯一的改變,他們使用名爲'NetScalar'的工具在外部測試LDAP查詢,並且它仍然返回作爲USER_NOT_FOUND。當使用應用程序時,它將引發異常作爲INVALID USERID密碼。 – shashi27

1

您可能需要綁定爲擁有權限的人。

我們在LDAP Wiki上有樣品。