AD域控接口对接

package com.thinkgem.jeesite.test;

import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.*;
import javax.naming.ldap.*;
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.Hashtable;

public class TestPageAd {

    public static void main(String[] args) {
        String host="1123";
        String post="456";
        String username="123@test.com";
        String password="123";
        Hashtable env = new Hashtable();

        String company = "";
        Hashtable<String, String> HashEnv = new Hashtable<String, String>();
        HashEnv.put(Context.SECURITY_AUTHENTICATION, "simple"); // LDAP访问安全级别(none,simple,strong)
        HashEnv.put(Context.SECURITY_PRINCIPAL, username); //AD的用户名
        HashEnv.put(Context.SECURITY_CREDENTIALS, password); //AD的密码
        HashEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); // LDAP工厂类
        HashEnv.put("com.sun.jndi.ldap.connect.timeout", "3000");//连接超时设置为3秒
        HashEnv.put(Context.PROVIDER_URL, "ldap://" + host + ":" + post);// 默认端口389

        String execSQL="";
        String attributeID = "";
        String displayName = "";
        String description = "";
        String mobile = "";
        String mail = "";
        String sAMAccountName = "";
        String userPrincipalName = "";
        String companyName = "";
        String department = "";
        String title = "";
        LdapContext ctx = null;
        int pageSize = 15;
        int pageNo = 1;
        int currentPage = 0;
        int total = 0;
        try {
            // 初始化连接
            ctx = new InitialLdapContext(HashEnv, null);
            // 设置分页大小
            ctx.setRequestControls(new Control[]{new PagedResultsControl(pageSize, Control.CRITICAL)});
            // 查询过滤器
            String filter = "userprincipalname=*@*";
            String searchFilter = "objectClass=user";
            String searchBase = "DC=test,DC=com";
            SearchControls searchControls = new SearchControls();
            searchControls.setCountLimit(pageSize);
            // 设置查询该树和子树的数据
            searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
            // 设置返回值
            String returnedAtts[] = {
                    "url", "whenChanged", "employeeID", "name", "userPrincipalName",
                    "physicalDeliveryOfficeName", "Description", "departmentNumber", "telephoneNumber",
                    "homePhone", "mobile", "title","mail","Company","Department", "sAMAccountName",
                    "mail"}; //定制返回属性
            searchControls.setReturningAttributes(returnedAtts);
            byte[] cookie = null;
            int totalResults = 0;// Specify the attributes to return
            int rows = 0;
            do {
                // 查询
                NamingEnumeration results = ctx.search(searchBase, searchFilter, searchControls);
                while (results != null && results.hasMoreElements()) {
                    SearchResult sr = (SearchResult) results.next();
                    total++;
                    if (currentPage == pageNo) {
                        // *****需要返回的页面的数据在这里操作*****

                    }


                    String dn = sr.getName();
                    System.out.println("<<<::[" + dn+"]::>>>>");


                    String match = dn.split("CN=")[1].split(",")[0];//返回格式一般是CN=ptyh,OU=专卖
                    System.out.println(match);
                    Attributes Attrs = sr.getAttributes();// 得到符合条件的属性集
                    if (Attrs != null) {
                        try {
                            for (NamingEnumeration ne = Attrs.getAll(); ne.hasMore(); ) {
                                Attribute Attr = (Attribute) ne.next();// 得到下一个属性
                                attributeID = Attr.getID().toString();
                                System.out.println(" AttributeID=属性名:" + attributeID);

                                // 读取属性值
                                for (NamingEnumeration e = Attr.getAll(); e.hasMore(); totalResults++) {
                                    company = e.next().toString();
                                    System.out.println("    AttributeValues=属性值:" + company);
                                    if(attributeID.equals("name")){
                                        displayName = company;
                                    } else if(attributeID.equals("mobile")){
                                        mobile = company;
                                    }else if(attributeID.equals("userPrincipalName")){
                                        userPrincipalName = company;
                                    }else if(attributeID.equals("sAMAccountName")){
                                        sAMAccountName = company;
                                    }else if(attributeID.equals("company")){
                                        companyName = company;
                                    }else if(attributeID.equals("department")){
                                        department = company;
                                    }else if(attributeID.equals("description")){
                                        description = company;
                                    }else if(attributeID.equals( "title")){
                                        title = company;
                                    }
                                }
                            }
                        } catch (NamingException e) {
                            System.err.println("Throw Exception : " + e);
                        }
                    }

                    //INSERT INTO `crm_mycomm`.`crm_def_customer` (`id`, `industry_id`, `field1`, `field2`, `field3`, `field4`, `field5`, `field6`, `field7`, `field8`, `field9`, `field10`, `field11`, `field12`, `field13`, `field14`, `field15`, `field16`, `field17`, `field18`, `field19`, `field20`, `field21`, `field22`, `field23`, `field24`, `field25`, `field26`, `field27`, `field28`, `field29`, `field30`, `field31`, `field32`, `field33`, `field34`, `field35`, `field36`, `field37`, `field38`, `field39`, `field40`, `field41`, `field42`, `field43`, `field44`, `field45`, `field46`, `field47`, `field48`, `field49`, `field50`, `field51`, `field52`, `field53`, `field54`, `field55`, `field56`, `field57`, `field58`, `field59`, `field60`, `create_by`, `create_date`, `update_by`, `update_date`, `remarks`, `del_flag`, `province`, `city`, `county`, `address`, `sex`, `tel_phone`, `mob_phone`, `customer_status`, `customer_name`, `cust_no`, `weixin_id`, `webchat_id`, `company_name`, `user_type`, `card_type`, `card_number`, `email`, `qq`, `webchat`, `zip`, `job`) VALUES ('28eb6e092d804e2bbd31ab3dbc194e5a', '4a18c62a03bb4207ac701afb62c49c03', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, '1', '2020-01-13 09:36:55', '1', '2020-01-13 09:36:55', NULL, b'0', NULL, NULL, NULL, NULL, NULL, '18518298602', '18201135602', NULL, '景铁', NULL, NULL, NULL, 'cti', NULL, NULL, NULL, 'jingtie@mycommcrm.cn', '项目', '工程师', NULL, NULL);
                    //execSQL = "insert into crm_def_customer(id,industry_id, customer_name,mob_phone,email,cust_no,company_name,field1,field2,field3) VALUES (UUID(),'1','"+displayName+"','"+mobile+"','"+userPrincipalName+"','"+sAMAccountName+"','"+companyName+"','"+department+"','"+description+"','"+title+"')";
                    execSQL += "INSERT INTO  crm_def_customer (id, industry_id, field1, field2, field3, field4, field5, field6, field7, field8, field9, field10, field11, field12, field13, field14, field15, field16, field17, field18, field19, field20, field21, field22, field23, field24, field25, field26, field27, field28, field29, field30, field31, field32, field33, field34, field35, field36, field37, field38, field39, field40, field41, field42, field43, field44, field45, field46, field47, field48, field49, field50, field51, field52, field53, field54, field55, field56, field57, field58, field59, field60, create_by, create_date, update_by, update_date, remarks, del_flag, province, city, county, address, sex, tel_phone, mob_phone, customer_status, customer_name, cust_no, weixin_id, webchat_id, company_name, user_type, card_type, card_number, email, qq, webchat, zip, job) VALUES (UUID(), '4a18c62a03bb4207ac701afb62c49c03', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, '1', '2020-01-15 10:30:01', '1', '2020-01-15 10:30:01', NULL, b'0', NULL, NULL, NULL, NULL, NULL, '"+mobile+"', '"+mobile+"', NULL, '"+displayName+"', NULL, NULL, NULL, '"+description+"', NULL, NULL, '"+sAMAccountName+"', '"+userPrincipalName+"', '"+department+"', '"+title+"', NULL, NULL);\r\n";
                    System.out.println("    AttributeValues=插表语句:" + execSQL);


                }
                Control[] controls = ctx.getResponseControls();
                if (controls != null) {
                    for (int i = 0; i < controls.length; i++) {
                        if (controls[i] instanceof PagedResultsResponseControl) {
                            PagedResultsResponseControl prrc = (PagedResultsResponseControl) controls[i];
                            cookie = prrc.getCookie();
                        }
                    }
                }
                // 将cookie信息设置进去,下次查询的时候会在本次查询的基础上向后查询,如果没有cookie了,就说明没有数据了
                ctx.setRequestControls(new Control[]{new PagedResultsControl(pageSize, cookie, Control.CRITICAL)});
                currentPage++;
            } while (cookie != null);

        } catch (Exception e) {
            System.out.println("LDAP获取信息失败:"+ e);
        } finally {
            if (null != ctx) {
                try {
                    ctx.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }


}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值