How to disable SSL certificate checking with Spring RestTemplate?

后端 未结 9 1229
余生分开走
余生分开走 2020-11-29 18:11

I am trying to write an integration test where our test launches an embedded HTTPS server using Simple. I created a self-signed certificate using keytool and am able to acce

9条回答
  •  星月不相逢
    2020-11-29 19:06

    Iknow It is too old to answer, but I couldn't find solution like this.

    The code that worked for me with the jersey client:

    import org.glassfish.jersey.client.ClientConfig;
    import org.glassfish.jersey.client.ClientProperties;
    
    import javax.net.ssl.*;
    import javax.ws.rs.client.Client;
    import javax.ws.rs.client.ClientBuilder;
    import javax.ws.rs.client.Entity;
    import javax.ws.rs.client.WebTarget;
    import javax.ws.rs.core.Form;
    import javax.ws.rs.core.MediaType;
    import javax.ws.rs.core.MultivaluedHashMap;
    import java.security.KeyManagementException;
    import java.security.NoSuchAlgorithmException;
    import java.security.cert.CertificateException;
    
    public class Testi {
    
    static {
        disableSslVerification();
    }
    private static void disableSslVerification() {
        // Create all-trusting host name verifier
        HostnameVerifier allHostsValid = new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        };
        // Install the all-trusting host verifier
        HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
    }
    
    public Testi() {
        MultivaluedHashMap headers = new MultivaluedHashMap<>();
        //... initialize headers
    
        Form form = new Form();
        Entity
    entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE); // initialize entity ... WebTarget target = getWebTarget(); Object responseResult = target.path("api/test/path...").request() .headers(headers).post(entity, Object.class); } public static void main(String args[]) { new Testi(); } private WebTarget getWebTarget() { ClientConfig clientConfig = new ClientConfig(); clientConfig.property(ClientProperties.CONNECT_TIMEOUT, 30000); clientConfig.property(ClientProperties.READ_TIMEOUT, 30000); SSLContext sc = getSSLContext(); Client client = ClientBuilder.newBuilder().sslContext(sc).withConfig(clientConfig).build(); WebTarget target = client.target("...url..."); return target; } private SSLContext getSSLContext() { try { // Create a trust manager that does not validate certificate chains TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() { @Override public void checkClientTrusted(java.security.cert.X509Certificate[] x509Certificates, String s) throws CertificateException { } @Override public void checkServerTrusted(java.security.cert.X509Certificate[] x509Certificates, String s) throws CertificateException { } public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } } }; // Install the all-trusting trust manager SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); return sc; } catch (NoSuchAlgorithmException | KeyManagementException e) { e.printStackTrace(); } return null; } }

提交回复
热议问题