當前位置: 首頁>>代碼示例>>Java>>正文


Java X509Certificate.getSubjectAlternativeNames方法代碼示例

本文整理匯總了Java中java.security.cert.X509Certificate.getSubjectAlternativeNames方法的典型用法代碼示例。如果您正苦於以下問題:Java X509Certificate.getSubjectAlternativeNames方法的具體用法?Java X509Certificate.getSubjectAlternativeNames怎麽用?Java X509Certificate.getSubjectAlternativeNames使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.security.cert.X509Certificate的用法示例。


在下文中一共展示了X509Certificate.getSubjectAlternativeNames方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: buildCertAppliesToString

import java.security.cert.X509Certificate; //導入方法依賴的package包/類
public static String buildCertAppliesToString(X509Certificate cert) {
    List<String> elements = new ArrayList<>();
    try {
        Collection<List<?>> altNames = cert.getSubjectAlternativeNames();
        if (altNames != null) {
            for (List<?> altName : altNames) {
                Integer altNameType = (Integer) altName.get(0);
                if (altNameType != 2 && altNameType != 7) // dns or ip
                    continue;
                elements.add((String) altName.get(1));
            }
        }
    } catch (CertificateParsingException ignored) {
    }

    if (elements.size() == 0)
        return "none";
    return TextUtils.join(",", elements.toArray());
}
 
開發者ID:MCMrARM,項目名稱:revolution-irc,代碼行數:20,代碼來源:ServerCertificateManager.java

示例2: resolvePrincipalInternal

import java.security.cert.X509Certificate; //導入方法依賴的package包/類
/**
 * Retrieves Subject Alternative Name UPN extension as a principal id String.
 *
 * @param certificate X.509 certificate credential.
 *
 * @return Resolved principal ID or null if no SAN UPN extension is available in provided certificate.
 *
 * @see AbstractX509PrincipalResolver#resolvePrincipalInternal(java.security.cert.X509Certificate)
 * @see java.security.cert.X509Certificate#getSubjectAlternativeNames()
 */
@Override
protected String resolvePrincipalInternal(final X509Certificate certificate) {
    logger.debug("Resolving principal from Subject Alternative Name UPN for {}", certificate);
    try {
        final Collection<List<?>> subjectAltNames = certificate.getSubjectAlternativeNames();
        if (subjectAltNames != null) {
            for (final List<?> sanItem : subjectAltNames) {
                final ASN1Sequence seq = getAltnameSequence(sanItem);
                final String upnString = getUPNStringFromSequence(seq);
                if (upnString != null) {
                    return upnString;
                }
            }
        }
    } catch (final CertificateParsingException e) {
        logger.error("Error is encountered while trying to retrieve subject alternative names collection from certificate", e);
        logger.debug("Returning null principal id...");
        return null;
    }
    logger.debug("Returning null principal id...");
    return null;
}
 
開發者ID:hsj-xiaokang,項目名稱:springboot-shiro-cas-mybatis,代碼行數:33,代碼來源:X509SubjectAlternativeNameUPNPrincipalResolver.java

示例3: getSubjectAlternativeNames

import java.security.cert.X509Certificate; //導入方法依賴的package包/類
/**
 * Returns a list of subject alternative names. Any name that is represented as a String by X509Certificate.getSubjectAlternativeNames() is converted to lowercase and returned.
 *
 * @param certificate a certificate
 * @return a list of subject alternative names; list is never null
 * @throws CertificateParsingException if parsing the certificate failed
 */
public static List<String> getSubjectAlternativeNames(final X509Certificate certificate) throws CertificateParsingException {

    final Collection<List<?>> altNames = certificate.getSubjectAlternativeNames();
    if (altNames == null) {
        return new ArrayList<>();
    }

    final List<String> result = new ArrayList<>();
    for (final List<?> generalName : altNames) {
        /**
         * generalName has the name type as the first element a String or byte array for the second element. We return any general names that are String types.
         *
         * We don't inspect the numeric name type because some certificates incorrectly put IPs and DNS names under the wrong name types.
         */
        final Object value = generalName.get(1);
        if (value instanceof String) {
            result.add(((String) value).toLowerCase());
        }

    }

    return result;
}
 
開發者ID:apache,項目名稱:nifi-registry,代碼行數:31,代碼來源:CertificateUtils.java

示例4: getSubjectAltName

import java.security.cert.X509Certificate; //導入方法依賴的package包/類
private static Object getSubjectAltName(X509Certificate cert, int type) {
    Collection<List<?>> subjectAltNames;

    try {
        subjectAltNames = cert.getSubjectAlternativeNames();
    } catch (CertificateParsingException cpe) {
        if (debug != null && Debug.isOn("handshake")) {
            System.out.println(
                    "Attempt to obtain subjectAltNames extension failed!");
        }
        return null;
    }

    if (subjectAltNames != null) {
        for (List<?> subjectAltName : subjectAltNames) {
            int subjectAltNameType = (Integer)subjectAltName.get(0);
            if (subjectAltNameType == type) {
                return subjectAltName.get(1);
            }
        }
    }

    return null;
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:25,代碼來源:ClientHandshaker.java

示例5: getSubjectAltNames

import java.security.cert.X509Certificate; //導入方法依賴的package包/類
private static List<String> getSubjectAltNames(X509Certificate certificate, int type) {
    List<String> result = new ArrayList();
    try {
        Collection<?> subjectAltNames = certificate.getSubjectAlternativeNames();
        if (subjectAltNames == null) {
            return Collections.emptyList();
        }
        Iterator it = subjectAltNames.iterator();
        while (it.hasNext()) {
            List<?> entry = (List) it.next();
            if (entry != null && entry.size() >= 2) {
                Integer altNameType = (Integer) entry.get(0);
                if (altNameType != null && altNameType.intValue() == type) {
                    String altName = (String) entry.get(1);
                    if (altName != null) {
                        result.add(altName);
                    }
                }
            }
        }
        return result;
    } catch (CertificateParsingException e) {
        return Collections.emptyList();
    }
}
 
開發者ID:JackChan1999,項目名稱:boohee_v5.6,代碼行數:26,代碼來源:OkHostnameVerifier.java

示例6: getSubjectAltNames

import java.security.cert.X509Certificate; //導入方法依賴的package包/類
private static List<String> getSubjectAltNames(X509Certificate certificate, int type) {
  List<String> result = new ArrayList<>();
  try {
    Collection<?> subjectAltNames = certificate.getSubjectAlternativeNames();
    if (subjectAltNames == null) {
      return Collections.emptyList();
    }
    for (Object subjectAltName : subjectAltNames) {
      List<?> entry = (List<?>) subjectAltName;
      if (entry == null || entry.size() < 2) {
        continue;
      }
      Integer altNameType = (Integer) entry.get(0);
      if (altNameType == null) {
        continue;
      }
      if (altNameType == type) {
        String altName = (String) entry.get(1);
        if (altName != null) {
          result.add(altName);
        }
      }
    }
    return result;
  } catch (CertificateParsingException e) {
    return Collections.emptyList();
  }
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:29,代碼來源:OkHostnameVerifier.java

示例7: getCN

import java.security.cert.X509Certificate; //導入方法依賴的package包/類
public static Set<String> getCN(X509Certificate cert) {
  Set<String> names = new HashSet<>();

  // 讀取CN
  String subjectDN = cert.getSubjectX500Principal().getName();
  String[] pairs = subjectDN.split(",");
  for (String p : pairs) {
    String[] kv = p.split("=");
    if (kv.length == 2 && kv[0].equals("CN")) {
      names.add(kv[1]);
    }
  }

  // 讀取SubjectAlternativeNames
  try {
    Collection<List<?>> collection = cert.getSubjectAlternativeNames();
    if (collection != null) {
      for (List<?> list : collection) {
        if (list.size() == 2) {
          Object key = list.get(0);
          Object value = list.get(1);
          if (key instanceof Integer && value instanceof String) {
            int intKey = ((Integer) key).intValue();
            String strValue = (String) value;
            if (intKey == SUBALTNAME_DNSNAME || intKey == SUBALTNAME_IPADDRESS) {
              names.add(strValue);
            }
          }
        }
      }
    }
  } catch (CertificateParsingException e) {
    throw new IllegalArgumentException("can not read AlternativeNames.");
  }

  return names;
}
 
開發者ID:apache,項目名稱:incubator-servicecomb-java-chassis,代碼行數:38,代碼來源:CertificateUtil.java

示例8: getDNSSubjectAlts

import java.security.cert.X509Certificate; //導入方法依賴的package包/類
/**
 * Extract all alternative names from a certificate.
 * @param cert
 * @return
 */
private static String[] getDNSSubjectAlts(X509Certificate cert) {
	LinkedList subjectAltList = new LinkedList();
	Collection c = null;
	try {
		c = cert.getSubjectAlternativeNames();
	} catch (CertificateParsingException cpe) {
		// Should probably log.debug() this?
		cpe.printStackTrace();
	}
	if (c != null) {
		Iterator it = c.iterator();
		while (it.hasNext()) {
			List list = (List) it.next();
			int type = ((Integer) list.get(0)).intValue();
			// If type is 2, then we've got a dNSName
			if (type == 2) {
				String s = (String) list.get(1);
				subjectAltList.add(s);
			}
		}
	}
	if (!subjectAltList.isEmpty()) {
		String[] subjectAlts = new String[subjectAltList.size()];
		subjectAltList.toArray(subjectAlts);
		return subjectAlts;
	} else {
		return new String[0];
	}
        
}
 
開發者ID:jenkinsci,項目名稱:lib-commons-httpclient,代碼行數:36,代碼來源:SSLProtocolSocketFactory.java

示例9: getDNSSubjectAlts

import java.security.cert.X509Certificate; //導入方法依賴的package包/類
/**
 * Extracts the array of SubjectAlt DNS names from an X509Certificate.
 * Returns null if there aren't any.
 * <p/>
 * Note:  Java doesn't appear able to extract international characters
 * from the SubjectAlts.  It can only extract international characters
 * from the CN field.
 * <p/>
 * (Or maybe the version of OpenSSL I'm using to test isn't storing the
 * international characters correctly in the SubjectAlts?).
 *
 * @param cert X509Certificate
 * @return Array of SubjectALT DNS names stored in the certificate.
 */
public static String[] getDNSSubjectAlts(X509Certificate cert) {
    final List<String> subjectAltList = new LinkedList<String>();
    Collection<List<?>> c = null;
    try {
        c = cert.getSubjectAlternativeNames();
    }
    catch (CertificateParsingException cpe) {
        // Should probably log.debug() this?
        cpe.printStackTrace();
    }
    if (c != null) {
        Iterator<List<?>> it = c.iterator();
        while (it.hasNext()) {
            List<?> list = it.next();
            int type = ((Integer) list.get(0)).intValue();
            // If type is 2, then we've got a dNSName
            if (type == 2) {
                String s = (String) list.get(1);
                subjectAltList.add(s);
            }
        }
    }
    if (!subjectAltList.isEmpty()) {
        String[] subjectAlts = new String[subjectAltList.size()];
        subjectAltList.toArray(subjectAlts);
        return subjectAlts;
    } else {
        return null;
    }
}
 
開發者ID:nucypher,項目名稱:hadoop-oss,代碼行數:45,代碼來源:SSLHostnameVerifier.java

示例10: getDisplayNameFromCertificate

import java.security.cert.X509Certificate; //導入方法依賴的package包/類
/**
 * Returns either a string that "sums up" the certificate for humans, in a similar manner to what you might see
 * in a web browser, or null if one cannot be extracted. This will typically be the common name (CN) field, but
 * can also be the org (O) field, org+location+country if withLocation is set, or the email
 * address for S/MIME certificates.
 */
@Nullable
public static String getDisplayNameFromCertificate(@Nonnull X509Certificate certificate, boolean withLocation) throws CertificateParsingException {
    X500Name name = new X500Name(certificate.getSubjectX500Principal().getName());
    String commonName = null, org = null, location = null, country = null;
    for (RDN rdn : name.getRDNs()) {
        AttributeTypeAndValue pair = rdn.getFirst();
        String val = ((ASN1String) pair.getValue()).getString();
        ASN1ObjectIdentifier type = pair.getType();
        if (type.equals(RFC4519Style.cn))
            commonName = val;
        else if (type.equals(RFC4519Style.o))
            org = val;
        else if (type.equals(RFC4519Style.l))
            location = val;
        else if (type.equals(RFC4519Style.c))
            country = val;
    }
    final Collection<List<?>> subjectAlternativeNames = certificate.getSubjectAlternativeNames();
    String altName = null;
    if (subjectAlternativeNames != null)
        for (final List<?> subjectAlternativeName : subjectAlternativeNames)
            if ((Integer) subjectAlternativeName.get(0) == 1) // rfc822name
                altName = (String) subjectAlternativeName.get(1);

    if (org != null) {
        return withLocation ? Joiner.on(", ").skipNulls().join(org, location, country) : org;
    } else if (commonName != null) {
        return commonName;
    } else {
        return altName;
    }
}
 
開發者ID:guodroid,項目名稱:okwallet,代碼行數:39,代碼來源:X509Utils.java

示例11: getSubjectAlts

import java.security.cert.X509Certificate; //導入方法依賴的package包/類
/**
 * Extracts the array of SubjectAlt DNS or IP names from an X509Certificate.
 * Returns null if there aren't any.
 *
 * @param cert X509Certificate
 * @param hostname
 * @return Array of SubjectALT DNS or IP names stored in the certificate.
 */
private static String[] getSubjectAlts(
        final X509Certificate cert, final String hostname) {
    int subjectType;
    if (isIPAddress(hostname)) {
        subjectType = 7;
    } else {
        subjectType = 2;
    }

    LinkedList<String> subjectAltList = new LinkedList<String>();
    Collection<List<?>> c = null;
    try {
        c = cert.getSubjectAlternativeNames();
    }
    catch(CertificateParsingException cpe) {
        Logger.getLogger(AbstractVerifier.class.getName())
                .log(Level.FINE, "Error parsing certificate.", cpe);
    }
    if(c != null) {
        for (List<?> aC : c) {
            List<?> list = aC;
            int type = ((Integer) list.get(0)).intValue();
            if (type == subjectType) {
                String s = (String) list.get(1);
                subjectAltList.add(s);
            }
        }
    }
    if(!subjectAltList.isEmpty()) {
        String[] subjectAlts = new String[subjectAltList.size()];
        subjectAltList.toArray(subjectAlts);
        return subjectAlts;
    } else {
        return null;
    }
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:45,代碼來源:AbstractVerifier.java

示例12: getSubjectAltNames

import java.security.cert.X509Certificate; //導入方法依賴的package包/類
private List<String> getSubjectAltNames(X509Certificate certificate, int type) {
  List<String> result = new ArrayList<String>();
  try {
    Collection<?> subjectAltNames = certificate.getSubjectAlternativeNames();
    if (subjectAltNames == null) {
      return Collections.emptyList();
    }
    for (Object subjectAltName : subjectAltNames) {
      List<?> entry = (List<?>) subjectAltName;
      if (entry == null || entry.size() < 2) {
        continue;
      }
      Integer altNameType = (Integer) entry.get(0);
      if (altNameType == null) {
        continue;
      }
      if (altNameType == type) {
        String altName = (String) entry.get(1);
        if (altName != null) {
          result.add(altName);
        }
      }
    }
    return result;
  } catch (CertificateParsingException e) {
    return Collections.emptyList();
  }
}
 
開發者ID:aabognah,項目名稱:LoRaWAN-Smart-Parking,代碼行數:29,代碼來源:OkHostnameVerifier.java

示例13: getSubjectAltNames

import java.security.cert.X509Certificate; //導入方法依賴的package包/類
private static List<String> getSubjectAltNames(X509Certificate certificate, int type) {
  List<String> result = new ArrayList<String>();
  try {
    Collection<?> subjectAltNames = certificate.getSubjectAlternativeNames();
    if (subjectAltNames == null) {
      return Collections.emptyList();
    }
    for (Object subjectAltName : subjectAltNames) {
      List<?> entry = (List<?>) subjectAltName;
      if (entry == null || entry.size() < 2) {
        continue;
      }
      Integer altNameType = (Integer) entry.get(0);
      if (altNameType == null) {
        continue;
      }
      if (altNameType == type) {
        String altName = (String) entry.get(1);
        if (altName != null) {
          result.add(altName);
        }
      }
    }
    return result;
  } catch (CertificateParsingException e) {
    return Collections.emptyList();
  }
}
 
開發者ID:angcyo,項目名稱:RLibrary,代碼行數:29,代碼來源:OkHostnameVerifier.java

示例14: main

import java.security.cert.X509Certificate; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    ByteArrayInputStream is = new ByteArrayInputStream(certStr.getBytes());
    X509Certificate cert = (X509Certificate) cf.generateCertificate(is);

    if (cert.getSubjectAlternativeNames() == null) {
        throw new Exception("Failed to parse Subject Alternative Name");
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:10,代碼來源:X400Address.java

示例15: printCertificateDetails

import java.security.cert.X509Certificate; //導入方法依賴的package包/類
private void printCertificateDetails() throws CertificateParsingException {

      try {
        System.err.println("* Cipher Suite       : " + cipherSuite);

        for (Certificate cert : serverCertificates) {
          System.err.println("* Cert Type          : " + cert.getType());

          if (cert instanceof X509Certificate) {
            X509Certificate x509Cert = (X509Certificate) cert;

            //                  *      Type          : "
            System.err.println("*      Issuer        : " + x509Cert.getIssuerDN());
            System.err.println("*      Subject       : " + x509Cert.getSubjectDN());

            //                  *      Type      : "
            System.err.println("*      Issuer ID     : " + x509Cert.getIssuerUniqueID());
            System.err.println("*      Sig Algorithm : " + x509Cert.getSigAlgName());
            System.err.println("*      Basic Const   : " + x509Cert.getBasicConstraints());
            System.err.println("*      Ext Key Usage : " + x509Cert.getExtendedKeyUsage());
            System.err.println("*      Not Before    : " + x509Cert.getNotBefore());
            System.err.println("*      Not After     : " + x509Cert.getNotAfter());
            System.err.println("*      Subject ID    : " + x509Cert.getSubjectUniqueID());

            Collection<List<?>> altNames = x509Cert.getSubjectAlternativeNames();

            if (altNames != null) {
              for (List<?> nameList : altNames) {
                for (Object name : nameList) {
                  System.err.println("*      Alt Name     : " + name);
                }
              }
            }
          }

          System.err.println("*      Hash Code     : " + cert.hashCode());
          System.err.println("*      PubKey Algo   : " + cert.getPublicKey().getAlgorithm());
          System.err.println("*      PubKey Format : " + cert.getPublicKey().getFormat());
          System.err.println("\n");
        }
      } catch (IllegalStateException ignored) {}
    }
 
開發者ID:symphonyoss,項目名稱:JCurl,代碼行數:43,代碼來源:JCurl.java


注:本文中的java.security.cert.X509Certificate.getSubjectAlternativeNames方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。