【总结】将关系数据库中组织机构同步至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;
	}
}


相关内容

热门资讯

俄将延长对华免签 据塔斯社报道,俄罗斯总统新闻秘书佩斯科夫今天(5月20日)在北京接受采访时表示,俄罗斯将延长对中国公...
李在明谴责以色列在国际水域扣押... 【环球网报道 记者 李梓瑜】据英国路透社、《以色列时报》等媒体报道,韩国总统李在明20日谴责以色列在...
《给阿嬷的情书》幕后故事全在这... 郑萱轩第一次看到《给阿嬷的情书》的小说就觉得,它如果拍成电影是可以破亿的。小说是导演蓝鸿春发给她的,...
青岛一排水沟出现蓝色水体,当地... 澎湃新闻记者 薛莎莎 实习生 陈芷若5月19日深夜,青岛市城阳区联合调查组发布通报:针对网友反映的城...
塞尔维亚总统武契奇将于5月24... 5月20日,据外交部网站消息,外交部发言人宣布:应国家主席习近平邀请,塞尔维亚总统武契奇将于5月24...
国家中医药管理局通报3・15晚... 针对2026年总台3·15晚会曝光的涉中医药领域违法违规问题,国家中医药管理局发布通报:针对辽宁沈阳...
回响∣一粒种子的无限可能 【编者按】由中共河南省委宣传部、河南广播电视台联合制作的理论宣传系列微视频《回响·第四季》正式上线!...
捷克总理最新涉华表态,中方回应 ‍‍5月20日,外交部发言人郭嘉昆主持例行记者会。有记者提问,据报道,捷克总理巴比什近日在捷媒发表评...
固强新材料取得防缠绕搅拌叶片组... 国家知识产权局信息显示,浙江固强新材料有限公司取得一项名为“一种防缠绕的搅拌叶片组件”的专利,授权公...
一人公司加速崛起 中国城市竞逐... [ 当技术让个人的能力边界不断扩展,当政策让创业的门槛不断降低,“一个人就是一支队伍”正在成为现实。...