【总结】将关系数据库中组织机构同步至LDAP中
admin
2023-05-26 01:21:24
0

代码参考:https://github.com/chocolateBlack/db2Ldap

QQ群:223460081


将关系型数据数据组织机构同步到LDAP中

1、获取关系型DB中组织机构关系

2、生成树型数据结构(因数据库不同,获取、生成树形结构方式不同)

3、按树形结构,自上而下向LDAP增加组织结构节点

4、获取关系型数据库中用户与组织机构关联关系。

5、LDAP增加用户节点


环境相关配置

applicationContext.xml 工程环境spring配置文件





       

    
    
    

    

    

    
    

    
    
    
    
        
    
    
        
    

    
    

    
        
        
            
            
            
            
            
        
    
    
    
    
    
    
      	
	      
	          
	          
	          
	          
	     
    
    
    

db.properties 关系型数据库相关配置

jdbc.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.url=jdbc:sqlserver://10.10.10.10;database=hr
jdbc.username=admin
jdbc.password=admin

ldap.properties,LDAP相关配置

spring.profiles.active=no-apacheds
sample.ldap.url=ldap://10.10.10.10:389
sample.ldap.userDn=cn=Manager,dc=openldap,dc=jw,dc=cn
sample.ldap.password=G0qGH9123
sample.ldap.base=dc=openldap,dc=jw,dc=cn
sample.ldap.directory.type=NORMAL

部分代码

    /**
     * 通过原生方式增加一个组织结构
     */
	@Test
	public void createNode(){
		Attributes attr = new BasicAttributes(); 
		BasicAttribute ocattr = new BasicAttribute("objectclass");
		ocattr.add("organizationalUnit");
		ocattr.add("top");
		attr.put(ocattr);
		ldapTemplate.bind("ou=业务", null, attr);
		ldapTemplate.bind("ou=事业部, ou=业务", null, attr);
		ldapTemplate.bind("ou=项目组,ou=事业部, ou=业务", null, attr);
	}


    /**
     * 通过原生方式添加User
     */
	@Test
	public void createU(){
		Attributes attr = new BasicAttributes(); 
		BasicAttribute ocattr = new BasicAttribute("objectclass");
		ocattr.add("top");
		ocattr.add("organizationalPerson");
		ocattr.add("shadowAccount");
		attr.put(ocattr);
		attr.put("userPassword", "12");
		attr.put("sn", "12");
		attr.put("uid", "12");
		
//		ldapTemplate.bind("ou=IT", null, attr);// buildDN() function
		ldapTemplate.bind("cn=123,ou=A项目组,ou=A事业部, ou=业务", null, attr);
	}
    /**
     * 通过Entity注解Java类的方式,增加一个组织机构,两种方式,一个通过orgService接口,另一个中直接通过ldapTemplate
     */
    @Test
	public void createOrganization(){
    	JWOrganization org = new JWOrganization();
    	org.setId("ou=1, ou=事业部, ou=业务");
    	orgService.createJWOrg(org);
//		ldapTemplate.create(org);
	}	
	
	
    /**
     * 测试新增一个用户,并将该用户添加到某个Group中
     */
    @Test
	public void createUser(){
    	JWUser user = new JWUser();
    	user.setId("cn=111, ou=事业部, ou=业务");
		user.setEmail("123@126.com");
		user.setEmployeeNumber("123");
		user.setLastName("lastName");
		user.setPhone("123");
		user.setTitle("title");
		user.setUid("123");
		user.setUserPassword("c9c4c39a6ce34112314ba89c1e777");
		
		userService.createJWUser(user);
		addMemberToGroup(user);
//		ldapTemplate.create(user);
	}


组织机构类JWOrganization

package org.springframework.ldap.samples.useradmin.domain;

import java.util.ArrayList;
import java.util.List;

import javax.naming.Name;

import org.springframework.data.domain.Persistable;
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 org.springframework.ldap.odm.annotations.Transient;
import org.springframework.ldap.support.LdapUtils;

/**
 * @author jgh
 */
@Entry(objectClasses = { "organizationalUnit",  "top"})
public final class JWOrganization implements Persistable{
	private static final long serialVersionUID = 1L;
	
	@Id
	private Name id;
	
    @Attribute(name = "ou")
    @DnAttribute(value="ou")
    private String fullName;
    
	@Transient
   	private String orgCode;
	@Transient
	private String orgName;
	@Transient
	private String orgParentCode;
	@Transient
	private String orgType;
	@Transient
	private List children = new ArrayList();
	
	public List getChildren() {
		return children;
	}
	public void setChildren(List children) {
		this.children = children;
	}
	
    public JWOrganization(String orgCode, String orgName,
			String orgParentCode, String orgType) {
		this.orgCode=orgCode;
		this.orgName=orgName;
		this.orgParentCode= orgParentCode;
		this.orgType=orgType;
		this.fullName = orgName;
	}

	public JWOrganization() {
		// TODO Auto-generated constructor stub
	}

    public void setId(Name id) {
        this.id = id;
    }
    
    public void setId(String id) {
        this.id = LdapUtils.newLdapName(id);
    }
	
	public String getOrgCode() {
		return orgCode;
	}

	public void setOrgCode(String orgCode) {
		this.orgCode = orgCode;
	}

	public String getOrgName() {
		return orgName;
	}

	public void setOrgName(String orgName) {
		this.orgName = orgName;
	}

	public String getOrgParentCode() {
		return orgParentCode;
	}

	public void setOrgParentCode(String orgParentCode) {
		this.orgParentCode = orgParentCode;
	}

	public String getOrgType() {
		return orgType;
	}

	public void setOrgType(String orgType) {
		this.orgType = orgType;
	}

	public String getFullName() {
		return fullName;
	}
	public void setFullName(String fullName) {
		this.fullName = fullName;
	}
	
	@Override
	public boolean isNew() {
//		Serializable id = getId();
//		return id == null || StringUtils.isBlank(String.valueOf(id));
		return true;
	}
	@Override
	public Name getId() {
		return this.id;
	}
	
}


用户类

/*
 * Copyright 2005-2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.ldap.samples.useradmin.domain;

import javax.naming.Name;

import org.springframework.data.domain.Persistable;
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 org.springframework.ldap.support.LdapUtils;

/**
 * @author Mattias Hellborg Arthursson
 */
@Entry(objectClasses = { "inetOrgPerson", "organizationalPerson", "person", "top", "shadowAccount" })
public final class JWUser implements Persistable{
	private static final long serialVersionUID = 1L;

    @Id
    private Name id;
    
    @Attribute(name = "cn")
    @DnAttribute(value="cn")
    private String fullName;

    @Attribute(name = "employeeNumber")
    private String employeeNumber;

    @Attribute(name = "sn")
    private String lastName;

    @Attribute(name = "title")
    private String title;

    @Attribute(name = "mail")
    private String email;

    @Attribute(name = "telephoneNumber")
    private String phone;
    
    @Attribute(name = "uid")
    private String uid;
    
    @Attribute(name = "userPassword")
    private String userPassword;
    
    @Override
    public Name getId() {
        return id;
    }

    public void setId(Name id) {
        this.id = id;
    }

    public void setId(String id) {
        this.id = LdapUtils.newLdapName(id);
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getEmployeeNumber() {
        return employeeNumber;
    }

    public void setEmployeeNumber(String employeeNumber) {
        this.employeeNumber = employeeNumber;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	public String getUserPassword() {
		return userPassword;
	}

	public void setUserPassword(String userPassword) {
		this.userPassword = userPassword;
	}

	@Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        JWUser user = (JWUser) o;

        if (id != null ? !id.equals(user.id) : user.id != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return id != null ? id.hashCode() : 0;
    }

	@Override
	public boolean isNew() {
		return true;
	}
}


相关内容

热门资讯

中国首例对外贸易国家安全调查 商务部新闻发言人就对相关进口打印复印办公设备发起对外贸易国家安全调查答记者问。问:我们关注到商务部发...
商务部:对美国合规性测试公司采... 中华人民共和国商务部令二〇二六年 第3号经国家反外国制裁工作协调机制批准,现公布《关于对美国合规性测...
神二十一号乘组返回后首次与记者... 今天(8月5日)下午,神舟二十一号航天员乘组与记者见面会在北京航天城举行。这是神舟二十一号航天员张陆...
将办公软件变为娱乐道具 “乱讲... 将办公软件变为娱乐道具 用轻松幽默甚至“无厘头”方式解读荒诞主题 “乱讲PPT”解锁社交新方式 PP...
三星电子公布CXL 3.2内存... IT之家 8 月 5 日消息,作为三星电子 FMS 2026 峰会活动的一部分,该企业在官网上公布了...
超擎数智与香港中文大学(深圳)... 【科技快报网】2026年8月3日,超擎数智与香港中文大学(深圳)正式签署联合实验室协议,共同成立港中...
男子突发性耳聋,元凶竟是“电钻... “池塘边的榕树上,知了在声声叫着夏天……”盛夏蝉鸣,本是很多人记忆中,夏天标配的背景音。 但是,最近...
合物660|为都市成年人打造一... 8月7日至9日,合物660文创园将在户外大草坪举办一场以“大人放假日”(BigKidDays)为主题...
日本机器人失落三十年 ► 文 观察者网心智观察所 东京的日本科学未来馆里,今天依然摆着一台已经“退休”的机器人。直到20...
长城H10汽车与小米CarIo... IT之家 8 月 5 日消息,长城汽车今日宣布,长城 H10 与小米 CarIoT 实现跨生态互通,...