【问题标题】:Adding a user with a password in Active Directory LDAP在 Active Directory LDAP 中添加具有密码的用户
【发布时间】:2011-05-18 08:37:27
【问题描述】:

这是我第一次在 StackOverflow 上,我希望我能在这里得到一些回应。 我正在使用 Windows Active Directory 2008 使用 spring-ldap api 存储来自 java 的新用户

我的问题是我无法使用密码添加用户。我在某处读到在 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();
}

但它给了我错误代码 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

我不知道如何在 AD 中设置用户密码。我还阅读了一些设置 unicodePwd 的地方,如果需要,我们需要 SSL,而不是我怎么做。有没有办法解决这个问题,请帮助我

【问题讨论】:

  • 在测试时,我发现不用使用“Unicode”编码和剥离 BOM,您可以简单地使用“UTF-16LE”作为编码,例如('"' + 密码 + '"').getBytes("UTF-16LE").

标签: java active-directory ldap spring-ldap


【解决方案1】:

是的,WILL_NOT_PERFORM 错误是 AD 告诉您需要使用 SSL 连接来设置密码。


要建立 SSL 连接,您需要使用如下所示的 URL:ldaps://your.ldap.server:636(注意“ldaps”)。如果遇到证书验证错误,则需要使用“keytool”将 AD 服务器的证书导入 Java 密钥库,以便 Java 应用程序将证书识别为有效。

【讨论】:

  • 好的,大卫,你能知道我如何将 SSL 与 Ldap 一起使用吗请你展示我从未了解过的关于 SSL 与 Ldap 的完整指南.. 是否有任何替代方案,我只想设置用户密码所以任何可以解决这个问题并且我没有实现 SSL 逻辑的 Windows 服务请告诉我
  • 好的,谢谢您的回复。我知道什么是带有 AD 的 SSL 的 URL 和端口,但我很困惑从 AD 服务器导入证书。非常抱歉,您能告诉我如何获得这些证书的程序,以及此过程需要多少证书..
  • 这本身就是一个话题。以下是一些关于如何使用不同应用程序(“Crowd”)confluence.atlassian.com/display/CROWD/… 的说明——不特定于 Spring-LDAP,但基本原则是相同的:从 AD 服务器获取证书,将其导入 Java使用 keytool 的密钥库
  • 好的,谢谢大卫,我会关注它并让你知道,如果我觉得有任何困难,请保持联系,我会告诉你..
  • 你好。我有一个关于从 AD 生成证书的问题。我按照您的链接和一些谷歌搜索我认为我们需要使用第 3 方来颁发这些证书。我只想为我的测试环境设置这些,告诉我如何绕过第 3 方证书生成...
猜你喜欢
  • 2012-06-26
  • 2021-10-22
  • 1970-01-01
  • 1970-01-01
  • 2015-09-11
  • 1970-01-01
  • 2010-11-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多