2016-11-11 16 views
0

UPDATE 11/14:Active Directory UIを使用してログインするときにユーザー名とパスワードが有効であることを運用チームから確認しました。つまり、プログラムでは失敗しているだけです。LDAP ADは作成したユーザーを認証できません

私はシンプルなActive Directoryサーバーで作業しています。新しいユーザーを作成し、設定した資格情報を使用してそのユーザーを認証する機能をテストしています。

私は正常に既存のユーザー(サーバーの管理インターフェイス経由で作成)を認証できます。新しいユーザーを作成してパスワードを設定することができます(下記のコードをご覧ください)。私は私が提供された資格情報を使用して認証しようとすると、そのユーザーが正常に

しかし、私は以下

javax.naming.AuthenticationException: [LDAP: error code 49 - 80090308: LdapErr: DSID-0C0903A9, comment: AcceptSecurityContext error, data 52e, v1db0] 

を取得しています見つけることができ、ユーザここ

public UserEntity authenticate (@NotNull final String username, 
            @NotNull final String password) 
    throws NamingException 
{ 
    try 
    { 
     Hashtable<String, Object> env = new Hashtable<>(); 

     env.put(Context.SECURITY_AUTHENTICATION,"simple"); 
     env.put(Context.SECURITY_PRINCIPAL,  username); 
     env.put(Context.SECURITY_CREDENTIALS, password); 
     env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); 
     env.put(Context.PROVIDER_URL,   "ldaps://<host>:636"); 
     env.put(Context.SECURITY_PROTOCOL,  "ssl") ; 

     LdapContext context = new InitialLdapContext(env, null); 

     // find the account now that the user is authenticated to 
     // construct the UserEntity 
     // note: code for findAccountByAccountName() not included as 
     //  the creation of the context is what generates the 
     //  exception 
     return findAccountByAccountName(context, "<search-base>", username); 
    } 
    } 

を認証するためのコードです新しいユーザーを作成するコードです

public static void addUser(@NotNull final String username, 
           @NotNull final String firstName, 
           @NotNull final String password) 
    throws NamingException 
    { 
    // Create a container set of attributes 
    // 
    Attributes container = new BasicAttributes(true); // case ignore 

    // Create the object class to add 
    Attribute objClasses = new BasicAttribute("objectClass"); 
    objClasses.add("top"); 
    objClasses.add("person"); 
    objClasses.add("organizationalPerson"); 
    objClasses.add("user"); 

    // Assign the username and first name 
    // 
    Attribute givenName = new BasicAttribute("givenName", firstName); 
    Attribute sAMAccountName = new BasicAttribute("sAMAccountName", username); 

    // Make the account active 
    // 
    Attribute userAccountControl = new BasicAttribute("userAccountControl", "512") ; 

    // Add these to the container 
    // 
    container.put(objClasses); 
    container.put(givenName); 
    container.put(sAMAccountName); 
    container.put(userAccountControl) ; 

    // only can do this if connecting via secure ldap 
    // 
    if (isSecureLdap()) 
    { 
     try 
     { 
      final String quotedPassword = String.format("\"%s\"", password) ; 
      Attribute pwd = new BasicAttribute("unicodePwd", quotedPassword.getBytes("UTF-16LE")); 
      container.put(pwd); 
     } 
     catch (UnsupportedEncodingException e) 
     { 
      LOGGER.error("Unable to encode password"); 
     } 
    } 

    // creates a context using the Admin credentials. 
    LdapContext context = setup() ; 

    // Create the entry 
    // 
    context.createSubcontext(getUserDN(username), container); 
} 

次に、私のテストコード:

// authenticate a known user 
// 
try 
{ 
    // existing user 'dev' and this works just fine 
    authenticate("dev", "password") ; 
} 
catch (NamingException e) 
{ 
    // if authentication fails, which it does not in this case 
} 

// create new user 
// 
final String username = "myTestUser" ; 
final String password = "myTestPassword" ; 
try 
{ 
    addUser(username, "test", password) ; 
} 
catch (NamingException e) 
{ 
    // if creation fails, which it does not in this case 
} 

// try to find that user 
// 
try 
{ 
    // code for find() not included since it works 
    final UserEntity user = findUser(username) ; 
} 
catch (NoSuchUserException e) 
{ 
    // thrown by find() if it fails -- again, this part still works 
} 

// attempt to authenticate the newly created user 
// 
try 
{ 
     authenticate(username, password) ; 
} 
catch (NamingException e) 
{ 
    // this is the part that throws the exception 
} 

私はそれが欠落しているマイナーなものであると確信していますが、SO、他の場所にかなりの周りに検索した後、私はこの問題を解決するための魔法の豆を見つけていません。

+1

ADドメインには複数のドメインコントローラがありますか?作成したアカウントをすぐに使用する場合は、アカウントが他のDCにレプリケートされていないため、同じドメインコントローラを必ず使用する必要があります。私たちのテスト環境では –

+0

、私たちは1台のコントローラしか稼動していません。 –

答えて

0

は、パスワードのコードを使用してみてください:

private byte[] encodePassword(String pass) throws UnsupportedEncodingException 
{ 
    final String ATT_ENCODING = "Unicode"; 
    // Agree with MS's ATTRIBUTE_CONSTRAINT 
    String pwd = "\"" + pass +"\""; 
    byte bytes[] = pwd.getBytes(ATTENCODING); 
    // strip unicode marker 
    byte bytes[] = new byte [_bytes.length - 2]; 
    System.arraycopy(_bytes, 2, bytes, 0,_bytes.length - 2); 
    return bytes; 
} 

のMicrosoft Active Directoryは、パスワードに関する面白いです。 通常、まずユーザーを追加してからパスワードを変更します。 -jim

+0

これは必須ではありません。レプリケーションの問題である可能性があります。 –

+0

上記のコードを試しましたが、それはWILL_NOT_PERFORM例外を生成します。私は元のUTF-16LEに "Unicode"を変更して同じ結果を得ようとしました。私の元のコード(投稿から)は成功するようです。 –

+0

と 'succeed'によって、私はそれがユーザーを作成するように見えることを意味します。ただし、そのユーザーに対してはまだ認証できません。 –

関連する問題