使用HttpClient访问Https时发生 SSL:Certificate for <IP> doesn‘t match any of the subject alternative
跳过证书认证,信任策略设置为all
import org.apache.http.conn.ssl.TrustStrategy;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
public class AnyTrustStrategy implements TrustStrategy {
@Override
public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
return true;
}
}
通过信任策略及SSL获取CloseableHttpClient
/**
* 解决https访问异常
* @return
*/
public CloseableHttpClient getCloseableHttpClient(){
SSLConnectionSocketFactory scsf = null;
try {
scsf = new SSLConnectionSocketFactory(
SSLContexts.custom().loadTrustMaterial(null, new AnyTrustStrategy()).build(),
NoopHostnameVerifier.INSTANCE);
return HttpClients.custom().setSSLSocketFactory(scsf).build();
} catch (NoSuchAlgorithmException e) {
log.error(e.getMessage());
} catch (KeyManagementException e) {
log.error(e.getMessage());
} catch (KeyStoreException e) {
log.error(e.getMessage());
}
return null;
}
参考文章//stackoverflow.com/questions/39762760/javax-net-ssl-sslexception-certificate-doesnt-match-any-of-the-subject-alterna
还没有评论,来说两句吧...