千家信息网

【总结】将关系数据库中组织机构同步至LDAP中

发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,代码参考:https://github.com/chocolateBlack/db2LdapQQ群:223460081将关系型数据数据组织机构同步到LDAP中1、获取关系型DB中组织机构关系2、生成树
千家信息网最后更新 2025年11月07日【总结】将关系数据库中组织机构同步至LDAP中

代码参考: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.SQLServerDriverjdbc.url=jdbc:sqlserver://10.10.10.10;database=hrjdbc.username=adminjdbc.password=admin

ldap.properties,LDAP相关配置

spring.profiles.active=no-apachedssample.ldap.url=ldap://10.10.10.10:389sample.ldap.userDn=cn=Manager,dc=openldap,dc=jw,dc=cnsample.ldap.password=G0qGH9123sample.ldap.base=dc=openldap,dc=jw,dc=cnsample.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;        }}


0