这篇文章的目的
当然是用“最”、“最”、”最“简单的方式来操作LDAP了。正好IBOMC项目的底层也是使用Spring LDAP的大家可以看看。
为啥要学习LDAP
鉴于中国移动使用LDAP存储数据已经有很长时间了导致过渡到Oracle很困难,公司内的很多项目依然依赖LDAP,因此JAVA对LDAP的操作还是需要学习下的。
Srping LDAP 是什么
Spring LDAP是Spring提供的一个简化JAVA对LDAP数据进行操作的工具类似于Spring的JdbcTemplate。
配置
1. 引入jar包
官网 :http://projects.spring.io/spring-ldap/
jar包的Maven地址:
org.springframework.ldap
spring-ldap-core
2.3.2.BUILD-SNAPSHOT
spring-snapshots
Spring Snapshots
https://repo.spring.io/libs-snapshot
true
2. 配置数据源
1. 在命名空间中增加以下描述
2. 配置数据源
这里有个要注意的就是base的值,后续的DN(类似于文件夹中的路径)都是基于这个的。
3. 创建PO配置映射关系
在这个实体中
objectClasses(LDAP中每一个节点的规则,例如必填字段有哪些,选填字段有哪些,规则是可以继承的)
base 这里的base加上前面数据源配置中的base合起来就是完整的路径
@Id 这个是必须有的生成每个条目都有一个路径也可以说是地址
@Attribute下面的代码表示把createDate变量和LDAP中的createDate字段关联起来
@Attribute(name="createDate")
private String createDate;
@Transient表示忽略该变量
@DnAttribute用于表示该变量是DN的一个参数
@DnAttribute(value="cn",index = 0)
private String cn;
这里index=3表示是DN的第四个参数倒过来的
例如:以下是一个条目的DN
cn=4028b8815eb6f10f015eb6f110f30001,ou=windows,ou=device,ou=resource
package com.boco.model.dto;
import org.springframework.ldap.odm.annotations.Attribute;
import org.springframework.ldap.odm.annotations.DnAttribute;
import org.springframework.ldap.odm.annotations.Entry;
import org.springframework.ldap.odm.annotations.Id;
import javax.naming.Name;
/**
* @author 黄炎
* @Time 2017/9/18.14:12
*/
@Entry(objectClasses = {"Top", "iam-windows" ,"cmcc-windows","cmcc-device"}, base="ou=windows,ou=device,ou=resource")
public class WindowsDto extends BaseDto {
@Id
private Name dn;
public Name getDn() {
return dn;
}
public void setDn(Name dn) {
this.dn = dn;
}
//必填
@Attribute(name="cn")
@DnAttribute(value="cn",index = 3)
private String cn;
@Attribute(name="conType")
private String conType;
@Attribute(name="createDate")
private String createDate;
@Attribute(name="createUserKey")
private String createUserKey;
@Attribute(name="displayName")
private String displayName;
@Attribute(name="driverType")
private String driverType;
@Attribute(name="ip")
private String ip;
@Attribute(name="name")
private String name;
@Attribute(name="progDicKey")
private String progDicKey;
@Attribute(name="resourceGroupKey")
private String resourceGroupKey;
//选填
@Attribute(name="adminAccount")
private String adminAccount;
@Attribute(name="adminPort")
private String adminPort;
@Attribute(name="adminPwd")
private String adminPwd;
@Attribute(name="desc")
private String desc;
@Attribute(name="manufacturer")
private String manufacturer;
@Attribute(name="modifyDate")
private String modifyDate;
@Attribute(name="modifyUserKey")
private String modifyUserKey;
//iam-windows ;
@Attribute(name="accessPolicykey")
private String accessPolicykey;
@Attribute(name="accountIsoType")
private String accountIsoType;
@Attribute(name="adminPrompt")
private String adminPrompt;
@Attribute(name="areaDicKey")
private String areaDicKey;
@Attribute(name="bakIp")
private String bakIp;
@Attribute(name="conPrompt")
private String conPrompt;
@Attribute(name="connected")
private String connected;
public String getCn() {
return cn;
}
public void setCn(String cn) {
this.cn = cn;
}
public String getConType() {
return conType;
}
public void setConType(String conType) {
this.conType = conType;
}
public String getCreateDate() {
return createDate;
}
public void setCreateDate(String createDate) {
this.createDate = createDate;
}
public String getCreateUserKey() {
return createUserKey;
}
public void setCreateUserKey(String createUserKey) {
this.createUserKey = createUserKey;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getDriverType() {
return driverType;
}
public void setDriverType(String driverType) {
this.driverType = driverType;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getProgDicKey() {
return progDicKey;
}
public void setProgDicKey(String progDicKey) {
this.progDicKey = progDicKey;
}
public String getResourceGroupKey() {
return resourceGroupKey;
}
public void setResourceGroupKey(String resourceGroupKey) {
this.resourceGroupKey = resourceGroupKey;
}
public String getAdminAccount() {
return adminAccount;
}
public void setAdminAccount(String adminAccount) {
this.adminAccount = adminAccount;
}
public String getAdminPort() {
return adminPort;
}
public void setAdminPort(String adminPort) {
this.adminPort = adminPort;
}
public String getAdminPwd() {
return adminPwd;
}
public void setAdminPwd(String adminPwd) {
this.adminPwd = adminPwd;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public String getModifyDate() {
return modifyDate;
}
public void setModifyDate(String modifyDate) {
this.modifyDate = modifyDate;
}
public String getModifyUserKey() {
return modifyUserKey;
}
public void setModifyUserKey(String modifyUserKey) {
this.modifyUserKey = modifyUserKey;
}
public String getAccessPolicykey() {
return accessPolicykey;
}
public void setAccessPolicykey(String accessPolicykey) {
this.accessPolicykey = accessPolicykey;
}
public String getAccountIsoType() {
return accountIsoType;
}
public void setAccountIsoType(String accountIsoType) {
this.accountIsoType = accountIsoType;
}
public String getAdminPrompt() {
return adminPrompt;
}
public void setAdminPrompt(String adminPrompt) {
this.adminPrompt = adminPrompt;
}
public String getAreaDicKey() {
return areaDicKey;
}
public void setAreaDicKey(String areaDicKey) {
this.areaDicKey = areaDicKey;
}
public String getBakIp() {
return bakIp;
}
public void setBakIp(String bakIp) {
this.bakIp = bakIp;
}
public String getConPrompt() {
return conPrompt;
}
public void setConPrompt(String conPrompt) {
this.conPrompt = conPrompt;
}
public String getConnected() {
return connected;
}
public void setConnected(String connected) {
this.connected = connected;
}
public String getIamAllowConnect() {
return iamAllowConnect;
}
public void setIamAllowConnect(String iamAllowConnect) {
this.iamAllowConnect = iamAllowConnect;
}
public String getIamDepDomain() {
return iamDepDomain;
}
public void setIamDepDomain(String iamDepDomain) {
this.iamDepDomain = iamDepDomain;
}
public String getIamDevicePwdPolicyKey() {
return iamDevicePwdPolicyKey;
}
public void setIamDevicePwdPolicyKey(String iamDevicePwdPolicyKey) {
this.iamDevicePwdPolicyKey = iamDevicePwdPolicyKey;
}
public String getIamDeviceStatus() {
return iamDeviceStatus;
}
public void setIamDeviceStatus(String iamDeviceStatus) {
this.iamDeviceStatus = iamDeviceStatus;
}
public String getIamResAccountPolicy() {
return iamResAccountPolicy;
}
public void setIamResAccountPolicy(String iamResAccountPolicy) {
this.iamResAccountPolicy = iamResAccountPolicy;
}
public String getIamResAdminUserKey() {
return iamResAdminUserKey;
}
public void setIamResAdminUserKey(String iamResAdminUserKey) {
this.iamResAdminUserKey = iamResAdminUserKey;
}
public String getIamResFlag() {
return iamResFlag;
}
public void setIamResFlag(String iamResFlag) {
this.iamResFlag = iamResFlag;
}
public String getIamResouceVersionDicKey() {
return iamResouceVersionDicKey;
}
public void setIamResouceVersionDicKey(String iamResouceVersionDicKey) {
this.iamResouceVersionDicKey = iamResouceVersionDicKey;
}
public String getIamSystemType() {
return iamSystemType;
}
public void setIamSystemType(String iamSystemType) {
this.iamSystemType = iamSystemType;
}
public String getIamWindowsJump() {
return iamWindowsJump;
}
public void setIamWindowsJump(String iamWindowsJump) {
this.iamWindowsJump = iamWindowsJump;
}
public String getIpType() {
return ipType;
}
public void setIpType(String ipType) {
this.ipType = ipType;
}
public String getIpv4() {
return ipv4;
}
public void setIpv4(String ipv4) {
this.ipv4 = ipv4;
}
public String getIpv6() {
return ipv6;
}
public void setIpv6(String ipv6) {
this.ipv6 = ipv6;
}
public String getIsOperative() {
return isOperative;
}
public void setIsOperative(String isOperative) {
this.isOperative = isOperative;
}
public String getIsSudoModel() {
return isSudoModel;
}
public void setIsSudoModel(String isSudoModel) {
this.isSudoModel = isSudoModel;
}
public String getIsVirRes() {
return isVirRes;
}
public void setIsVirRes(String isVirRes) {
this.isVirRes = isVirRes;
}
public String getLogip() {
return logip;
}
public void setLogip(String logip) {
this.logip = logip;
}
public String getPhysicalMacIp() {
return physicalMacIp;
}
public void setPhysicalMacIp(String physicalMacIp) {
this.physicalMacIp = physicalMacIp;
}
public String getResStatus() {
return resStatus;
}
public void setResStatus(String resStatus) {
this.resStatus = resStatus;
}
public String getVirResCon() {
return virResCon;
}
public void setVirResCon(String virResCon) {
this.virResCon = virResCon;
}
public String getVirResCreateTime() {
return virResCreateTime;
}
public void setVirResCreateTime(String virResCreateTime) {
this.virResCreateTime = virResCreateTime;
}
public String getVirResCreateUser() {
return virResCreateUser;
}
public void setVirResCreateUser(String virResCreateUser) {
this.virResCreateUser = virResCreateUser;
}
public String getVirResDesc() {
return virResDesc;
}
public void setVirResDesc(String virResDesc) {
this.virResDesc = virResDesc;
}
public String getVirResId() {
return virResId;
}
public void setVirResId(String virResId) {
this.virResId = virResId;
}
public String getVirResTemp() {
return virResTemp;
}
public void setVirResTemp(String virResTemp) {
this.virResTemp = virResTemp;
}
public String getVirResTempId() {
return virResTempId;
}
public void setVirResTempId(String virResTempId) {
this.virResTempId = virResTempId;
}
public String getDomainDN() {
return domainDN;
}
public void setDomainDN(String domainDN) {
this.domainDN = domainDN;
}
public String getDomainName() {
return domainName;
}
public void setDomainName(String domainName) {
this.domainName = domainName;
}
private String iamAllowConnect;
private String iamDepDomain;
private String iamDevicePwdPolicyKey;
private String iamDeviceStatus;
private String iamResAccountPolicy;
private String iamResAdminUserKey;
private String iamResFlag;
private String iamResouceVersionDicKey;
private String iamSystemType;
private String iamWindowsJump;
private String ipType;
private String ipv4;
private String ipv6;
private String isOperative;
private String isSudoModel;
private String isVirRes;
private String logip;
private String physicalMacIp;
private String resStatus;
private String virResCon;
private String virResCreateTime;
private String virResCreateUser;
private String virResDesc;
private String virResId;
private String virResTemp;
private String virResTempId;
//cmcc-windows ;
private String domainDN;
private String domainName;
}
4.使用
LdapTemplate的调用方式和spring中其他的bean是一样的
这里就不写了方式很有多种。
- 先来个最简单的查询
查找所有objectclass字段是cmcc-windows的条目
List list = ldapTemplate.find(query().where("objectclass").is("cmcc-windows"), WindowsDto.class);
for(WindowsDto windowsDto :list){
System.out.println(windowsDto.getIp());
}
结果:
- 新增
WindowsDto vo = new WindowsDto();
vo.setCn(this.getSequence());
vo.setConType("conType");
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
vo.setCreateDate(format.format(new Date()));
vo.setCreateUserKey("huangyan");
vo.setDisplayName("hy");
vo.setDriverType("windows");
vo.setIp("192.168.1.990");
vo.setName("testEntry");
vo.setProgDicKey("what is it");
vo.setResourceGroupKey("测试资源组key");
vo.setAdminAccount("huangyan");
vo.setAdminPwd("111111");
ldapTemplate.create(vo);
- 删除
这里cn是DN的最后一个参数
ldapTemplate.unbind("cn=4028b8815ebbcc77015ebbcc77010000");
//或者
ldapTemplate.unbind(windowsDto.getDn());
- 修改
ldapTemplate.update(windowsDto);