Disabling SSL Certificate Validation in Spring RestTemplate

后端 未结 12 1456
花落未央
花落未央 2020-11-28 20:05

I am having two Spring based web apps A and B, on two different machines.

I want to make a https call from web app A to web app B, however I am using a self-signed c

相关标签:
12条回答
  • 2020-11-28 20:28

    I found a simple way

        TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
        SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
        SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
        CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(csf).build();
        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
        requestFactory.setHttpClient(httpClient);
    
        RestTemplate restTemplate = new RestTemplate(requestFactory);
    
    0 讨论(0)
  • 2020-11-28 20:35

    Security: disable https/TLS certificate hostname check,the following code worked in spring boot rest template

    *HttpsURLConnection.setDefaultHostnameVerifier(
            //SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER
            // * @deprecated (4.4) Use {@link org.apache.http.conn.ssl.NoopHostnameVerifier}
            new NoopHostnameVerifier()
    );*
    
    0 讨论(0)
  • 2020-11-28 20:35

    If you are using rest template, you can use this piece of code

        fun getClientHttpRequestFactory(): ClientHttpRequestFactory {
            val timeout = envTimeout.toInt()
            val config = RequestConfig.custom()
                .setConnectTimeout(timeout)
                .setConnectionRequestTimeout(timeout)
                .setSocketTimeout(timeout)
                .build()
    
            val acceptingTrustStrategy = TrustStrategy { chain: Array<X509Certificate?>?, authType: String? -> true }
    
            val sslContext: SSLContext = SSLContexts.custom()
                .loadTrustMaterial(null, acceptingTrustStrategy)
                .build()
    
            val csf = SSLConnectionSocketFactory(sslContext)
    
            val client = HttpClientBuilder
                .create()
                .setDefaultRequestConfig(config)
                .setSSLSocketFactory(csf)
                .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
                .build()
            return HttpComponentsClientHttpRequestFactory(client)
        }
    
        @Bean
        fun getRestTemplate(): RestTemplate {
            return RestTemplate(getClientHttpRequestFactory())
        }
    
    0 讨论(0)
  • 2020-11-28 20:39
    @Bean
    public RestTemplate restTemplate() 
                    throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
        TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
    
        SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
                        .loadTrustMaterial(null, acceptingTrustStrategy)
                        .build();
    
        SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
    
        CloseableHttpClient httpClient = HttpClients.custom()
                        .setSSLSocketFactory(csf)
                        .build();
    
        HttpComponentsClientHttpRequestFactory requestFactory =
                        new HttpComponentsClientHttpRequestFactory();
    
        requestFactory.setHttpClient(httpClient);
        RestTemplate restTemplate = new RestTemplate(requestFactory);
        return restTemplate;
     }
    
    0 讨论(0)
  • 2020-11-28 20:46

    You can use this with HTTPClient API.

    public RestTemplate getRestTemplateBypassingHostNameVerifcation() {
        CloseableHttpClient httpClient = HttpClients.custom().setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
        requestFactory.setHttpClient(httpClient);
        return new RestTemplate(requestFactory);
    
    }
    
    0 讨论(0)
  • 2020-11-28 20:46

    Java code example for HttpClient > 4.3

    package com.example.teocodownloader;
    
    import org.apache.http.conn.ssl.NoopHostnameVerifier;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
    import org.springframework.web.client.RestTemplate;
    
    public class Example {
        public static void main(String[] args) {
            CloseableHttpClient httpClient
                    = HttpClients.custom()
                    .setSSLHostnameVerifier(new NoopHostnameVerifier())
                    .build();
            HttpComponentsClientHttpRequestFactory requestFactory
                    = new HttpComponentsClientHttpRequestFactory();
            requestFactory.setHttpClient(httpClient);
            RestTemplate restTemplate = new RestTemplate(requestFactory);
        }
    }
    

    By the way, don't forget to add the following dependencies to the pom file:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
    </dependency>
    

    You could find Java code example for HttpClient < 4.3 as well.

    0 讨论(0)
提交回复
热议问题