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
案例:
工程图:
实体类:
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; public class User { private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void 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; public class UserDao { private LdapTemplate ldapTemplate; public void add(String rdn,User user){ Attributes attrs = new BasicAttributes(); Attribute ocAttr = new BasicAttribute("objectClass"); ocAttr.add("top"); ocAttr.add("organizationalunit"); attrs.put(ocAttr); Attribute snAttr = new BasicAttribute("sn"); snAttr.add(user.getName()); attrs.put(snAttr); this.ldapTemplate.bind(rdn, null, attrs); } public void delete(String rdn){ this.ldapTemplate.unbind(rdn); } public void modifyRDN(String oRDN,String nRDN){ this.ldapTemplate.rename(oRDN, nRDN); } public void modfiyAttrs(String rdn,User user){ Attribute attr = new BasicAttribute("sn"); attr.add(user.getName()); ModificationItem item = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attr); this.ldapTemplate.modifyAttributes(rdn, new ModificationItem[]{item}); } public User find(String rdn) { return (User)this.ldapTemplate.lookup(rdn, new AttributesMapper(){ public Object mapFromAttributes(Attributes attributes) throws NamingException { User user = new User(); 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; } }); } public List<User> search(String rdn,String fileter) { return this.ldapTemplate.search(rdn, fileter, new AttributesMapper(){ public Object mapFromAttributes(Attributes attributes) throws NamingException { User user = new User(); 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; } }); } public void setLdapTemplate(LdapTemplate ldapTemplate) { this.ldapTemplate = ldapTemplate; } }
配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="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"> <!-- 配置数据源 --> <bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource"> <property name="url" value="ldap://127.0.0.1:389" /> <property name="base" value="dc=domain,dc=com" /> <property name="userDn" value="cn=manager" /> <property name="password" value="12345678" /> </bean> <!-- 配置LdapTemplate --> <bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate"> <constructor-arg ref="contextSource" /> </bean> <!-- 配置实体类 --> <bean id="userDao" class="com.nice.dao.UserDao"> <property name="ldapTemplate" ref="ldapTemplate" /> </bean> </beans>
测试类:
package com.nice.dao; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void add(UserDao userDao){ User user = new User(); user.setId("12345678"); user.setName("zhangsan"); userDao.add("ou=" + user.getName(), user); } public static void delete(UserDao userDao){ userDao.delete("ou=1234"); } public static void modfiyRDN(UserDao userDao){ userDao.modifyRDN("ou=12345678", "ou=123456"); } public static void modfiyAttrs(UserDao userDao){ User user = new User(); user.setId("12345678"); user.setName("张三"); userDao.modfiyAttrs("uid=cuibo, ou=users", user); } public static void find(UserDao userDao){ User user = userDao.find("uid=zhangsan, ou=users"); System.out.println(user.getId() + " | " + user.getName()); } public static void 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 */ public static void main(String[] args) { ApplicationContext ac=new ClassPathXmlApplicationContext("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); } }