如何从Java查询LDAP,以从Active Directory的“ netbiosDomain \ samAccountName”中获取对象的DN

时间:2018-09-20 20:31:57

标签: java active-directory ldap

我需要从Java查询LDAP,以将用户或组的netbiosDomain\samAccountName转换为distinguishedName

例如

有两个子域:  * DC=northeast,DC=domain,DC=com  * DC=southeast,DC=domain,DC=com

有2个不同的用户:

  • NORTHEAST\NICKD = CN=nickd,CN=Users,DC=northeast,DC=domain,DC=com
  • SOUTHEAST\NICKD = CN=nickd,CN=Users,DC=southeast,DC=domain,DC=com

给出NORTHEAST\NICKD,如何查询ldap将其转换为CN=nickd,CN=Users,DC=northeast,DC=domain,DC=com

基本上,这个问题可以重新提出:如何查询LDAP以获取netbios域的distingushedName?

这里的答案https://social.technet.microsoft.com/Forums/scriptcenter/en-US/dbbeeefd-001b-4d1d-93cb-b44b0d5ba155/how-do-you-search-for-a-domain-samaccountname-in-active-directory?forum=winserverDS&prof=required提供了可以执行此操作的vbscript和powershell命令。但是我需要一个可以执行此操作的LDAP查询。或任何可以跨平台从Java调用的东西。

这是可以将northeast\nickd转换为CN=nickd,CN=Users,DC=northeast,DC=domain,DC=com的vbscript:

' Constants for the NameTranslate object.
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1

' Specify the NetBIOS name of the domain.
strNetBIOSDomain = "northeast"

' Specify the NT name of the user.
strNTName = "nickd"

' Use the NameTranslate object to convert the NT user name to the
' Distinguished Name required for the LDAP provider.
Set objTrans = CreateObject("NameTranslate")
' Initialize NameTranslate by locating the Global Catalog.
objTrans.Init ADS_NAME_INITTYPE_GC, ""
' Use the Set method to specify the NT format of the object name.
objTrans.Set ADS_NAME_TYPE_NT4, strNetBIOSDomain & "\" & strNTName
' Use the Get method to retrieve the RFC 1779 Distinguished Name.
strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)

' Escape any "/" characters with backslash escape character.
' All other characters that need to be escaped will be escaped.
strUserDN = Replace(strUserDN, "/", "\/")

Wscript.Echo strUserDN

和powershell:

$Name = "northeast"
$Domain = "nickd"

# Use the NameTranslate object.
$objTrans = New-Object -comObject "NameTranslate"
$objNT = $objTrans.GetType()

# Initialize NameTranslate by locating the Global Catalog.
$objNT.InvokeMember("Init", "InvokeMethod", $Null, $objTrans, (3, $Null))
# Specify NT name of the object.
# Trap error if object does not exist.
Try
{
    $objNT.InvokeMember("Set", "InvokeMethod", $Null, $objTrans, (3, "$Domain\$Name"))
    # Retrieve Distinguished Name of the object.
    $DN = $objNT.InvokeMember("Get", "InvokeMethod", $Null, $objTrans, 1)

    $DN
}
Catch
{
    "Bad name: $Domain\$Name"
}

相关:https://serverfault.com/questions/234041/can-an-ldap-query-on-ad-provide-the-netbios-domain-name-for-a-single-account-whe

1 个答案:

答案 0 :(得分:0)

我想我已经知道了。但我正在检查以确保结果。

我从互联网搜索中学到,AD中有一个特殊的地方可以存储域及其属性CN=Partitions,CN=Configuration,DC=domain,DC=com

我正在查询CN=SOUTHEAST,CN=Partitions,CN=Configuration,DC=domain,DC=com,但始终缺少我需要的ldap对象属性,即域的ncname的{​​{1}}。

如果看到this answer,则说明出现此问题的原因是我正在查询全局目录!当查询全局目录时,将缺少某些属性。

因此,在执行多域LDAP搜索用户和组时,您确实需要使用全局编录(默认情况下为端口3268),否则您将不会从子域获取用户/组。但是,当执行LDAP查询以获取netbios域的DN时,请确保连接到父LDAP服务器并使用本地ldap端口(默认为端口389)。

针对DN的查询变为:

  • 基本DN:ldap://parent-ldap-host:389
  • 搜索过滤器:CN=SOUTHEAST,CN=Partitions,CN=Configuration,DC=domain,DC=com
  • 搜索范围:(objectClass=*)
  • 属性:wholeSubtree

这似乎有效。我缺少的任何内容,请在下面评论或添加您自己的更好答案。谢谢。