连接到Active目录并搜索用户

时间:2014-10-10 07:28:02

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

在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)

1 个答案:

答案 0 :(得分:2)

嗯,你这里有两件不同的东西。首先,您可以创建新的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

相关问题