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

📄 ldapholder.java

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

    }


  /*add by songwei
  *得到mail信息
 */
  public String getMail(String CN) throws LDAPException{
       String mail="";
       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("mail")){
                         enumVals = anAttr.getByteValues();
                           if (enumVals != null) {
                                 while ( enumVals.hasMoreElements() ) {
                                 b_cert=(byte[])enumVals.nextElement();
                                 try{
                                      mail = new String(b_cert, "ISO8859_1");
                                    }catch(UnsupportedEncodingException un) {un.printStackTrace();}
                                  }
                           }
                     }
                 }
             }
             return mail;
       }
       return null;

    }





  public byte[] getCert(String CN) throws LDAPException{
    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("cn")){
                       enumVals = anAttr.getByteValues();
                         if (enumVals != null) {
                               while ( enumVals.hasMoreElements() ) {
                               b_cert=(byte[])enumVals.nextElement();
                               }
                         }
                   }
                   if (attrName.equalsIgnoreCase("usercertificate;binary")){
                         enumVals = anAttr.getByteValues();
                         if (enumVals != null) {
                               while ( enumVals.hasMoreElements() ) {
                                     certb=(byte[])enumVals.nextElement();
                                     return certb;
                               }
                         }
                   }
            }
           }
     }
     return null;

  }

  public Vector getCert(String CN,String s) throws LDAPException{
       System.out.println("进入getCert方法");
       Vector result =new Vector();
        Vector certName=new Vector();
        Vector certContent=new Vector();
        byte[] certb=null;
        LDAPSearchResults res=null;
        res =existCN(CN);
        LDAPEntry findEntry = null;
        //LogOutput.sysLog("条目是:"+res);
        if (res!=null){
          System.out.println("res不为空");
          try{
          while ( res.hasMoreElements() ) {
              /* Next directory entry */
            try {
              findEntry = res.next();
            }
            catch ( LDAPReferralException e ) {
              LDAPUrl refUrls[] = e.getURLs();
              for (int i=0; i<refUrls.length; i++) {
              }
              continue;
            }
            catch ( LDAPException e ) {
              continue;
            }
            //LogOutput.sysLog( findEntry.getDN() );
              /* Get the attributes of the entry */
            String cname=findEntry.getDN();
            cname=cname.substring(cname.indexOf("=")+1,cname.indexOf(","));
            certName.addElement(cname);
            LDAPAttributeSet findAttrs = findEntry.getAttributeSet();
            Enumeration enumAttrs = findAttrs.getAttributes();
            //LogOutput.sysLog( "\tAttributes: " );
              /* Loop on attributes */
            while ( enumAttrs.hasMoreElements() ) {
              LDAPAttribute anAttr =(LDAPAttribute)enumAttrs.nextElement();
              String attrName = anAttr.getName();
              //LogOutput.sysLog( "\t\t" + attrName );
                    /* Loop on values for this attribute */
              Enumeration enumVals ;
              if (attrName.equalsIgnoreCase("usercertificate;binary")){
                enumVals = anAttr.getByteValues();
                if (enumVals != null) {
                  while ( enumVals.hasMoreElements() ) {
                    String temp="";
                    certb=(byte[])enumVals.nextElement();
                    temp=new String(certb);
                    if (temp.equals(""))
                      certContent.addElement("null");
                    else
                      certContent.addElement(temp);
                  }
                }
                else
                  System.out.println("There is no certificate in this entry");
              }
            }
          }
          result.addElement(certName);
          result.addElement(certContent);
        }catch(Exception ex){
          ex.printStackTrace();
        }
        return result;
        }else{
          System.out.println("Entry has no element1111111");
          return null;
        }
  }


  public String getHost() {
    return host;
  }
  public void setPort(int newPort) {
    port = newPort;
  }
  public int getPort() {
    return port;
  }
  public void setUserDn(String newUserDn) {
    userDn= newUserDn;
  }
  public String getUserDn() {
    return userDn;
  }
  public void setPassword(String newPassword) {
    password = newPassword;
  }
  public String getPassword() {
    return password;
  }
  public void setLDAPCon(netscape.ldap.LDAPConnection newLDAPCon) {
    LDAPCon = newLDAPCon;
  }
  public netscape.ldap.LDAPConnection getLDAPCon() {
    return LDAPCon;
  }
  public String checkCert(String cn){
    System.out.println("进入checkCert");
    try{

      byte[] b = getCert(cn);
      if (b!=null&&!cn.equals("crl")){
         System.out.println("开始证书工厂");
          CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509","IBMJCE");
         System.out.println("证书工厂完毕");
          ByteArrayInputStream bais = new ByteArrayInputStream(b);
          X509Certificate cert=(X509Certificate)certificateFactory.generateCertificate(bais);
          Date date=cert.getNotAfter();
          Date nowDate=new Date();
          bais.close();

          ByteArrayInputStream baiscrl = new ByteArrayInputStream(getCert("crl"));
          CertificateFactory cf = CertificateFactory.getInstance("X.509");
          X509CRL crl = (X509CRL)cf.generateCRL(baiscrl);
          baiscrl.close();
          //1:有效
          if (!crl.isRevoked(cert)&&nowDate.before(date)){
            return "1";
          }
          //2:过期
          if(!nowDate.before(date)){
            return "2";
          }
          //3:吊销
          if(crl.isRevoked(cert)){
            return "3";
          }
      }
    }
    catch(Exception e){
       e.printStackTrace();
    }
    return "4";
  }





  public void setSearchBase(String searchBase) {
    this.searchBase = searchBase;
  }
  public String getSearchBase() {
    return searchBase;
  }
  public byte[] getB_cert() {
    return b_cert;
  }




  public static void main(String args[]){

  LdapHolder ldapholder=null;
  String cn="40083168341086355152418838774710332999";
  Vector vv=new Vector();
  try{
    ldapholder=new LdapHolder("cn=Directory Manager","12345678","210.25.9.101",389,"OU=edi,O=MOFTEC,C=CN");
    ldapholder.getConnectionA();
    vv=ldapholder.getObjectClass(cn);
    System.out.println("11=="+vv.get(0).toString());
    System.out.println("22=="+vv.get(1).toString());
    System.out.println("33=="+vv.get(2).toString());
    System.out.println("44=="+vv.get(3).toString());
   String mail="";
   mail=ldapholder.getMail(cn);
   System.out.println("mail==="+mail);
      byte[] bb=ldapholder.getCert(cn);
      BASE64Encoder base64Encoder= new BASE64Encoder();
      String ss=base64Encoder.encode(bb);
  /*
      String c=ldapholder.checkCert(cn);
    if(c.equals("1"))
    {
      System.out.println("证书有效!!");
    }
    else if(c.equals("2"))
    {
      System.out.println("证书过期!!");
    }
    else if(c.equals("3"))
    {
      System.out.println("证书无效!!");
    }
  */
   // System.out.println("cn====="+v1.get(0).toString());
    System.out.println("cert is====="+ss+"======");
    }catch( LDAPReferralException e ) {

    }catch( Exception e ) {
      System.out.println("XXXXXXXXXXXXXXXXXXXXXX");
      e.printStackTrace();
    }finally{
      try{
          ldapholder.closeCon();
          System.out.println("ZZZZZZZZZZZZZZZZZZZZZZZZzz");
      }catch (LDAPException e){System.out.println(e.getMessage());}
    }
 }

}

⌨️ 快捷键说明

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