LDAP 相关的工具类,基于 Spring Ldap Core
需提前在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"
\}\
\}
启动时,在 BroToolkitGrailsPlugin 的 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, int), Object#wait(long), 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")})
根据过滤器搜索对象,使用闭包处理每个对象后返回