springLdap 操作ldap示例(增删改查)
转自:http://blog.csdn.net/sundenskyqq/article/details/9002440
这部分的示例网上的确有很多,但是个人在查找的过程中还是感到不够满意,所以就自己总结一下,方便自己以后查阅,也方便其他童鞋查找资料。
springLdap 操作ldap示例(增删改查)
在看这个文章之前,最好是了解了openldap的schema文件,也就是了解objectClass和attribute以及它们的关系。否则很容易不了解代码的含义以及抛出的异常。
实体类:
package ldap.entity; /**
* 本测试类person对象来自schema文件的core.schema文件
* objectClass为person,必填属性和可选属性也是根据该对象得到的。
* Author:Ding Chengyun
*/
public class Person { private String sn; //必填属性
private String cn; //必填属性 private String userPassword; //可选属性
private String telephoneNumber; //可选属性
private String seeAlso; //可选属性
private String description; //可选属性
public String getSn() {
return sn;
}
public void setSn(String sn) {
this.sn = sn;
}
public String getCn() {
return cn;
}
public void setCn(String cn) {
this.cn = cn;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public String getTelephoneNumber() {
return telephoneNumber;
}
public void setTelephoneNumber(String telephoneNumber) {
this.telephoneNumber = telephoneNumber;
}
public String getSeeAlso() {
return seeAlso;
}
public void setSeeAlso(String seeAlso) {
this.seeAlso = seeAlso;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
} }
mapper类:
package ldap.mapper; import javax.naming.NamingException;
import javax.naming.directory.Attributes; import ldap.entity.Person; import org.springframework.ldap.core.AttributesMapper; /**
* 这个类的作用是将ldap中的属性转化为实体类的属性值,
* 在查询信息的时候会用到
*/
public class PersonAttributeMapper implements AttributesMapper{ @Override
public Object mapFromAttributes(Attributes attr) throws NamingException {
Person person = new Person();
person.setSn((String)attr.get("sn").get());
person.setCn((String)attr.get("cn").get()); if (attr.get("userPassword") != null) {
person.setUserPassword((String)attr.get("userPassword").get());
}
if (attr.get("telephoneNumber") != null) {
person.setTelephoneNumber((String)attr.get("telephoneNumber").get());
}
if (attr.get("seeAlso") != null) {
person.setSeeAlso((String)attr.get("seeAlso").get());
}
if (attr.get("description") != null) {
person.setDescription((String)attr.get("description").get());
}
return person;
} }
dao类:
package ldap.dao; import java.util.ArrayList;
import java.util.List; 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 ldap.entity.Person;
import ldap.mapper.PersonAttributeMapper; import org.springframework.ldap.NameNotFoundException;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.filter.AndFilter;
import org.springframework.ldap.filter.EqualsFilter; import xhrd.ucenter.ldap.entity.UcenterLdapApplication;
import xhrd.ucenter.ldap.ldapAttributeMappper.ApplicationAttributeMapper; /**
* Description: 此类的作用是使用spring的 LdapTemplate完成对ldap的增删改查的操作
* Author:Ding Chengyun
*/
public class PersonDao { //注入spring的LdapTemplate,此处在spring的配置文件中需要配置
private LdapTemplate ldapTemplate; public LdapTemplate getLdapTemplate() {
return ldapTemplate;
}
public void setLdapTemplate(LdapTemplate ldapTemplate) {
this.ldapTemplate = ldapTemplate;
} /**
* 添加 一条记录
* @param person
*/
public void createOnePerson(Person person) {
BasicAttribute ba = new BasicAttribute("objectclass");
ba.add("person"); //此处的person对应的是core.schema文件中的objectClass:person
Attributes attr = new BasicAttributes();
attr.put(ba);
//必填属性,不能为null也不能为空字符串
attr.put("cn", person.getCn());
attr.put("sn", person.getSn()); //可选字段需要判断是否为空,如果为空则不能添加
if (person.getDescription() != null
&& person.getDescription().length() > 0) {
attr.put("description", person.getDescription());
} if (person.getUserPassword() != null
&& person.getUserPassword().length() > 0) {
attr.put("userPassword", person.getUserPassword());
}
if (person.getSeeAlso() != null
&& person.getSeeAlso().length() > 0) {
attr.put("seeAlso", person.getSeeAlso());
}
if (person.getTelephoneNumber() != null
&& person.getTelephoneNumber().length() > 0) {
attr.put("telephoneNumber", person.getTelephoneNumber());
} //bind方法即是添加一条记录。
ldapTemplate.bind(getDn(person.getCn()), null, attr);
} /** /**
* 根据dn查询详细信息
* @param cn
* @return
*/
public UcenterLdapApplication getPersonDetail(String cn) {
try {
//ldapTeplate的lookup方法是根据dn进行查询,此查询的效率超高
UcenterLdapApplication ua = (UcenterLdapApplication)
ldapTemplate.lookup(getDn(cn),
new ApplicationAttributeMapper());
return ua;
} catch (NameNotFoundException e) {
return null;
}
} /**
* 根据自定义的属性值查询person列表
* @param person
* @return
*/
public List<Person> getPersonList(
Person person) {
List<Person> list = new ArrayList<Person>();
//查询过滤条件
AndFilter andFilter = new AndFilter();
andFilter.and(new EqualsFilter("objectclass", "person")); if (person.getCn() != null
&& person.getCn().length() > 0) {
andFilter.and(new EqualsFilter("cn", person.getCn()));
}
if (person.getSn() != null
&& person.getSn().length() > 0) {
andFilter.and(new EqualsFilter("sn", person.getSn()));
} if (person.getDescription() != null
&& person.getDescription().length() > 0) {
andFilter.and(new EqualsFilter("description", person.getDescription()));
} if (person.getUserPassword() != null
&& person.getUserPassword().length() > 0) {
andFilter.and(new EqualsFilter("userPassword", person.getUserPassword()));
}
if (person.getSeeAlso() != null
&& person.getSeeAlso().length() > 0) {
andFilter.and(new EqualsFilter("seeAlso", person.getSeeAlso()));
}
if (person.getTelephoneNumber() != null
&& person.getTelephoneNumber().length() > 0) {
andFilter.and(new EqualsFilter("telephoneNumber", person.getTelephoneNumber()));
}
//search是根据过滤条件进行查询,第一个参数是父节点的dn,可以为空,不为空时查询效率更高
list = ldapTemplate.search("", andFilter.encode(),
new PersonAttributeMapper());
return list;
} /**
* 删除一条记录,根据dn
* @param cn
*/
public void removeOnePerson(String cn) {
ldapTemplate.unbind(getDn(cn));
} /**
* 修改操作
* @param person
*/
public void updateOnePerson(Person person) {
if (person == null || person.getCn() == null
|| person.getCn().length() <= 0) {
return;
}
List<ModificationItem> mList = new ArrayList<ModificationItem>(); mList.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
new BasicAttribute("sn",person.getSn())));
mList.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
new BasicAttribute("description",person.getDescription())));
mList.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
new BasicAttribute("seeAlso",person.getSeeAlso())));
mList.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
new BasicAttribute("telephoneNumber",person.getTelephoneNumber())));
mList.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
new BasicAttribute("userPassword",person.getUserPassword()))); if (mList.size() > 0) {
ModificationItem[] mArray = new ModificationItem[mList.size()];
for (int i = 0; i < mList.size(); i++) {
mArray[i] = mList.get(i);
}
//modifyAttributes 方法是修改对象的操作,与rebind()方法需要区别开
ldapTemplate.modifyAttributes(this.getDn(person.getCn()), mArray);
}
}
/**
* 得到dn
* @param cn
* @return
*/
private DistinguishedName getDn(String cn) {
//得到根目录,也就是配置文件中配置的ldap的根目录
DistinguishedName newContactDN = new DistinguishedName();
// 添加cn,即使得该条记录的dn为"cn=cn,根目录",例如"cn=abc,dc=testdc,dc=com"
newContactDN.add("cn", cn);
return newContactDN;
}
}
springLdap 操作ldap示例(增删改查)的更多相关文章
- C#操作Excel数据增删改查示例
Excel数据增删改查我们可以使用c#进行操作,首先创建ExcelDB.xlsx文件,并添加两张工作表,接下按照下面的操作步骤即可 C#操作Excel数据增删改查. 首先创建ExcelDB.xlsx文 ...
- Spring Ldap 的增删改查
package ldap.entity; /** * 本测试类person对象来自schema文件的core.schema文件 * objectClass为person,必填属性和可选属性也是根据该对 ...
- C#操作Excel数据增删改查(转)
C#操作Excel数据增删改查. 首先创建ExcelDB.xlsx文件,并添加两张工作表. 工作表1: UserInfo表,字段:UserId.UserName.Age.Address.CreateT ...
- sqlite数据库操作详细介绍 增删改查,游标
sqlite数据库操作详细介绍 增删改查,游标 本文来源于www.ifyao.com禁止转载!www.ifyao.com Source code package com.example ...
- java操作数据库:增删改查
不多bb了直接上. 工具:myeclipse 2016,mysql 5.7 目的:java操作数据库增删改查商品信息 test数据库的goods表 gid主键,自增 1.实体类Goods:封装数据库数 ...
- python操作三大主流数据库(8)python操作mongodb数据库②python使用pymongo操作mongodb的增删改查
python操作mongodb数据库②python使用pymongo操作mongodb的增删改查 文档http://api.mongodb.com/python/current/api/index.h ...
- python操作mysql数据库增删改查的dbutils实例
python操作mysql数据库增删改查的dbutils实例 # 数据库配置文件 # cat gconf.py #encoding=utf-8 import json # json里面的字典不能用单引 ...
- Asp.Net操作MySql数据库增删改查
Asp.Net操作MySql数据库增删改查,话不多说直接步入正题.git源码地址:https://git.oschina.net/gxiaopan/NetMySql.git 1.安装MySQL数据库 ...
- SpringBoot操作MongoDB实现增删改查
本篇博客主讲如何使用SpringBoot操作MongoDB. SpringBoot操作MongoDB实现增删改查 (1)pom.xml引入依赖 <dependency> <group ...
随机推荐
- 为什么要有http响应码
其实还是比较容易理解的.这就和你去小卖部买东西一样,老板,我想要一袋大米,那老板先得回答有还是没有,还是说我们这没有,去其它地方买去吧,得先给个说法,这个说法就是http相应码,有了http响应码之后 ...
- c语言实践:求两个数的最大公约数
我的思路是这样的:比如12和16这两个数.先理解一下概念,什么叫最大公约数.就是12有很多个因数,16也有很多个因数,这两堆因数中有一些重合的因数,在这些重合的因数中找到那个最大的.那么最大公约数一定 ...
- JavaScript——Dom编程(2)
①.创建一个元素节点: var reference = document.createElement(element) createElement(): 按照给定的标签名创建一个新的元素节点. 方法只 ...
- ROW_NUMBER分页
var query = string.Format("SELECT {0} FROM (SELECT ROW_NUMBER() OVER (ORDER BY {3}) AS RowNum, ...
- linux查看进程开始时间
ps -eo pid,lstart|grep [pid] 命令解释: -e Select all processes. -o,o Specify user-defined format. pid a ...
- RequireJS 主入口加载模块经常会加载失败的问题
在接入requirejs时,在main入口遇到了这样的问题,使用jquery,刷新10次页面会有3-4次加载失败,找不到$符号等等 require.config({ 'baseUrl': './mod ...
- mvc - view传值到js
http://www.cnblogs.com/akwwl/p/5238975.html
- FTP文件上传以及获取ftp配置帮助类
帮助类: using QSProjectBase; using Reform.CommonLib; using System; using System.Collections.Generic; us ...
- MySQL事务在MGR中的漫游记—路线图
欢迎访问网易云社区,了解更多网易技术产品运营经验. MGR即MySQL Group Replication,是MySQL官方推出的基于Paxos一致性协议的数据高可靠.服务高可用方案.MGR在20 ...
- web静态资源的访问(笔记)
1.在web-inf外的静态资源访问: 三种方式: 2.访问web-inf内的静态资源: 使用请求转发的方式访问web-inf静态资源(重定向相当于在浏览器输入,无法访问) 参考:https://bl ...