这个问题的原因是因为应用程序无法验证证书的有效性。可以通过编写自定义SSLSocketFactory来解决此问题,以验证证书主机名是否有效。以下是一个示例:
public class CustomSSLSocketFactory extends SSLSocketFactory {
private SSLSocketFactory internalSSLSocketFactory;
public CustomSSLSocketFactory() throws Exception {
TrustManager[] tmfs = new TrustManager[]{new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
// Blank intentionally
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
try {
chain[0].checkValidity();
String hostname = "<>"; //replace with ELB API endpoint
SSLSession session = HttpsURLConnection.getDefaultSSLSocketFactory().createSocket().getSession();
if (!hostname.equals(session.getPeerHost())) {
throw new CertificateException("Hostname does not match " + hostname);
}
} catch (Exception e) {
throw new CertificateException(e);
}
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}};
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, tmfs, null);
internalSSLSocketFactory = sslContext.getSocketFactory();
}
// Implement all methods of SSLSocketFactory
@Override
public String[] getDefaultCipherSuites() {
return internalSSLSocketFactory.getDefaultCipherSuites();
}
@Override
public String[] getSupportedCipherSuites() {
return internalSSLSocketFactory.getSupportedCipherSuites();
}
@Override
public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
return internalSSLSocketFactory.createSocket(s, host, port, autoClose);
}
@Override
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
return internalSSLSocketFactory.createSocket(host, port);
}
@Override
public Socket createSocket(String host, int port, InetAddress localAddress, int localPort)
throws IOException, UnknownHostException {
return internalSSLSocketFactory.createSocket(host, port, localAddress, localPort);
}
@Override
public Socket createSocket(InetAddress host, int port) throws IOException {
return internalSSLSocketFactory
上一篇:AndroidSSLPinning需要公钥的SHA256哈希值长度为32字节的问题
下一篇:AndroidstartActivityForResultsandreturningtocallingapplicationsonexceptions