Ignore SSL Certificate in a Servlet

后端 未结 3 1113
忘掉有多难
忘掉有多难 2021-02-19 04:21

I am getting the following exception:

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.pro         


        
3条回答
  •  无人及你
    2021-02-19 04:49

    Try the following code.

    import java.security.cert.CertificateException;
    import java.security.cert.X509Certificate;
    
    import javax.net.ssl.X509TrustManager;
    
    public class DummyX509TrustManager implements X509TrustManager {
    
        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    
        @Override
        public void checkServerTrusted(X509Certificate[] paramArrayOfX509Certificate, String paramString)
                throws CertificateException {
        }
    
        @Override
        public void checkClientTrusted(X509Certificate[] paramArrayOfX509Certificate, String paramString)
                throws CertificateException {
        }
    };
    
    
    final TrustManager[] trustAllCerts = new TrustManager[] { new DummyX509TrustManager() };
    try {
        SSLContext sslContext= SSLContext.getInstance("SSL"); 
        sslContext.init(null, trustAllCerts, null);
    
        CloseableHttpClient client = HttpClients.custom()
            .setRedirectStrategy(new LaxRedirectStrategy()) 
            .setSslcontext(sslContext)   
            .setConnectionManager(connMgr)
            .build();
    } catch (KeyManagementException e) {
        throw new IOException(e.getMessage());
    } catch (NoSuchAlgorithmException e) {
        throw new IOException(e.getMessage());
    }
    

提交回复
热议问题