在 Grails 项目中,我需要连接 Active Directory 并搜索用户(身份验证目的)。
我正在使用Groovy LDAP API在 LDAP 和 Active Directory 中更大。
我的公司给了我一个用于测试的用户凭据
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 绑定(bind),但公司员工始终使用 sAMAccountName
登录。
所以,当我尝试使用 sAMAccountName=test2 和密码进行绑定(bind)时
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)
。
最佳答案
嗯,这里有两个不同的东西。首先创建新的 LDAP 实例 (LDAP.newInstance
),然后创建搜索过程 (ldapConn.search
)。在您的示例代码中,您对两种方法使用相同的凭据。但它确实应该是:
LDAP.newInstance
的有效连接字符串- 用于测试
ldapConn.search
的不同sAMAccountName
因此,作为第一步,您应该尝试使用原始的工作连接字符串(其中包括 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
关于grails - 连接到 Active Directory 并搜索用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26294077/