Spring-ldap 操作LDAP

Posted sjwudhwhhw

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring-ldap 操作LDAP相关的知识,希望对你有一定的参考价值。

Spring-ldap 操作LDAP: 

Spring-ldap 地址: http://www.springsource.org/ldap/(进入
Spring 版本: spring-framework-2.5.6
Spring-ldap 版本:  spring-ldap-1.3.1
所需Spring的jar包: spring.jar
所需Spring-ldap的jar包: spring-ldap-1.3.1.RELEASE-all.jar
所需其他的jar包:ldapbp-1.0.jar、commons-lang-2.5.jar、commons-logging-1.0.4.jar、log4j-1.2.9.jar


案例:

工程图:
技术图片

实体类:

package com.nice.dao; publicclassUser{ privateString id; privateString name; publicString getId(){ return id; } publicvoid setId(String id){ this.id = id; } publicString getName(){ return name; } publicvoid setName(String name){ this.name = name; } }

操作类:

package com.nice.dao; import java.util.List; import javax.naming.NamingException; import javax.naming.directory.Attribute; import javax.naming.directory.Attributes; import javax.naming.directory.BasicAttribute; import javax.naming.directory.BasicAttributes; import javax.naming.directory.DirContext; import javax.naming.directory.ModificationItem; import org.springframework.ldap.core.AttributesMapper; import org.springframework.ldap.core.LdapTemplate; publicclassUserDao{ privateLdapTemplate ldapTemplate; publicvoid add(String rdn,User user){ Attributes attrs =newBasicAttributes(); Attribute ocAttr =newBasicAttribute("objectClass"); ocAttr.add("top"); ocAttr.add("organizationalunit"); attrs.put(ocAttr); Attribute snAttr =newBasicAttribute("sn"); snAttr.add(user.getName()); attrs.put(snAttr); this.ldapTemplate.bind(rdn,null, attrs); } publicvoiddelete(String rdn){ this.ldapTemplate.unbind(rdn); } publicvoid modifyRDN(String oRDN,String nRDN){ this.ldapTemplate.rename(oRDN, nRDN); } publicvoid modfiyAttrs(String rdn,User user){ Attribute attr =newBasicAttribute("sn"); attr.add(user.getName()); ModificationItem item =newModificationItem(DirContext.REPLACE_ATTRIBUTE, attr); this.ldapTemplate.modifyAttributes(rdn,newModificationItem[]{item}); } publicUser find(String rdn){ return(User)this.ldapTemplate.lookup(rdn,newAttributesMapper(){ publicObject mapFromAttributes(Attributes attributes) throwsNamingException{ User user =newUser(); if(attributes!=null){ Attribute idAttr = attributes.get("uid"); if(idAttr!=null){ user.setId((String)idAttr.get()); } Attribute nameAttr = attributes.get("sn"); if(nameAttr!=null){ user.setName((String)nameAttr.get()); } } return user; } }); } publicList<User> search(String rdn,String fileter){ returnthis.ldapTemplate.search(rdn, fileter, newAttributesMapper(){ publicObject mapFromAttributes(Attributes attributes) throwsNamingException{ User user =newUser(); if(attributes!=null){ Attribute idAttr = attributes.get("uid"); if(idAttr!=null){ user.setId((String)idAttr.get()); } Attribute nameAttr = attributes.get("sn"); if(nameAttr!=null){ user.setName((String)nameAttr.get()); } } return user; } }); } publicvoid setLdapTemplate(LdapTemplate ldapTemplate){ this.ldapTemplate = ldapTemplate; } }

配置文件:

<?xml version="1.0" encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- 配置数据源 --> <beanid="contextSource" class="org.springframework.ldap.core.support.LdapContextSource"> <propertyname="url"value="ldap://127.0.0.1:389"/> <propertyname="base"value="dc=domain,dc=com"/> <propertyname="userDn"value="cn=manager"/> <propertyname="password"value="12345678"/> </bean> <!-- 配置LdapTemplate --> <beanid="ldapTemplate"class="org.springframework.ldap.core.LdapTemplate"> <constructor-argref="contextSource"/> </bean> <!-- 配置实体类 --> <beanid="userDao"class="com.nice.dao.UserDao"> <propertyname="ldapTemplate"ref="ldapTemplate"/> </bean> </beans>

测试类:

package com.nice.dao; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; publicclassMain{ publicstaticvoid add(UserDao userDao){ User user =newUser(); user.setId("12345678"); user.setName("zhangsan"); userDao.add("ou="+ user.getName(), user); } publicstaticvoiddelete(UserDao userDao){ userDao.delete("ou=1234"); } publicstaticvoid modfiyRDN(UserDao userDao){ userDao.modifyRDN("ou=12345678","ou=123456"); } publicstaticvoid modfiyAttrs(UserDao userDao){ User user =newUser(); user.setId("12345678"); user.setName("张三"); userDao.modfiyAttrs("uid=cuibo, ou=users", user); } publicstaticvoid find(UserDao userDao){ User user = userDao.find("uid=zhangsan, ou=users"); System.out.println(user.getId()+" | "+ user.getName()); } publicstaticvoid search(UserDao userDao){ List<User> userList = userDao.search("ou=users","(objectclass=*)"); System.out.println(userList.size()); for(User user : userList){ System.out.println(user.getId()+" | "+ user.getName()); } } /** * @param args */ publicstaticvoid main(String[] args){ ApplicationContext ac=newClassPathXmlApplicationContext("applicationContext.xml"); UserDao userDao=(UserDao)ac.getBean("userDao"); System.out.println(userDao); // 增加 // add(userDao); // 删除 //delete(userDao); // 修改DN // modfiyRDN(userDao); // 修改属性 modfiyAttrs(userDao); // 单个条目查找 // find(userDao); // 批量查询 search(userDao); } }

--------------------------------------------------------------------------------------------------------------------------------------------------

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!http://www.captainbed.net

以上是关于Spring-ldap 操作LDAP的主要内容,如果未能解决你的问题,请参考以下文章

2017.4.10 spring-ldap官方文档学习

验证文件 spring-ldap.xsd 时出错

如何将自定义凭据传递给 Spring Boot ldap

Spring LDAP vs UnboundId LDAP

如何从 ldap 服务器读取操作属性

操作AD的用户信息(通过LDAP应用)