亚威组织导出接口

This commit is contained in:
huangweixiong 2022-10-15 00:21:28 +08:00
parent 1af6d798ed
commit 79b86cb96c
2 changed files with 92 additions and 0 deletions

View File

@ -2,9 +2,13 @@ package io.renren.modules.gateway.controller;
import cn.hutool.core.io.FileUtil;
import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.excel.annotation.ExcelProperty;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import io.renren.common.utils.ExcelUtils;
import io.renren.common.utils.JhlDAPTool;
import io.renren.modules.gateway.dao.ApiCountHistoryDao;
import io.renren.modules.gateway.entity.ApiCountHistoryEntity;
import io.renren.modules.gateway.service.MonitorServiceV2;
@ -18,8 +22,11 @@ import io.renren.modules.resource.entity.AttrEntity;
import io.renren.modules.resource.entity.ResourceEntity;
import io.renren.modules.sys.dao.SysDeptDao;
import io.renren.modules.sys.entity.SysDeptEntity;
import io.renren.modules.sys.enums.JhDeptEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Builder;
import lombok.Data;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
@ -33,6 +40,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.naming.NamingException;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.LdapContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@ -79,6 +89,9 @@ public class MonitorControllerV2 {
@Autowired
private ApiCountHistoryDao apiCountHistoryDao;
@Autowired
private JhlDAPTool jhlDAPTool;
@Autowired
private SysNoticeServiceImpl sysNoticeService;
@Autowired
@ -351,4 +364,45 @@ public class MonitorControllerV2 {
return monitorService.fileCode(code);
}
@Data
static public class Org{
@ExcelProperty("名称")
String orgName;
}
@GetMapping("/exportYaweiOrg")
public void exportYaweiOrg(HttpServletResponse response) throws Exception {
List<Map> deptList = JhDeptEnum.getAllToList();
LinkedList<Org> all = new LinkedList<>();
for (Map map : deptList) {
String key = (String) map.keySet().iterator().next();
if (StringUtils.isBlank(key)) {
continue;
}
String filter = "(&(objectClass=organizationalUnit))";
List<SearchResult> results = jhlDAPTool.query("OU=" + key, filter, new String[]{"distinguishedName"});
results.stream().forEach(relult -> {
try {
String origStr = (String) relult.getAttributes().get("distinguishedName").get();
String[] orgs = origStr.split(",");
StringBuilder builder = new StringBuilder();
for (int i = orgs.length -3-1; i >= 0; i--) {
builder.append("/" + orgs[i].replace("OU=",""));
}
Org org = new Org();
org.setOrgName(builder.toString());
all.add(org);
} catch (NamingException e) {
log.error("ldap解析错误:", e);
return;
}
});
}
// ExcelUtil.getWriter().write(all, false);
ExcelUtils.exportExcelToTarget(response, "org","all", all, Org.class);
}
}

View File

@ -1,5 +1,6 @@
package io.renren.common.utils;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
@ -16,6 +17,7 @@ import java.util.*;
* 通过该类完成金宏系统中 部门和用户名信息的提取
*/
@Component
@Log4j2
public class JhlDAPTool {
private String URL = "ldap://15.72.158.50:389/";
private String BASEDN = "DC=qd,DC=gov,DC=cn";//指定从哪里开始搜索从上往下推
@ -299,4 +301,40 @@ public class JhlDAPTool {
return (cookie == null) ? new byte[0] : cookie;
}
public List<SearchResult> query(String name, String filter, String[] returnAttr) {
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
env.put(Context.PROVIDER_URL, URL +BASEDN);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, username); // 管理员
env.put(Context.SECURITY_CREDENTIALS, password); // 管理员密码
env.put("java.naming.ldap.attributes.binary","objectGUID");
LinkedList<SearchResult> all = new LinkedList<>();
try {
InitialLdapContext ctx = new InitialLdapContext(env, connCtls);
byte[] cookie = null;
SearchControls searchControls = new SearchControls();//搜索控件
if (returnAttr != null) {
searchControls.setReturningAttributes(returnAttr);
}
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);//搜索范围,1只搜索指定命名对象的一个级别这是缺省值2以指定命名对象为根结点的整棵树 SearchControls.SUBTREE_SCOPE
ctx.setRequestControls(new Control[]{new PagedResultsControl(50, Control.CRITICAL)});
NamingEnumeration<SearchResult> answer = ctx.search(name, filter, searchControls);
while (answer.hasMore()) {
SearchResult result = (SearchResult) answer.next();
all.add(result);
}
} catch (Exception e) {
log.error("ldap错误", e);
return null;
}finally {
closeContext();
}
return all;
}
}