public static Set getCN()

in foundations/foundation-ssl/src/main/java/org/apache/servicecomb/foundation/ssl/CertificateUtil.java [107:143]


  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;
              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;
  }