LDAP相关的工具类,基于Spring Ldap(spring-ldap-core-1.3.x.RELEASE.jar)
需提前在Config.groovy中配置ldap的上下文源,例如:
ldapContextSource {
userDn = "cn=admin,dc=bropen,dc=com,dc=cn"
b1 {
url = "ldap://b1ldap.bropen.com.cn:389"
base = "dc=bropen,dc=com,dc=cn" // 设置base后,search、lookup都从该base开始
password = "passwordb1"
}
b2 {
url = "ldap://b2ldap.bropen.com.cn:389"
base = ""
password = "passwordb2"
}
}
启动时,在 BroBpmGrailsPlugin 的 doWithSpring 方法中,遍历该配置,并注册Spring的Bean:
b1LdapContextSource、b1LdapTemplate、b2LdapContextSource、b2LdapTemplate
等四个spring对象,使用同一个userDn来登录到ldap。
或者可以在运行时动态的调用BeanUtils.registerBean方法,注册对应的ContextSource和LdapTemplate两个Spring Bean,例如:
def abcd = BeanUtils.registerBean("abcd",
org.springframework.ldap.core.support.LdapContextSource,
[url:"ldap://localhost:389", base:"dc=nodomain", userDn:"cn=admin, dc=nodomain", password:"password"])
BeanUtils.registerBean("abcdLdapTemplate",
org.springframework.ldap.core.LdapTemplate,
[contextSource:abcd])
目前实现的方法包括 lookup、search等,例如:
LdapUtils.lookup("b1", "cn=test", ["cn","userPassword"] )
LdapUtils.lookup("b2", "cn=test,dc=nodomain", ["cn","userPassword"] ) // b2没有设置base,因此需要完整的dn
LdapUtils.lookup("b1", "cn=test", {ctx->return ctx.getStringAttribute("cn")} )
LdapUtils.search("b1", "", "objectclass=top", ["cn","sn", "objectclass[]"] )
LdapUtils.search("b2", "dc=nodomain", "objectclass=top", ["cn","sn"] )
LdapUtils.search("b1", "", "objectclass=person", {ctx->return ctx.getStringAttribute("cn")} )
这里主要对ldapTemplate的常用方法进行进一步简化和封装,具体可参考: LdapTemplate
Modifiers | Name | Description |
---|---|---|
static class |
LdapUtils.AttributesContextMapper |
根据属性列表,返回一个Map对象的映射类 |
static class |
LdapUtils.ClosureContextMapper |
返回闭包计算结果的映射类 |
Type | Name and description |
---|---|
static org.springframework.ldap.core.LdapTemplate |
getLdapTemplate(String contextName) 根据配置的ldap上下文名称,获得对应的LdapTemplate |
static Map |
lookup(String contextName, Object dn, List<String> attributes) 按照dn查找对象,并返回所需的属性 |
static Object |
lookup(String contextName, Object dn, Closure mapper) 按照dn查找对象,并返回闭包处理的结果 |
static Object |
lookup(String contextName, Object dn, List<String> attributes, Closure mapper) 按照dn查找对象,并返回闭包处理的结果 |
static List<Map> |
search(String contextName, Object base, String filter, List<String> attributes, Map options = [:] ) 按照dn查找对象,并返回所需的属性 |
static List |
search(String contextName, Object base, String filter, Closure mapper, Map options = [:] ) 根据过滤器搜索对象,使用闭包处理每个对象后返回 |
static List |
search(String contextName, Object base, String filter, List<String> attributes, Closure mapper, Map options = [:] ) 根据过滤器搜索对象,使用闭包处理每个对象后返回 |
Methods inherited from class | Name |
---|---|
class Object |
Object#wait(long), Object#wait(long, int), Object#wait(), Object#equals(Object), Object#toString(), Object#hashCode(), Object#getClass(), Object#notify(), Object#notifyAll() |
根据配置的ldap上下文名称,获得对应的LdapTemplate
按照dn查找对象,并返回所需的属性
contextName
- 配置中ldap.context中对应的上下文的名称,不能为空dn
- 要查询对象的dn(String 或 javax.naming.Name 对象,如DistinguishedName),如果上下文中配置了base,则实际查找的dn为:"dn,base"attributes
- 需要查询、返回的属性列表,不能为空,如果属性名称有后缀“[]”,则表示返回数组,否则只返回第一个值按照dn查找对象,并返回闭包处理的结果
例:根据默认的ldap.context配置,查找dn为"cn=test2,${ldap.context.base}"的对象,并返回其cn属性。
LdapUtils.lookup("", "cn=test2", {ctx-> return ctx.getStringAttribute("cn") })
mapper
- 用来转换查询结果对象的闭包,可接收参数DirContextOperations ctx,有一系列取属性的方法,常用的如getStringAttribute、getStringAttributes按照dn查找对象,并返回闭包处理的结果
attributes
- 查询结果仅返回对应属性,以供mapper使用,避免查询出所有属性后再处理,类似于sql的 "select xxx,xxx from..."按照dn查找对象,并返回所需的属性
base
- 查询的baseDnfilter
- 过滤条件,如"objectclass=person"attributes
- 需要查询、返回的属性列表,不能为空,如果属性名称有后缀“[]”,则表示返回数组,否则只返回第一个值options
- 主要是设置 SearchControls 中的参数
,包括 searchScope(0=OBJECT_SCOPE、1=ONELEVEL_SCOPE、2=SUBTREE_SCOPE)、countLimit、timeLimit、derefLink
,默认值分别为2、0、0、false。根据过滤器搜索对象,使用闭包处理每个对象后返回
例:LdapUtils.search("", "", "(objectclass=person)", {ctx-> return ctx.getStringAttribute("cn")})
根据过滤器搜索对象,使用闭包处理每个对象后返回