2010-12-01 11 views
3

にパスワードを持つユーザーを追加するこれは私の初めてのStackOverflowです。私はここでいくつかの回答を得ることを願っています。 私はWindows Active Directory 2008を使用して、spring-ldap APIを使用してJavaから新しいユーザーを保存していますActive Directory LDAP

私の問題は、パスワードをユーザーに追加できないことです。パスワードを設定するにはADのどこかを読んで、unicodePwd属性を使用する必要があります。出典: http://geekswithblogs.net/lance/archive/2005/08/19/LdapAuthenticationASP.aspx

public void insertContact(ContactDTO contactDTO) { 
    try{ 

    Attributes personAttributes = new BasicAttributes(); 
    BasicAttribute personBasicAttribute = new BasicAttribute("objectclass"); 
    personBasicAttribute.add("person"); 
    personBasicAttribute.add("user"); 
    personAttributes.put(personBasicAttribute); 

     personAttributes.put("givenName", contactDTO.getCommonName()); 
     personAttributes.put("cn", contactDTO.getCommonName()); 
     personAttributes.put("sn", contactDTO.getLastName()); 
     personAttributes.put("description", contactDTO.getDescription()); 

     personAttributes.put("unicodePwd", 
      this.createUnicodePassword(contactDTO.getPassword())); 
     personAttributes.put("userPrincipalName", contactDTO.getUserLoginName()); 
     personAttributes.put("sAMAccountName", contactDTO.getsAMAccountName()); 
     personAttributes.put("displayname", contactDTO.getDisplayname()); 
     // personAttributes.put("pwdLastSet", "0"); 
     // personAttributes.put("LockOutTime", "0"); 

     personAttributes.put("userAccountControl", "544"); 

     BasicAttribute roomAttribute = new BasicAttribute("roomNumber"); 
     for(String r : contactDTO.getRoomNumber()) 
     { 
     roomAttribute.add(r); 
     } 

     personAttributes.put(roomAttribute); 


     DistinguishedName newContactDN = new DistinguishedName(); 
     newContactDN.add("cn", contactDTO.getCommonName()); 

     ldapTemplate.bind(newContactDN, null, personAttributes); 
    } 

public byte[] createUnicodePassword(String password){ 
    return toUnicodeBytes(doubleQuoteString(password)); 
} 

private byte[] toUnicodeBytes(String str){ 
    byte[] unicodeBytes = null; 
    try{ 
     byte[] unicodeBytesWithQuotes = str.getBytes("Unicode"); 
     unicodeBytes = new byte[unicodeBytesWithQuotes.length - 2]; 
     System.arraycopy(unicodeBytesWithQuotes, 2, unicodeBytes, 0, 
      unicodeBytesWithQuotes.length - 2); 
    } catch(UnsupportedEncodingException e){ 
     // This should never happen. 
     e.printStackTrace(); 
    } 
    return unicodeBytes; 
} 

private String doubleQuoteString(String str){ 
    StringBuffer sb = new StringBuffer(); 
    sb.append("\""); 
    sb.append(str); 
    sb.append("\""); 
    return sb.toString(); 
} 

が、それは私がADでユーザーパスワードを設定する方法を私は知らない、エラーコード53

enter code here: org.springframework.ldap.UncategorizedLdapException: Operation failed; nested exception is javax.naming.OperationNotSupportedException: [LDAP: error code 53 - 0000001F: SvcErr: DSID-031A11E5, problem 5003 (WILL_NOT_PERFORM), data 0 

を与えられました。私はunicodePwdを設定する場所もいくつか読んでいますが、これが必要な場合には私はSSLを必要とする場合があります。この問題を解決するための方法はありますか?

+0

これをテストしているときに、「Unicode」エンコードとBOMのストリッピングを使用する代わりに、エンコーディングとして「UTF-16LE」を使用することができます。 ( '' '' + password + '"').getBytes(" UTF-16LE ")。 –

答えて

0

あなたは 'unicodePassword'について語りますが、あなたのコードは 'unicodepwd'を設定します。どちらですか?

+0

ああ、申し訳ありませんEJPこれはちょうど "unicodePwd"属性です... –

+0

これまでに3通りの方法がありました。あなたのコードで正しいものを使用することをお勧めします。 – EJP

+0

あなたは何を言っているのか分かりません。もっと説明してください。 –

2

はい、WILL_NOT_PERFORMエラーは、SSL接続を使用してパスワードを設定する必要があることを知らせるADです。


SSL接続を作成するには、のように見えるURLを使用する必要があります。ldaps://your.ldap.server:636を(「LDAPS」を注意してください)。証明書の検証エラーが発生した場合は、「keytool」を使用してADキーの証明書をJavaキーストアにインポートする必要があります。これにより、Javaアプリケーションは証明書を有効と認識します。

+0

ok DavidさんはLdapでSSLをどのように使用できるのか知っていますか?LdapでSSLについてわからない完全なガイドを表示できますか。私はちょうどこの問題を解決することができますし、SSLロジックを実装していないすべてのWindowsサービスをユーザーパスワードを設定するようにしたい –

+0

あなたの応答のために大丈夫です..私はURLとSSL ADサーバーから証明書をインポートするのは混乱します。非常に申し訳ありませんが、これらの証明書をどのように入手できるのか、どのくらいの証明書がこのプロセスのrequresなのかを私に教えてください。 –

+0

それ自体は話題です。別のアプリケーション( "Crowd")でどのように操作するかについて説明します。http://confluence.atlassian.com/display/CROWD/Configuring+an+SSL+Certificate+for+Microsoft+Active+Directory - not SpringのLDAP固有のものですが、基本的な原則は同じです:ADサーバから証明書を取得し、 'keytool'を使用してJavaキーストアにインポートします。 –

関連する問題