com.aliyuncs.http.CompositeX509TrustManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aliyun-java-sdk-core Show documentation
Show all versions of aliyun-java-sdk-core Show documentation
Aliyun Open API SDK for Java
Copyright (C) Alibaba Cloud Computing
All rights reserved.
版权所有 (C)阿里云计算有限公司
http://www.aliyun.com
package com.aliyuncs.http;
import javax.net.ssl.X509TrustManager;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class CompositeX509TrustManager implements X509TrustManager {
private final List trustManagers;
private boolean ignoreSSLCert = false;
public boolean isIgnoreSSLCert() {
return ignoreSSLCert;
}
public void setIgnoreSSLCert(boolean ignoreSSLCert) {
this.ignoreSSLCert = ignoreSSLCert;
}
public CompositeX509TrustManager(List trustManagers) {
this.trustManagers = trustManagers;
}
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) {
// do nothing
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
if (ignoreSSLCert) {
return;
}
for (X509TrustManager trustManager : trustManagers) {
try {
trustManager.checkServerTrusted(chain, authType);
return; // someone trusts them. success!
} catch (CertificateException e) {
// maybe someone else will trust them
}
}
throw new CertificateException("None of the TrustManagers trust this certificate chain");
}
@Override
public X509Certificate[] getAcceptedIssuers() {
List certificates = new ArrayList();
for (X509TrustManager trustManager : trustManagers) {
certificates.addAll(Arrays.asList(trustManager.getAcceptedIssuers()));
}
X509Certificate[] certificatesArray = new X509Certificate[certificates.size()];
return certificates.toArray(certificatesArray);
}
}