Commit 818d5f4b authored by huyuchen's avatar huyuchen

huyc ldap相关代码提交

parent b92e4d48
......@@ -41,5 +41,6 @@
<module>yifu-common-test</module>
<module>yifu-common-dapr</module>
<module>yifu-common-swagger</module>
<module>yifu-common-ldap</module>
</modules>
</project>
......@@ -128,4 +128,7 @@ public interface CommonConstants {
* hgw 2022-6-9 17:36:35
*/
String DOWN_LINE = "_";
//空字符串
String EMPTY_STRING ="";
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>yifu-common</artifactId>
<groupId>com.yifu.cloud.plus.v1</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<description>ldap引入</description>
<artifactId>yifu-common-ldap</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-core</artifactId>
</dependency>
<dependency>
<groupId>com.unboundid</groupId>
<artifactId>unboundid-ldapsdk</artifactId>
<version>3.2.0</version>
</dependency>
</dependencies>
</project>
package com.yifu.cloud.plus.v1.yifu.common.ldap.config;
import com.yifu.cloud.plus.v1.yifu.common.ldap.util.LdapUtil;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class LdapAutoConfigue {
@Bean
public LdapUtil getLdapUtils() {
return new LdapUtil();
}
}
package com.yifu.cloud.plus.v1.yifu.common.ldap.entity;
import lombok.Data;
import lombok.ToString;
import org.springframework.ldap.odm.annotations.Attribute;
import org.springframework.ldap.odm.annotations.Entry;
import java.util.Date;
@Data
@ToString
@Entry(objectClasses = {"posixAccount","top","inetOrgPerson"}, base = "ou=安徽皖信人力资源管理有限公司,ou=wanxin")
public class PersonVo {
/**
* 主键
*/
@Attribute(name = "uid")
private String uid;
/**
* 常用姓名
*/
@Attribute(name = "cn")
private String personName;
/**
* 电话
*/
@Attribute(name = "telephoneNumber")
private String telephoneNumber;
/**
* 邮箱
*/
@Attribute(name = "email")
private String email;
/**
* 展示姓名
*/
@Attribute(name = "displayName")
private String displayName;
/**
* 真实姓名
*/
@Attribute(name = "sn")
private String sn;
/**
* 用户url
*/
@Attribute(name = "homeDirectory")
private String homeDirectory;
/**
* 主键
*/
@Attribute(name = "ou")
private String deptName;
/**
* 主键
*/
@Attribute(name = "userPassword")
private String password;
/**
* 创建时间
*/
@Attribute
protected Date createTime;
/**
* 更新时间
*/
@Attribute
protected Date updateTime;
/**
* 状态
*/
@Attribute
protected Integer status;
}
package com.yifu.cloud.plus.v1.yifu.common.ldap.mapper;
import com.yifu.cloud.plus.v1.yifu.common.ldap.entity.PersonVo;
import org.springframework.ldap.core.AttributesMapper;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
public class PersonAttributesMapper implements AttributesMapper<PersonVo> {
@Override
public PersonVo mapFromAttributes(Attributes attributes) throws NamingException {
PersonVo personVo = new PersonVo();
if (null != attributes.get("cn")) {
personVo.setPersonName((String) attributes.get("cn").get());
}
if (null != attributes.get("uid")) {
personVo.setUid((String) attributes.get("uid").get());
}
if (null != attributes.get("telephoneNumber")) {
personVo.setTelephoneNumber((String) attributes.get("telephoneNumber").get());
}
if (null != attributes.get("mail")) {
personVo.setEmail((String) attributes.get("mail").get());
}else {
personVo.setEmail("");
}
if (null != attributes.get("ou")) {
personVo.setDeptName((String) attributes.get("ou").get());
}
personVo.setPassword((String) attributes.get("uid").get());
return personVo;
}
}
package com.yifu.cloud.plus.v1.yifu.common.ldap.util;
import com.unboundid.ldap.sdk.*;
import com.unboundid.ldap.sdk.controls.SubentriesRequestControl;
import com.yifu.cloud.plus.v1.yifu.common.ldap.entity.PersonVo;
import com.yifu.cloud.plus.v1.yifu.common.ldap.mapper.PersonAttributesMapper;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ldap.core.LdapTemplate;
import java.util.ArrayList;
import java.util.List;
import static org.springframework.ldap.query.LdapQueryBuilder.query;
@Log4j2
public class LdapUtil {
@Autowired
private LdapTemplate ldapTemplate;
/**
* @return List<SearchResultEntry>
* @author huyc
* @date 2022-06-14 17:56
* @description get请求获取指定对象信息
**/
public List<SearchResultEntry> getAllPersonNamesWithTraditionalWay() {
List<SearchResultEntry> result = new ArrayList<SearchResultEntry>();
try {
LDAPConnection connection = new LDAPConnection("192.168.1.65", 389, "cn=admin,dc=worfu,dc=com", "yifu123456!");
SearchRequest searchRequest = new SearchRequest("ou=安徽皖信人力资源管理有限公司,ou=wanxin,dc=worfu,dc=com", SearchScope.SUB, "(objectclass=*)");
searchRequest.addControl(new SubentriesRequestControl());
SearchResult searchResult = connection.search(searchRequest);
for (SearchResultEntry entry : searchResult.getSearchEntries()) {
result.add(entry);
}
} catch (LDAPException e) {
e.printStackTrace();
}
return result;
}
public List<PersonVo> getAllPersons() {
List<PersonVo> list = ldapTemplate.search(query()
.where("objectclass").is("posixAccount"), new PersonAttributesMapper());
return list;
}
}
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.yifu.cloud.plus.v1.yifu.common.ldap.config.LdapAutoConfigue\
spring.ldap.urls=ldap://192.168.1.65:389
spring.ldap.username=cn=admin,dc=worfu,dc=com
spring.ldap.password=yifu123456!
spring.ldap.base=dc=worfu,dc=com
\ No newline at end of file
......@@ -65,6 +65,11 @@
<groupId>com.yifu.cloud.plus.v1</groupId>
<artifactId>yifu-common-swagger</artifactId>
</dependency>
<dependency>
<groupId>com.yifu.cloud.plus.v1</groupId>
<artifactId>yifu-common-ldap</artifactId>
<version>1.0.0</version>
</dependency>
<!--<dependency>
<groupId>com.yifu.cloud.plus.v1</groupId>
<artifactId>yifu-common-swagger</artifactId>
......
......@@ -134,4 +134,13 @@ public class DeptController {
return R.ok(sysDeptService.listChildDeptId(deptId));
}
/**
* 从ldap中获取部门
* @return R
*/
@SysLog("从ldap中获取部门")
@PostMapping("/updateDept")
public R updateDeptByLdap() {
return R.ok(sysDeptService.updateDeptByLdap());
}
}
......@@ -226,4 +226,13 @@ public class UserController {
return userService.importUser(excelVOList, bindingResult);
}
/**
* 从ldap中获取用户
* @return R
*/
@SysLog("从ldap中获取用户")
@PostMapping("/updateUser")
public R updateByLdap() {
return R.ok(userService.updateByLdap());
}
}
......@@ -19,6 +19,7 @@ package com.yifu.cloud.plus.v1.yifu.admin.service;
import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.extension.service.IService;
import com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysDept;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import java.util.List;
......@@ -72,4 +73,10 @@ public interface SysDeptService extends IService<SysDept> {
*/
List<Long> listChildDeptId(Long deptId);
/**
* 从ldap中获取部门
* @return R
*/
R updateDeptByLdap();
}
......@@ -122,4 +122,10 @@ public interface SysUserService extends IService<SysUser> {
*/
R<Boolean> registerUser(UserDTO userDto);
/**
* 从ldap中获取用户
* @return R
*/
R updateByLdap();
}
......@@ -20,15 +20,23 @@ import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unboundid.ldap.sdk.SearchResultEntry;
import com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysDept;
import com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysDeptRelation;
import com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysUser;
import com.yifu.cloud.plus.v1.yifu.admin.mapper.SysDeptMapper;
import com.yifu.cloud.plus.v1.yifu.admin.service.SysDeptRelationService;
import com.yifu.cloud.plus.v1.yifu.admin.service.SysDeptService;
import com.yifu.cloud.plus.v1.yifu.admin.service.SysUserService;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import com.yifu.cloud.plus.v1.yifu.common.ldap.util.LdapUtil;
import com.yifu.cloud.plus.v1.yifu.common.security.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -46,10 +54,15 @@ import java.util.stream.Collectors;
*/
@Service
@RequiredArgsConstructor
@Slf4j
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements SysDeptService {
private final SysDeptRelationService sysDeptRelationService;
private final SysUserService sysUserService;
private final LdapUtil ldapUtil;
/**
* 添加信息部门
* @param dept 部门
......@@ -164,4 +177,69 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
return TreeUtil.build(collect, parentId);
}
}
/**
* 从ldap中获取用户
* @return R
*/
@Override
public R<Boolean> updateDeptByLdap() {
List<SearchResultEntry> entryList = ldapUtil.getAllPersonNamesWithTraditionalWay();
for (SearchResultEntry entry : entryList) {
String dn = entry.getDN();
String userId = "";
if (dn.contains("uid")) {
userId = dn.substring(4, dn.indexOf(",", 1));
}
List<String> list = Arrays.asList(dn.split(",")).stream().filter(e -> (e.contains("ou=") && !e.equals("ou=wanxin"))).collect(Collectors.toList());
String deptName = list.get(0).replace("ou=", "");
int size = list.size();
if (size == 1) {
SysDept sysDept = this.getOne(Wrappers.<SysDept>query().lambda().eq(SysDept::getName, deptName));
if (null == sysDept) {
SysDept insertSysDept = new SysDept();
insertSysDept.setName(deptName);
insertSysDept.setParentId(CommonConstants.MENU_TREE_ROOT_ID);
this.save(insertSysDept);
} else {
sysDept.setName(deptName);
this.updateDeptById(sysDept);
}
} else if (size >= 2) {
SysDept sysDept = new SysDept();
if (size > 2) {
SysDept sysDeptThree = this.getOne(Wrappers.<SysDept>query().lambda().eq(SysDept::getName, list.get(2).replace("ou=", "")));
sysDept = this.getOne(Wrappers.<SysDept>query().lambda().eq(SysDept::getName, list.get(1).replace("ou=", ""))
.eq(SysDept::getParentId,sysDeptThree.getDeptId()));
} else {
sysDept = this.getOne(Wrappers.<SysDept>query().lambda().eq(SysDept::getName, list.get(1).replace("ou=", "")));
}
if (null != sysDept) {
SysDept sysDeptCount = this.getOne(Wrappers.<SysDept>query().lambda().eq(SysDept::getName, deptName).eq(SysDept::getParentId,sysDept.getDeptId()));
if (null == sysDeptCount) {
SysDept insertSysDept = new SysDept();
insertSysDept.setName(deptName);
insertSysDept.setParentId(sysDept.getDeptId());
this.save(insertSysDept);
} else {
if (StringUtils.isNotBlank(userId)) {
try {
SysUser user = sysUserService.getOne(Wrappers.<SysUser>query().lambda().eq(SysUser::getUsername, userId));
if (null != user) {
user.setDeptId(sysDeptCount.getDeptId());
sysUserService.updateById(user);
}
sysDeptCount.setName(deptName);
sysDeptCount.setParentId(sysDept.getDeptId());
this.updateDeptById(sysDeptCount);
} catch (Exception e) {
log.info("更新异常:"+e.getMessage());
return R.failed("同步信息失败!");
}
}
}
}
}
}
return R.ok();
}
}
......@@ -24,6 +24,7 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.plugin.excel.vo.ErrorMessage;
import com.unboundid.ldap.sdk.SearchResultEntry;
import com.yifu.cloud.plus.v1.yifu.admin.api.dto.UserDTO;
import com.yifu.cloud.plus.v1.yifu.admin.api.dto.UserInfo;
import com.yifu.cloud.plus.v1.yifu.admin.api.entity.*;
......@@ -39,6 +40,8 @@ import com.yifu.cloud.plus.v1.yifu.common.core.constant.enums.MenuTypeEnum;
import com.yifu.cloud.plus.v1.yifu.common.core.exception.ErrorCodes;
import com.yifu.cloud.plus.v1.yifu.common.core.util.MsgUtils;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import com.yifu.cloud.plus.v1.yifu.common.ldap.entity.PersonVo;
import com.yifu.cloud.plus.v1.yifu.common.ldap.util.LdapUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
......@@ -77,6 +80,8 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
private final SysUserPostMapper sysUserPostMapper;
private final LdapUtil ldapUtil;
/**
* 保存用户信息
* @param userDto DTO 对象
......@@ -393,4 +398,39 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
return R.ok(saveUser(userDto));
}
/**
* 从ldap中获取用户
* @return R
*/
@Override
public R<Boolean> updateByLdap() {
List<PersonVo> list = ldapUtil.getAllPersons();
if (null != list) {
List<SysUser> listUser = this.list(Wrappers.<SysUser>query().lambda().eq(SysUser::getDelFlag, CommonConstants.STATUS_NORMAL));
List<String> list1 = listUser.stream().map(SysUser::getUsername).collect(Collectors.toList());
Map<String, String> map = listUser.stream().collect(HashMap::new, (m, v) -> m.put(v.getUsername(), v.getUserId()), HashMap::putAll);
for (PersonVo personVo : list) {
if (list1.stream().anyMatch(e -> e.equals(personVo.getUid()))) {
SysUser sysUser = this.getById(map.get(personVo.getUid()));
sysUser.setUsername(personVo.getUid());
sysUser.setPhone(personVo.getTelephoneNumber());
sysUser.setEmail(personVo.getEmail());
sysUser.setNickname(personVo.getPersonName());
sysUser.setPassword(personVo.getPassword());
this.updateById(sysUser);
} else {
SysUser sysUser = new SysUser();
sysUser.setUsername(personVo.getUid());
sysUser.setPhone(personVo.getTelephoneNumber());
sysUser.setEmail(personVo.getEmail());
sysUser.setNickname(personVo.getPersonName());
sysUser.setDelFlag(CommonConstants.STATUS_NORMAL);
sysUser.setPassword(personVo.getPassword());
sysUser.setUserGroup(CommonConstants.EMPTY_STRING);
this.save(sysUser);
}
}
}
return R.ok();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment