【问题标题】:Connecting to Active directory and searching user连接到 Active Directory 并搜索用户
【发布时间】:2014-10-31 17:12:06
【问题描述】:

在 Grails 项目中,我需要连接 Active Directory 并搜索用户(身份验证目的)。

我在 LDAP 和 Active Directory 中使用 Groovy LDAP API 和更大。

我的公司给了我一张用于测试的用户凭证

OU=Vendors,DC=company,DC=net
CN=Testing -2
sAMAccountName=test2
password=test123
ip=LDAP://xx.xx.xx.xx:389

所以我尝试了

LDAP ldapConn = LDAP.newInstance("LDAP://xx.xx.xx.xx:389", "CN=Testing -2,OU=Vendors,DC=company,DC=net", "test123")
try{
    def results = ldapConn.search('(CN=Testing -2)', 'OU=Vendors,DC=company,DC=net', SearchScope.ONE )
    println "${results.size} entries found:"
    println results
}
catch(Exception ex){
    println ex.printStackTrace()
}

以上代码有效,我使用CN=Testing -2 绑定Active Directory,但公司员工始终使用sAMAccountName 登录。

所以,当我尝试使用 sAMAccountName=test2 和密码进行绑定时

LDAP ldapConn = LDAP.newInstance("LDAP://xx.xx.xx.xx:389", "sAMAccountName=test2,OU=Vendors,DC=company,DC=net", "test123")
try{
    def results = ldapConn.search('(sAMAccountName=test2)', 'OU=Vendors,DC=company,DC=net', SearchScope.ONE )
    println "${results.size} entries found:"
    println results
}
catch(Exception ex){
    println ex.printStackTrace()
}

我来了

java.lang.NullPointerException
Error |
    at org.apache.directory.groovyldap.LDAP.search(Unknown Source)
Error |
    at org.apache.directory.groovyldap.LDAP$search.call(Unknown Source)
Error |

那么我该如何继续,test2 用户总是使用他的sAMAccountName (test2) 登录而不是使用CN (Testing -2)

【问题讨论】:

    标签: grails groovy active-directory ldap groovy-ldap


    【解决方案1】:

    嗯,这里有两个不同的东西。首先,您创建了新的 LDAP 实例 (LDAP.newInstance),然后是搜索过程 (ldapConn.search)。在您的示例代码中,您对两种方法使用相同的凭据。但它确实应该是:

    • LDAP.newInstance 的有效连接字符串
    • 另一个sAMAccountName 用于测试ldapConn.search

    因此,作为第一步,您应该尝试使用原始工作连接字符串(包括LDAP.newInstance("LDAP://xx.xx.xx.xx:389", "CN=Testing -2,... 的连接字符串 - 对于初学者)并在您的ldapConn.search 中留下您要检查的sAMAccountName

    说明:根据您的设置,您尝试的连接方式可能无效,此外,不检查两个地方是一种更简洁的方法,但可以确保初始连接正常工作并且只有搜索返回实际结果。

    在下面的文章中,有一些关于不同 LDAP 属性的信息,这也可能会有所帮助: http://www.computerperformance.co.uk/Logon/LDAP_attributes_active_directory.htm

    【讨论】:

      猜你喜欢
      • 2010-11-26
      • 1970-01-01
      • 2012-12-07
      • 1970-01-01
      • 1970-01-01
      • 2021-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多