resttemplate实现https请求

直接上代码

package com.raysdata.ess.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.SimpleClientHttpRequestFactory;

import org.springframework.web.client.RestTemplate;


import javax.net.ssl.*;
import javax.security.cert.CertificateException;
import javax.security.cert.X509Certificate;
import java.io.IOException;
import java.net.HttpURLConnection;


@Configuration
public class WebConfigurer {
     

    @Bean
    public RestTemplate restTemplate(ClientHttpsRequestFactory factory) {
     
        return new RestTemplate(factory);
    }

    @Bean
    public ClientHttpsRequestFactory simpleClientHttpRequestFactory() {
     
        ClientHttpsRequestFactory factory = new ClientHttpsRequestFactory();
        factory.setReadTimeout(5000);
        factory.setConnectTimeout(5000);
        return factory;
    }

    class ClientHttpsRequestFactory extends SimpleClientHttpRequestFactory {
     

        @Override
        protected void prepareConnection(HttpURLConnection connection, String httpMethod)
                throws IOException {
     
            if (connection instanceof HttpsURLConnection) {
     
                prepareHttpsConnection((HttpsURLConnection) connection);
            }
            super.prepareConnection(connection, httpMethod);
        }

        private void prepareHttpsConnection(HttpsURLConnection connection) {
     
            connection.setHostnameVerifier(new SkipHostnameVerifier());
            try {
     
                connection.setSSLSocketFactory(createSslSocketFactory());
            } catch (Exception ex) {
     
                // Ignore
            }
        }

        private SSLSocketFactory createSslSocketFactory() throws Exception {
     
            SSLContext context = SSLContext.getInstance("TLSv1.2");
            context.init(null, new TrustManager[]{
     new X509TrustManager() {
     
                @Override
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
     
                    return new java.security.cert.X509Certificate[0];
                }

                @Override
                public void checkClientTrusted(java.security.cert.X509Certificate[] var1, String var2) {
     
                    if(true) {
     }
                }

                @Override
                public void checkServerTrusted(java.security.cert.X509Certificate[] var1, String var2) {
     
                    if(true) {
     }
                }

            }
            }, new java.security.SecureRandom());
            return context.getSocketFactory();
        }

        private class SkipHostnameVerifier implements HostnameVerifier {
     

            @Override
            public boolean verify(String requestedHost, SSLSession remoteServerSession) {
     
                System.out.println(requestedHost);
                System.out.println("+++++++++++++++++++++++++++");
                System.out.println(remoteServerSession.getPeerHost());
                System.out.println("+++++++++++++++++++++++++++++");
                System.out.println(requestedHost.equalsIgnoreCase(remoteServerSession.getPeerHost()));
                return requestedHost.equalsIgnoreCase(remoteServerSession.getPeerHost()); // Compliant
            }

        }
    }
}



测试
package com.raysdata;


import com.raysdata.ess.EssSsoServiceApplication;
import org.apache.commons.lang3.builder.ToStringExclude;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;


@RunWith(SpringRunner.class)
@SpringBootTest(classes = EssSsoServiceApplication.class)
public class Test {
     

    @Autowired
    private RestTemplate restTemplate;
    @org.junit.Test
    public void test() {
     


        String url = "https://free-api.heweather.com/v5/forecast?city=CN101080101&key=5c043b56de9f4371b0c7f8bee8f5b75e";
        String resp = restTemplate.getForObject(url, String.class);
        System.out.println(resp);
        
    }
}

你可能感兴趣的:(Resttemplate,https,java,spring,boot)