LDAP

1.Ldap_Connection.java

import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPException;
import com.novell.ldap.LDAPSocketFactory;

@SuppressWarnings("unused")
public class Ldap_Connection {
    @SuppressWarnings("deprecation")
    public static LDAPConnection getLdapConnection(String dn,String pwd)throws LDAPException {
               LDAPConnection lc = new LDAPConnection();
        lc.connect("10.100.68.254", 389);//测试环境
        lc.bind(LDAPConnection.LDAP_V3, dn,pwd);
        System.out.println("连接成功!");
        return lc;
    }
}

2.Ldap_Main.java

import org.apache.commons.lang3.StringUtils;
import com.novell.ldap.LDAPConnection;
public class Ldap_Main {
    public static void main(String[] args) {
        String mail="v-zhaojincui";
        Ldap_Main.login(mail)
    }

    @SuppressWarnings("unused")
    public static String login(String mail){
        String searchemail = "";
        String rs="";
        if(null==mail || "".equals(mail)){
            //return "邮箱不能为空";
            return "3";
        }else{
            mail=mail +"@creditease.cn";
        }
        try {
            LDAPConnection ldapConnection = Ldap_Connection.getLdapConnection("it.omptest","12(039)&JG");//测试           searchemail=Ldap_Search.Ldap_Search1(ldapConnection,mail);
        } catch (Exception e) {
            e.printStackTrace();
            //return "连接与服务器异常";
            return "0";
        }

        try {
            if(StringUtils.isBlank(searchemail)){
                //return "未找到用户"+mail;
                return "2";
            }else{
                return "1";
            }

        } catch (Exception e) {
            e.printStackTrace();
//          System.out.println( "登录失败");
            //return "登陆失败";
            return "0";
        }
    }   
}

3.Ldap_Search.java

import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import com.novell.ldap.LDAPAttribute;
import com.novell.ldap.LDAPAttributeSet;
import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPEntry;
import com.novell.ldap.LDAPException;
import com.novell.ldap.LDAPSearchResults;
/**
 * LDAPConnection.SCOPE_ONE // 只查找基节点第一层的子节点 LDAPConnection.SCOPE_BASE // 只查找基节点
 * LDAPConnection.SCOPE_SUB // 查找基节点下面的所有子节点
 **/

public class Ldap_Search {
    private static String searchBase = "ou=宜信,ou=HABROOT,dc=creditease,dc=corp";
    private static int searchScope =  LDAPConnection.SCOPE_SUB;
    private static  String filter = "(|(objectclass=person)(objectclass=user)(objectclass=organizationalPerson))";

    public static Map uidMap = new HashMap();

    public static String Ldap_Search1(LDAPConnection lc,String mail) throws LDAPException {
        filter = "(&(userPrincipalName="+mail+")(|(objectclass=person)(objectclass=user)(objectclass=organizationalPerson)))";
        LDAPSearchResults rs = lc.search(searchBase,searchScope,filter, null,false);

        String searchemail="";
        while (rs.hasMore()) {
            LDAPEntry entry = rs.next();
            if (null != entry.getAttribute("userPrincipalName")) {
                searchemail = entry.getAttribute("userPrincipalName").getStringValue();
                String dn = entry.getDN();
//              System.out.println("cn="+searchemail+" ,dn="+dn);
            }
        }
        lc.disconnect();
        return searchemail;
    }
}

httpclient-4.2.3.jar
httpclient-cache-4.2.3.jar
httpcore-4.2.2.jar
httpmime-4.2.3.jar
jldap-4.3.jar

你可能感兴趣的:(java)