2023年6月21日发(作者:)

java对Ldap操作

{

/** *//**

* 定义使用springframework对ldap的访问操作对象 借助LdapTemplate可以实现大部分对ldap的操作

*/

private LdapTemplate ldapTemplate;

/** *//**

* @param ldapTemplate

* spring setter 注入

*/

public void setLdapTemplate(LdapTemplate ldapTemplate) ...{

mplate = ldapTemplate;

}

/** *//**

* 获得所有的用户名(ldap称cn),可根据第二个参数指定返回值是否重复

*

* @param scope

* 取值为0、1、2,分别对应 SearchControls 类 OBJECT_SCOPE, ONELEVEL_SCOPE,

* SUBTREE_SCOPE三个查询范围,分别代表 当前对象查询、当前节点下对象查询、当前节点所有子目录查询

*

* @param distinct

* true,去掉结构中的重复值;false 允许结果中包含重复值

* @return 查询范围下返回的cn列表

*/

public List<String> getAllPersonNames(int scope, boolean distinct) ...{

// 存储查询结果的集合

final ArrayList<String> allPersonCnList = new ArrayList<String>(); // 使用ldapTemplate提供的接口查询,objectclass=person 表示对象为person的对象

("", "(objectclass=person)",

createSearchControls(scope), new AttributesMapper() ...{

public Object mapFromAttributes(Attributes attrs)

throws NamingException ...{

// 获取cn的属性,用户名存在cn中

Attribute attr = ("cn");

// 若没有该属性返回null

if (attr == null) ...{

return null;

} else ...{ // 获取属性

// 若包含多个cn属性,需要循环获取

Enumeration ent = ();

while (eElements()) ...{

(ement()

.toString());

}

}

return null;

}

});

// true,去掉结果中的重复值

if (distinct) ...{

ArrayList<String> templist = new ArrayList<String>();

for (String cn : allPersonCnList)

if (!ns(cn)) ...{

(cn);

}

// 返回无重复值的结果

return templist;

}

// 返回包含重复值的结果 return allPersonCnList;

}

/** *//**

* 查询指定范围下的所有用户信息

*

* @param scope

* 取值为0、1、2,分别对应 SearchControls 类 OBJECT_SCOPE, ONELEVEL_SCOPE,

* SUBTREE_SCOPE三个查询范围,分别代表 当前对象查询、当前节点下对象查询、当前节点所有子目录查询

*

* @return 查询范围下返回的所有用户信息列表

*/

public List getAllPersons(int scope) ...{

// 使用ldapTemplate提供的接口查询,objectclass=person 表示对象为person的对象

return ("", "(objectclass=person)",

createSearchControls(scope), new LdapObjectAttributesMapper());

}

/** *//**

* 根据Uid查询用户信息,*代表任意长度的任意字符

*

* @param uid

* 用户的uid

* @param scope

* 取值为0、1、2,分别对应 SearchControls 类 OBJECT_SCOPE, ONELEVEL_SCOPE,

* SUBTREE_SCOPE三个查询范围,分别代表 当前对象查询、当前节点下对象查询、当前节点所有子目录查询

*

* @return 用户信息

*/ public List getPersonByUid(String uid, int scope) ...{

// 使用ldapTemplate提供的接口查询,objectclass=person 表示对象为person的对象

// uid 和 objectclass 组成联合查询条件,两个同时满足的查询结果

return ("", "(&(objectclass=person)(uid=" + uid

+ "))", createSearchControls(scope),

new LdapObjectAttributesMapper());

}

/** *//**

* 查询包含当前Cn信息的所有用户,*代表任意长度的任意字符

*

* @param cn

* 用户的cn

* @param scope

* 取值为0、1、2,分别对应 SearchControls 类 OBJECT_SCOPE, ONELEVEL_SCOPE,

* SUBTREE_SCOPE三个查询范围,分别代表 当前对象查询、当前节点下对象查询、当前节点所有子目录查询

*

* @return 用户列表

*/

public List getPersonByCn(String cn, int scope) ...{

// 使用ldapTemplate提供的接口查询,objectclass=person 表示对象为person的对象

// cn 和 objectclass 组成联合查询条件,两个同时满足的查询结果

return ("",

"(&(objectclass=person)(cn=" + cn + "))",

createSearchControls(scope), new LdapObjectAttributesMapper());

}

/** *//**

* @param scope * 取值为0、1、2,分别对应 SearchControls 类 OBJECT_SCOPE, ONELEVEL_SCOPE,

* SUBTREE_SCOPE三个查询范围,分别代表 当前对象查询、当前节点下对象查询、当前节点所有子目录查询

*

* @return 返回查询控制器对象

*/

private SearchControls createSearchControls(int scope) ...{

// 查询控制类SearchControls设定查询范围

SearchControls constraints = new SearchControls();

rchScope(scope);

return constraints;

}

/** *//**

* 使用LdapPersonInfo类对象实现复合查询,属性中可使用通配符*,*代表任意长度的任意字符

*

* @param ldapPersonInfo

* 查询条件

* @param scope

* 取值为0、1、2,分别对应 SearchControls 类 OBJECT_SCOPE, ONELEVEL_SCOPE,

* SUBTREE_SCOPE三个查询范围,分别代表 当前对象查询、当前节点下对象查询、当前节点所有子目录查询

*

* @return 用户列表

*/

public List getPersonByPersonEnty(LdapPersonInfo ldapPersonInfo, int scope) ...{

// strb存储、组装查询条件集合

StringBuffer strb = new StringBuffer("(&(objectclass=person)");

// uid 属性

if (() != null && () != "") ...{ ("(uid=" + () + ")");

}

// givenname 属性

if (stName() != null

&& stName() != "") ...{

("(givenname=" + stName() + ")");

}

// sn 属性

if (tName() != null

&& tName() != "") ...{

("(sn=" + tName() + ")");

}

// cn 属性

if (() != null && ().size() > 0)

...{

for (int i = 0; i < ().size(); i++)

("(cn=" + ().get(i) + ")");

}

// telephonenumber 属性

if (ephone() != null

&& ephone() != "") ...{

("(telephonenumber=" + ephone()

+ ")");

}

// facsimiletelephonenumber 属性

if (() != null && () != "") ...{

("(facsimiletelephonenumber=" + ()

+ ")");

} // mail 属性

if (l() != null && l() != "") ...{

("(mail=" + l() + ")");

}

String filter = (")").toString();

return ("", filter, createSearchControls(scope),

new LdapObjectAttributesMapper());

}

/** *//**

* 根据dn查找用户,dn为base dn 的相对dn.(若basedn为:dc=koal,dc=com,user

* dn为:uid=123,dc=koal,dc=com,则此处只需要提供 123 作为参数)

*

* @param dn

* 相对base dn的dn参数

* @return 用户信息

*/

public LdapPersonInfo getLdapObjectByDn(String dn) ...{

return (LdapPersonInfo) (dn,

new LdapObjectAttributesMapper());

}

/** *//**

* 登陆验证

*

* @param userDn

* 用户的user dn

* @param password

* 用户登陆密码

* @return 登陆成功返回true,不成功返回false

*/

private boolean loginCheack(String userDn, String password) ...{ // 获取DirContext对象

DirContext ctxs = textSource().getReadOnlyContext();

try ...{

// 组装登陆ldap需要的信息数据

Hashtable<Object, Object> ht = new Hashtable<Object, Object>();

// 获取已有的登陆信息

ht = (Hashtable<Object, Object>) ironment();

// 设置用户

(TY_PRINCIPAL, userDn);

// 设置用户登陆密码

(TY_CREDENTIALS, password);

// 设置用户验证方式为simple

(TY_AUTHENTICATION, "simple");

// 出现异常时表示用当前登陆人登陆失败

DirContext c = new InitialDirContext(ht);

();

return true;

} catch (Exception e) ...{

// tackTrace();

n("login false");

return false;

} finally ...{

try ...{

();

} catch (Exception ie) ...{

}

}

}

/** *//**

* 验证用户登陆 *

* @param uid

* 用户uid

* @param password

* 用户密码

*

* @return 是否登陆成功

*/

public boolean userLogin(String uid, String password) ...{

// 获取用户id所在ldap中的user dn

List dnlist = getUserDnByUid(uid);

// 根据查询到的所有dn遍历,检查是否某一user dn与用户密码可以登陆ldap

for (Object dn : dnlist) ...{

if (loginCheack(ng(), password) == true) ...{

return true;

}

}

return false;

}

/** *//**

* 查询用户user dn

*

* @param uid

* 用户uid

*

* @return 用户dn列表,当前目录节点下可能存在多个相同uid的多个user dn

*/

public List<String> getUserDnByUid(String uid) ...{

// 获取DirContext对象

DirContext ctx = textSource().getReadOnlyContext();

// 存储用户dn ArrayList<String> dn = new ArrayList<String>();

try ...{

SearchControls constraints = new SearchControls();

rchScope(E_SCOPE);

NamingEnumeration en = ("", "uid=" + uid, constraints);

// 查询所有用户

while (en != null && eElements()) ...{

Object obj = ement();

if (obj instanceof SearchResult) ...{

SearchResult si = (SearchResult) obj;

// 获取dn并添加到查询列表

(eInNamespace());

}

}

();

} catch (Exception e) ...{

tackTrace();

try ...{

();

} catch (Exception ee) ...{

tackTrace();

}

}

return dn;

}

/** *//**

* ldap用户信息数据填充类 将获取的属性信息封装为LdapObject对象

*

*/

private class LdapObjectAttributesMapper implements AttributesMapper ...{ public Object mapFromAttributes(Attributes attrs)

throws NamingException ...{

LdapPersonInfo LdapObject = new LdapPersonInfo();

try ...{

// 获取并封装uid属性

((String) getAttribute(attrs, "uid"));

// 获取并封装givenname属性

stName((String) getAttribute(attrs,

"givenname"));

// 获取并封装sn属性

tName((String) getAttribute(attrs, "sn"));

// 获取并封装cn属性

(getMoreSameAttributes(attrs, "cn"));

// 获取并封装telephonenumber属性

ephone((String) getAttribute(attrs,

"telephonenumber"));

// 获取并封装facsimiletelephonenumber属性

((String) getAttribute(attrs,

"facsimiletelephonenumber"));

// 获取并封装mail属性

l((String) getAttribute(attrs, "mail"));

} catch (NamingException n) ...{

tackTrace();

}

// 返回封装后的用户对象

return LdapObject;

}

/** *//**

* 从属性列表中获取指定的属性

*

* @param attrs * 属性列表

* @param attrName

* 需要获取的属性

* @return 返回获取的属性值

* @throws NamingException

*/

private String getAttribute(Attributes attrs, String attrName)

throws NamingException ...{

Attribute attr = (attrName);

// 若没有指定的属性返回空字符串

if (attr == null) ...{

return "";

} else ...{

return (String) ();

}

}

/** *//**

* 从属性列表中获取指定的属性的所有属性值

*

* @param attrs

* 属性列表

* @param attrName

* 需要获取的属性

* @return 返回获取的属性值

* @throws NamingException

*/

private List<String> getMoreSameAttributes(Attributes attrs,

String attrName) throws NamingException ...{

Attribute attr = (attrName);

List<String> elelist = new ArrayList<String>(); // 若没有指定的属性返回null

if (attr == null) ...{

return null;

} else ...{

// 获取当前属性的所有值,添加到返回列表中

Enumeration ent = ();

while (eElements())

(ement().toString());

return elelist;

}

}

}

private void dispPerson(LdapPersonInfo temp) ...{

n("-----------------------------");

n("User(uid: " + () + ") ");

n("First Name: " + stName());

n("Last Name: " + tName());

n("Common Name: " + ());

n("User ID: " + ());

n("E-Mail: " + l());

n("Phone: " + ephone());

n("Fax: " + ());

n("List completed.");

n("-----------------------------n");

}

public static void main(String[] str) ...{

ApplicationContext context = new ClassPathXmlApplicationContext(

"");

UserDaoLdapImpl test = (UserDaoLdapImpl) n("userDao"); n(gin("lisi", "123"));

}

}

查看文档来源:/sniperit8/article/details/2113525

2023年6月21日发(作者:)

java对Ldap操作

{

/** *//**

* 定义使用springframework对ldap的访问操作对象 借助LdapTemplate可以实现大部分对ldap的操作

*/

private LdapTemplate ldapTemplate;

/** *//**

* @param ldapTemplate

* spring setter 注入

*/

public void setLdapTemplate(LdapTemplate ldapTemplate) ...{

mplate = ldapTemplate;

}

/** *//**

* 获得所有的用户名(ldap称cn),可根据第二个参数指定返回值是否重复

*

* @param scope

* 取值为0、1、2,分别对应 SearchControls 类 OBJECT_SCOPE, ONELEVEL_SCOPE,

* SUBTREE_SCOPE三个查询范围,分别代表 当前对象查询、当前节点下对象查询、当前节点所有子目录查询

*

* @param distinct

* true,去掉结构中的重复值;false 允许结果中包含重复值

* @return 查询范围下返回的cn列表

*/

public List<String> getAllPersonNames(int scope, boolean distinct) ...{

// 存储查询结果的集合

final ArrayList<String> allPersonCnList = new ArrayList<String>(); // 使用ldapTemplate提供的接口查询,objectclass=person 表示对象为person的对象

("", "(objectclass=person)",

createSearchControls(scope), new AttributesMapper() ...{

public Object mapFromAttributes(Attributes attrs)

throws NamingException ...{

// 获取cn的属性,用户名存在cn中

Attribute attr = ("cn");

// 若没有该属性返回null

if (attr == null) ...{

return null;

} else ...{ // 获取属性

// 若包含多个cn属性,需要循环获取

Enumeration ent = ();

while (eElements()) ...{

(ement()

.toString());

}

}

return null;

}

});

// true,去掉结果中的重复值

if (distinct) ...{

ArrayList<String> templist = new ArrayList<String>();

for (String cn : allPersonCnList)

if (!ns(cn)) ...{

(cn);

}

// 返回无重复值的结果

return templist;

}

// 返回包含重复值的结果 return allPersonCnList;

}

/** *//**

* 查询指定范围下的所有用户信息

*

* @param scope

* 取值为0、1、2,分别对应 SearchControls 类 OBJECT_SCOPE, ONELEVEL_SCOPE,

* SUBTREE_SCOPE三个查询范围,分别代表 当前对象查询、当前节点下对象查询、当前节点所有子目录查询

*

* @return 查询范围下返回的所有用户信息列表

*/

public List getAllPersons(int scope) ...{

// 使用ldapTemplate提供的接口查询,objectclass=person 表示对象为person的对象

return ("", "(objectclass=person)",

createSearchControls(scope), new LdapObjectAttributesMapper());

}

/** *//**

* 根据Uid查询用户信息,*代表任意长度的任意字符

*

* @param uid

* 用户的uid

* @param scope

* 取值为0、1、2,分别对应 SearchControls 类 OBJECT_SCOPE, ONELEVEL_SCOPE,

* SUBTREE_SCOPE三个查询范围,分别代表 当前对象查询、当前节点下对象查询、当前节点所有子目录查询

*

* @return 用户信息

*/ public List getPersonByUid(String uid, int scope) ...{

// 使用ldapTemplate提供的接口查询,objectclass=person 表示对象为person的对象

// uid 和 objectclass 组成联合查询条件,两个同时满足的查询结果

return ("", "(&(objectclass=person)(uid=" + uid

+ "))", createSearchControls(scope),

new LdapObjectAttributesMapper());

}

/** *//**

* 查询包含当前Cn信息的所有用户,*代表任意长度的任意字符

*

* @param cn

* 用户的cn

* @param scope

* 取值为0、1、2,分别对应 SearchControls 类 OBJECT_SCOPE, ONELEVEL_SCOPE,

* SUBTREE_SCOPE三个查询范围,分别代表 当前对象查询、当前节点下对象查询、当前节点所有子目录查询

*

* @return 用户列表

*/

public List getPersonByCn(String cn, int scope) ...{

// 使用ldapTemplate提供的接口查询,objectclass=person 表示对象为person的对象

// cn 和 objectclass 组成联合查询条件,两个同时满足的查询结果

return ("",

"(&(objectclass=person)(cn=" + cn + "))",

createSearchControls(scope), new LdapObjectAttributesMapper());

}

/** *//**

* @param scope * 取值为0、1、2,分别对应 SearchControls 类 OBJECT_SCOPE, ONELEVEL_SCOPE,

* SUBTREE_SCOPE三个查询范围,分别代表 当前对象查询、当前节点下对象查询、当前节点所有子目录查询

*

* @return 返回查询控制器对象

*/

private SearchControls createSearchControls(int scope) ...{

// 查询控制类SearchControls设定查询范围

SearchControls constraints = new SearchControls();

rchScope(scope);

return constraints;

}

/** *//**

* 使用LdapPersonInfo类对象实现复合查询,属性中可使用通配符*,*代表任意长度的任意字符

*

* @param ldapPersonInfo

* 查询条件

* @param scope

* 取值为0、1、2,分别对应 SearchControls 类 OBJECT_SCOPE, ONELEVEL_SCOPE,

* SUBTREE_SCOPE三个查询范围,分别代表 当前对象查询、当前节点下对象查询、当前节点所有子目录查询

*

* @return 用户列表

*/

public List getPersonByPersonEnty(LdapPersonInfo ldapPersonInfo, int scope) ...{

// strb存储、组装查询条件集合

StringBuffer strb = new StringBuffer("(&(objectclass=person)");

// uid 属性

if (() != null && () != "") ...{ ("(uid=" + () + ")");

}

// givenname 属性

if (stName() != null

&& stName() != "") ...{

("(givenname=" + stName() + ")");

}

// sn 属性

if (tName() != null

&& tName() != "") ...{

("(sn=" + tName() + ")");

}

// cn 属性

if (() != null && ().size() > 0)

...{

for (int i = 0; i < ().size(); i++)

("(cn=" + ().get(i) + ")");

}

// telephonenumber 属性

if (ephone() != null

&& ephone() != "") ...{

("(telephonenumber=" + ephone()

+ ")");

}

// facsimiletelephonenumber 属性

if (() != null && () != "") ...{

("(facsimiletelephonenumber=" + ()

+ ")");

} // mail 属性

if (l() != null && l() != "") ...{

("(mail=" + l() + ")");

}

String filter = (")").toString();

return ("", filter, createSearchControls(scope),

new LdapObjectAttributesMapper());

}

/** *//**

* 根据dn查找用户,dn为base dn 的相对dn.(若basedn为:dc=koal,dc=com,user

* dn为:uid=123,dc=koal,dc=com,则此处只需要提供 123 作为参数)

*

* @param dn

* 相对base dn的dn参数

* @return 用户信息

*/

public LdapPersonInfo getLdapObjectByDn(String dn) ...{

return (LdapPersonInfo) (dn,

new LdapObjectAttributesMapper());

}

/** *//**

* 登陆验证

*

* @param userDn

* 用户的user dn

* @param password

* 用户登陆密码

* @return 登陆成功返回true,不成功返回false

*/

private boolean loginCheack(String userDn, String password) ...{ // 获取DirContext对象

DirContext ctxs = textSource().getReadOnlyContext();

try ...{

// 组装登陆ldap需要的信息数据

Hashtable<Object, Object> ht = new Hashtable<Object, Object>();

// 获取已有的登陆信息

ht = (Hashtable<Object, Object>) ironment();

// 设置用户

(TY_PRINCIPAL, userDn);

// 设置用户登陆密码

(TY_CREDENTIALS, password);

// 设置用户验证方式为simple

(TY_AUTHENTICATION, "simple");

// 出现异常时表示用当前登陆人登陆失败

DirContext c = new InitialDirContext(ht);

();

return true;

} catch (Exception e) ...{

// tackTrace();

n("login false");

return false;

} finally ...{

try ...{

();

} catch (Exception ie) ...{

}

}

}

/** *//**

* 验证用户登陆 *

* @param uid

* 用户uid

* @param password

* 用户密码

*

* @return 是否登陆成功

*/

public boolean userLogin(String uid, String password) ...{

// 获取用户id所在ldap中的user dn

List dnlist = getUserDnByUid(uid);

// 根据查询到的所有dn遍历,检查是否某一user dn与用户密码可以登陆ldap

for (Object dn : dnlist) ...{

if (loginCheack(ng(), password) == true) ...{

return true;

}

}

return false;

}

/** *//**

* 查询用户user dn

*

* @param uid

* 用户uid

*

* @return 用户dn列表,当前目录节点下可能存在多个相同uid的多个user dn

*/

public List<String> getUserDnByUid(String uid) ...{

// 获取DirContext对象

DirContext ctx = textSource().getReadOnlyContext();

// 存储用户dn ArrayList<String> dn = new ArrayList<String>();

try ...{

SearchControls constraints = new SearchControls();

rchScope(E_SCOPE);

NamingEnumeration en = ("", "uid=" + uid, constraints);

// 查询所有用户

while (en != null && eElements()) ...{

Object obj = ement();

if (obj instanceof SearchResult) ...{

SearchResult si = (SearchResult) obj;

// 获取dn并添加到查询列表

(eInNamespace());

}

}

();

} catch (Exception e) ...{

tackTrace();

try ...{

();

} catch (Exception ee) ...{

tackTrace();

}

}

return dn;

}

/** *//**

* ldap用户信息数据填充类 将获取的属性信息封装为LdapObject对象

*

*/

private class LdapObjectAttributesMapper implements AttributesMapper ...{ public Object mapFromAttributes(Attributes attrs)

throws NamingException ...{

LdapPersonInfo LdapObject = new LdapPersonInfo();

try ...{

// 获取并封装uid属性

((String) getAttribute(attrs, "uid"));

// 获取并封装givenname属性

stName((String) getAttribute(attrs,

"givenname"));

// 获取并封装sn属性

tName((String) getAttribute(attrs, "sn"));

// 获取并封装cn属性

(getMoreSameAttributes(attrs, "cn"));

// 获取并封装telephonenumber属性

ephone((String) getAttribute(attrs,

"telephonenumber"));

// 获取并封装facsimiletelephonenumber属性

((String) getAttribute(attrs,

"facsimiletelephonenumber"));

// 获取并封装mail属性

l((String) getAttribute(attrs, "mail"));

} catch (NamingException n) ...{

tackTrace();

}

// 返回封装后的用户对象

return LdapObject;

}

/** *//**

* 从属性列表中获取指定的属性

*

* @param attrs * 属性列表

* @param attrName

* 需要获取的属性

* @return 返回获取的属性值

* @throws NamingException

*/

private String getAttribute(Attributes attrs, String attrName)

throws NamingException ...{

Attribute attr = (attrName);

// 若没有指定的属性返回空字符串

if (attr == null) ...{

return "";

} else ...{

return (String) ();

}

}

/** *//**

* 从属性列表中获取指定的属性的所有属性值

*

* @param attrs

* 属性列表

* @param attrName

* 需要获取的属性

* @return 返回获取的属性值

* @throws NamingException

*/

private List<String> getMoreSameAttributes(Attributes attrs,

String attrName) throws NamingException ...{

Attribute attr = (attrName);

List<String> elelist = new ArrayList<String>(); // 若没有指定的属性返回null

if (attr == null) ...{

return null;

} else ...{

// 获取当前属性的所有值,添加到返回列表中

Enumeration ent = ();

while (eElements())

(ement().toString());

return elelist;

}

}

}

private void dispPerson(LdapPersonInfo temp) ...{

n("-----------------------------");

n("User(uid: " + () + ") ");

n("First Name: " + stName());

n("Last Name: " + tName());

n("Common Name: " + ());

n("User ID: " + ());

n("E-Mail: " + l());

n("Phone: " + ephone());

n("Fax: " + ());

n("List completed.");

n("-----------------------------n");

}

public static void main(String[] str) ...{

ApplicationContext context = new ClassPathXmlApplicationContext(

"");

UserDaoLdapImpl test = (UserDaoLdapImpl) n("userDao"); n(gin("lisi", "123"));

}

}

查看文档来源:/sniperit8/article/details/2113525