diff --git a/renren-admin/src/main/java/io/renren/modules/gateway/controller/MonitorControllerV2.java b/renren-admin/src/main/java/io/renren/modules/gateway/controller/MonitorControllerV2.java index 2ae99c03..1e9c481e 100644 --- a/renren-admin/src/main/java/io/renren/modules/gateway/controller/MonitorControllerV2.java +++ b/renren-admin/src/main/java/io/renren/modules/gateway/controller/MonitorControllerV2.java @@ -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 deptList = JhDeptEnum.getAllToList(); + LinkedList all = new LinkedList<>(); + for (Map map : deptList) { + String key = (String) map.keySet().iterator().next(); + if (StringUtils.isBlank(key)) { + continue; + } + String filter = "(&(objectClass=organizationalUnit))"; + List 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); + } + } diff --git a/renren-common/src/main/java/io/renren/common/utils/JhlDAPTool.java b/renren-common/src/main/java/io/renren/common/utils/JhlDAPTool.java index 5d44667c..f8e0158d 100644 --- a/renren-common/src/main/java/io/renren/common/utils/JhlDAPTool.java +++ b/renren-common/src/main/java/io/renren/common/utils/JhlDAPTool.java @@ -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 query(String name, String filter, String[] returnAttr) { + + Hashtable env = new Hashtable(); + 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 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 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; + } + }