⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ldapholder.java

📁 对ldap服务的基本操作
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package ldaptest;

import java.util.*;
import java.io.*;
import netscape.ldap.*;
import java.security.cert.*;
//import com.ibm.security.x509.X500Name;
import sun.misc.HexDumpEncoder;
import java.io.*;
import java.security.*;
import java.security.cert.*;
import java.security.spec.*;
import com.ibm.misc.BASE64Encoder;

public class LdapHolder {

  private String host;
  private int port;
  private String userDn;
  private String password;
  private netscape.ldap.LDAPConnection LDAPCon;
  private String  searchBase ;
  public byte[] b_cert;

    /**
     * 当连接LDAP,需要添加条目的时候使用
     * @param   host    主机地址
     * @param   port    端口
     * @param   searchBase  搜索目录
     * @param   userDn    LADP 管理员DN
     * @param   password    管理员口令
     */

  public LdapHolder(String userDn,String password,String host,int port, String  searchBase) {
    if (Security.getProvider("IBMJCE") == null) {
      try {
        Security.addProvider
            ( (Provider) Class.forName("com.ibm.crypto.provider.IBMJCE").
             newInstance());
      }
      catch (Exception ex) {
        System.out.println("Cannot install provider: " + ex.getMessage());
      }
    }
      this.userDn=userDn;
      this.password=password;
      this.searchBase=searchBase;
      this.host=host;
      this.port=port;
//      try {
//        getConnectionA();
//      }
//      catch (Exception ex) {
//        ex.printStackTrace();
//      }

  }

    /**
     * 当连接LDAP,不需要添加条目的时候使用
     * @param   host    主机地址
     * @param   port    端口
     * @param   searchBase  搜索目录
     */

  public LdapHolder(String host,int port,String  searchBase) {
      this.host=host;
      this.port=port;
      this.searchBase=searchBase;

  }

  public LdapHolder() throws LDAPException {

    this.userDn="cn=Directory Manager";
    this.password="12345678";
    this.searchBase="OU=EDI,O=MOFTEC,C=CN";
    this.host="211.88.8.189";
    this.port=389;

    //getConnectionA();//delete 20021125
  }

  public LDAPConnection getConnectionA()  throws LDAPException {
    System.out.println("开始连接");
    LDAPCon=new LDAPConnection();
    LDAPCon.connect(host,port);
    LDAPCon.authenticate(userDn,password);
    System.out.println("返回连接");
    return LDAPCon;
  }

  public LDAPConnection getConnection() {
        LDAPCon=new LDAPConnection();
        try{
          LDAPCon.connect(host,port);
          System.out.println("连接成功");
          return LDAPCon;
        }catch ( LDAPException e){System.out.println("连接失败");}
        return null;
  }

  public void closeCon() throws LDAPException{
      LDAPCon.disconnect();
  }

  public boolean connectionTest() {
        LDAPCon=new LDAPConnection();
        try{
          LDAPCon.connect(host,port);
          LDAPCon.authenticate(userDn,password);
          return true;
        }catch ( LDAPException e){
        }finally{
          try{
            LDAPCon.disconnect();
          }catch (LDAPException e){}
        }
        return false;
  }

     /**
     * 按照CN,检查是否在目录服务器上存在词条目
     * @param   String CN   要查找CN
     *
     */
    private LDAPSearchResults existCN(String cn)  throws LDAPException{
        System.out.println("进入exittCN方法");
        LDAPSearchResults ret=null;
        //HexDumpEncoder h=new HexDumpEncoder();
        //LogOutput.sysLog(h.encode(cn.getBytes()));//*/
        String MY_FILTER="cn="+cn;
        System.out.println("cn001===="+MY_FILTER);
        //LogOutput.sysLog(h.encode(MY_FILTER.getBytes()));
        getConnection();
        LDAPSearchConstraints cons = LDAPCon.getSearchConstraints();
        cons.setBatchSize( 1 );
        ret = LDAPCon.search( this.searchBase,LDAPConnection.SCOPE_SUB,MY_FILTER,null,false,cons );
        if (ret.hasMoreElements() )
        {
            return ret;
        }else
         {
             return null;
          }
}

    public boolean existEntry(String CN) throws LDAPException {
    if (existCN(CN)!=null)
        return true;
    else
        return false;
  }

  /**
  * 向LDAP中添加组织单元OU
  * @param  entry 目录条目类
  */
  public boolean addOU(String OUName) throws LDAPException {
      String objectclass_values[] = { "top","organizationalunit"};
      LDAPAttributeSet attrs = new LDAPAttributeSet();
      LDAPAttribute attr = new LDAPAttribute( "objectclass" );
      for( int i = 0; i < objectclass_values.length; i++ ) {
              attr.addValue( objectclass_values[i] );
      }
      attrs.add( attr );
      LDAPEntry myEntry = new LDAPEntry( "ou="+OUName+",o=moftec,c=cn", attrs );
//      getConnectionA();
      LDAPCon.add(myEntry);
      return true;
  }
  /**
  * 向LDAP中添加条目
  * @param  entry 目录条目类
  */
  public boolean addEntry(Object entry) throws LDAPException{
      String objectclass_values[] = { "top","person","organizationalPerson","inetOrgPerson" };
      LDAPAttributeSet attrs = new LDAPAttributeSet();
      //添加objectclass
      LDAPAttribute attr = new LDAPAttribute( "objectclass" );
      for( int i = 0; i < objectclass_values.length; i++ ) {
              attr.addValue( objectclass_values[i] );
      }
      attrs.add( attr );
      //添加证书
      byte certb[];
      LdapEntry ldapentry=(LdapEntry)entry;
      certb=ldapentry.getCert();
      LDAPAttribute certAttrs= new LDAPAttribute("usercertificate;binary",certb);
      attrs.add(certAttrs);
      //添加CN
      attr=new LDAPAttribute( "cn" );
      attr.addValue(ldapentry.getCN());
      attrs.add( attr );
      //添加SN
      attr=new LDAPAttribute( "sn" );
      attr.addValue(ldapentry.getCN());
      attrs.add( attr );
      //添加EMIAL
//      if (ldapentry.getEmailAddress()!=null&&!ldapentry.getEmailAddress().equals("")){
//        attr = new LDAPAttribute("mail",ldapentry.getEmailAddress());
//        attrs.add(attr);
//      }
      //添加CN
      //certAttrs= new LDAPAttribute("cn","1234567890");
      //attrs.add(certAttrs);
      LDAPEntry myEntry = new LDAPEntry( ldapentry.getDN(), attrs );
      LDAPCon.setOption(LDAPv2.REFERRALS,Boolean.valueOf(false));
      LDAPCon.add(myEntry);
      return true;
  }

 /**
  * 通过通用名删除条目
  * @param  entry 条目类
  */
  public boolean delEntry (String CN) throws LDAPException{
    try{
//    getConnectionA();
    //LogOutput.sysLog("cn="+CN+","+this.searchBase);
    //LogOutput.sysLog("cn="+CN+","+this.searchBase);
    //LDAPSearchConstraints c=new LDAPSearchConstraints();
    LDAPCon.delete( "cn="+CN+","+this.searchBase);
}catch(LDAPException e){
        e.printStackTrace();
        throw e;
        }

    return  true;
  }
 /**
  * 通过通用名移动条目
  * @param entry 条目类
  */
  public boolean moveEntry(String CN,String sourceDN,String distinationDN) throws LDAPException{
//    getConnectionA();
    LdapEntry entry=null;
    setSearchBase(sourceDN);
    entry=getEntry(CN,sourceDN);
    entry.setDNBase(distinationDN);
    setSearchBase(distinationDN);
    addEntry(entry);
    setSearchBase(sourceDN);
    delEntry(CN);
    return true;
  }

  public LdapEntry getEntry(String dnanme,String sourceDN)throws LDAPException{
    //entry= new LdapEntry(dnanme,getCert(dnanme),sourceDN);
   System.out.println("进入getEntry方法");
    byte[] certb=null;
    System.out.println("进入getEntry方法");
    certb=getCert(dnanme);
    System.out.println("证书信息====="+certb+"======");
    if (certb==null)
    {

      return null;
    }
      else
      {
       System.out.println("返回查询结果");
          return new LdapEntry(dnanme,sourceDN,certb,null);

     }
  }

  public void setHost(String newHost) {
    host = newHost;
  }

/*add by songwei
  *得到objectclass中的信息
 */
  public Vector getObjectClass(String CN) throws LDAPException{
       Vector rel=new Vector();
       byte[] certb=null;
       LDAPSearchResults res=null;
       res =existCN(CN);
       LDAPEntry findEntry = null;
       if (res!=null){
           while ( res.hasMoreElements() ) {
              try {
                       findEntry = res.next();
               } catch ( LDAPReferralException e ) {
                       LDAPUrl refUrls[] = e.getURLs();
                       for (int i=0; i<refUrls.length; i++) {
                }
               continue;
               } catch ( LDAPException e ) {
                       continue;
               }

             /* Get the attributes of the entry */
             LDAPAttributeSet findAttrs = findEntry.getAttributeSet();
             Enumeration enumAttrs = findAttrs.getAttributes();

             /* Loop on attributes */
             while ( enumAttrs.hasMoreElements() ) {
                     LDAPAttribute anAttr =(LDAPAttribute)enumAttrs.nextElement();
                     String attrName = anAttr.getName();

                     /* Loop on values for this attribute */
                     Enumeration enumVals ;
                     HexDumpEncoder h=new HexDumpEncoder();
                     if (attrName.equalsIgnoreCase("objectclass")){
                         enumVals = anAttr.getByteValues();
                           if (enumVals != null) {
                                 int i=0;
                                 while ( enumVals.hasMoreElements() ) {
                                 b_cert=(byte[])enumVals.nextElement();
                                 try{
                                       String  strOut = new String(b_cert, "ISO8859_1");
                                       System.out.println(i+"==="+strOut);
                                       rel.add(i,strOut);
                                    }catch(UnsupportedEncodingException un) {un.printStackTrace();}
                                   i++;
                                 }
                           }
                     }
                 }
             }
             return rel;
       }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -