2012-08-28 12 views
8

LDAPを使用してユーザーを認証できません。私は上記の詳細を使用してsAMAccountName値を検索することができますLDAP:接続の詳細でユーザーを認証する方法

URL=ldap://10.10.10.10:389 
LDAP BASE:DC=lab2,DC=ins 
LDAP Bind Account: CN=Ldap Bind,OU=Service Accounts,OU=TECH,DC=lab2,DC=ins 
LDAP Bind Account Pw: secret 

が、どのようにユーザー名とパスワードを使用してユーザーを認証するために:私は、次の詳細を持っていますか?
私の前の質問に従えば、LDAPサーバーに接続できても認証できないことが分かります。認証する
ユーザー:

user: someusername 
password: somepwd 

私は'somepwd'でLDAPサーバーに接続できないですし、どのように私はsomeusernameを使用する必要があります。指定されたユーザーをsAMAccountNameとして検索できます。

+1

正しいパスワードをお持ちですか?それはJXplorerのようなクライアントを使って動作しますか? – dbrin

+0

@DmitryB私は 'secret'でサーバに接続できますが、 'somepwd'では接続できません。 –

+0

よりもパスワードが間違っています: – dbrin

答えて

22

これは私がさまざまな場所で見つかったもののマッシュアップです。 UnboundID SDKを使用しない場合は、正しいパスに沿って配置する必要があります。これは実稼働品質ではありませんので、お店でサポートされている場合は、ここにSSLを追加することをお勧めします。役立つかもしれない

public static Boolean validateLogin(String userName, String userPassword) { 
    Hashtable<String, String> env = new Hashtable<String, String>(); 


    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); 
    env.put(Context.PROVIDER_URL, "ldap://" + LDAP_SERVER + ":" + LDAP_SERVER_PORT + "/" + LDAP_BASE_DN); 

    // To get rid of the PartialResultException when using Active Directory 
    env.put(Context.REFERRAL, "follow"); 

    // Needed for the Bind (User Authorized to Query the LDAP server) 
    env.put(Context.SECURITY_AUTHENTICATION, "simple"); 
    env.put(Context.SECURITY_PRINCIPAL, LDAP_BIND_DN); 
    env.put(Context.SECURITY_CREDENTIALS, LDAP_BIND_PASSWORD); 

    DirContext ctx; 
    try { 
     ctx = new InitialDirContext(env); 
    } catch (NamingException e) { 
     throw new RuntimeException(e); 
    } 

    NamingEnumeration<SearchResult> results = null; 

    try { 
     SearchControls controls = new SearchControls(); 
     controls.setSearchScope(SearchControls.SUBTREE_SCOPE); // Search Entire Subtree 
     controls.setCountLimit(1); //Sets the maximum number of entries to be returned as a result of the search 
     controls.setTimeLimit(5000); // Sets the time limit of these SearchControls in milliseconds 

     String searchString = "(&(objectCategory=user)(sAMAccountName=" + userName + "))"; 

     results = ctx.search("", searchString, controls); 

     if (results.hasMore()) { 

      SearchResult result = (SearchResult) results.next(); 
      Attributes attrs = result.getAttributes(); 
      Attribute dnAttr = attrs.get("distinguishedName"); 
      String dn = (String) dnAttr.get(); 

      // User Exists, Validate the Password 

      env.put(Context.SECURITY_PRINCIPAL, dn); 
      env.put(Context.SECURITY_CREDENTIALS, userPassword); 

      new InitialDirContext(env); // Exception will be thrown on Invalid case 
      return true; 
     } 
     else 
      return false; 

    } catch (AuthenticationException e) { // Invalid Login 

     return false; 
    } catch (NameNotFoundException e) { // The base context was not found. 

     return false; 
    } catch (SizeLimitExceededException e) { 
     throw new RuntimeException("LDAP Query Limit Exceeded, adjust the query to bring back less records", e); 
    } catch (NamingException e) { 
     throw new RuntimeException(e); 
    } finally { 

     if (results != null) { 
      try { results.close(); } catch (Exception e) { /* Do Nothing */ } 
     } 

     if (ctx != null) { 
      try { ctx.close(); } catch (Exception e) { /* Do Nothing */ } 
     } 
    } 
} 
+2

これはこれまで私が見てきた最良の解決策です。LDAPサーバーにバインドした後にユーザーを認証するために特に探していました。 – likeToCode

+0

これは接続プーリングで安全に使用できますか? – bdrx

7

LDAP接続は、anonymousとして開始されます。接続の許可状態を変更するには、BIND要求を使用します。 BIND要求には、 'simple'または 'SASL'という2つの形式があります。 'シンプルな' BINDリクエストには、識別名とパスワードが必要です。 BIND要求は、セキュア接続、または拡張要求を使用してセキュア接続に昇格された非セキュア接続を介して送信する必要があります。

UnboundID LDAP SDKを使用して:

// exception handling not shown 
LDAPConnection ldapConnection = new LDAPConnection(hostname,port); 
BindRequest bindRequest = new SimpleBindRequest(username,password); 
BindResult bindResult = ldapConnection.bind(bindRequest); 
if(bindResult.getResultCode().equals(ResultCode.SUCCESS)) { 
    /// successful authentication 
} 
ldapConnection.close(); 
+0

クライアントの制限により、UnboundID LDAP SDKを使用することはできません。それを達成する他の方法はありますか? –

関連する問題