Service: This system interface is HTTP. The third-party interface cannot be invoked from RestTemplate because security verification is performed

Methods: overwriting SimpleClientHttpRequestFactory prepareConnection methods of an abstract class

package com.minstone.apprBase.common.utils.http.rest;

import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.conn.ssl.TrustStrategy;
import org.springframework.http.client.SimpleClientHttpRequestFactory;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import java.net.HttpURLConnection;
import java.security.KeyStore;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

/** * compatible with Https interface *@Author mazq
 * @Date2020/06/04 "*@Param
 * @return* /
public class HttpsClientRequestFactory extends SimpleClientHttpRequestFactory {
 
    @Override
    protected void prepareConnection(HttpURLConnection connection, String httpMethod) {
        try {
            if(! (connectioninstanceof HttpsURLConnection)) {/ / HTTP protocol
                //throw new RuntimeException("An instance of HttpsURLConnection is expected");
                super.prepareConnection(connection, httpMethod);
            }
            if (connection instanceof HttpsURLConnection) {// HTTPS to change the protocol version
                KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
                // Trust any link
                TrustStrategy anyTrustStrategy = new TrustStrategy() {
                    @Override
                    public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
                        return true; }}; SSLContext ctx = SSLContexts.custom().useTLS().loadTrustMaterial(trustStore, anyTrustStrategy).build(); ((HttpsURLConnection) connection).setSSLSocketFactory(ctx.getSocketFactory()); HttpsURLConnection httpsConnection = (HttpsURLConnection) connection;super.prepareConnection(httpsConnection, httpMethod); }}catch(Exception e) { e.printStackTrace(); }}}Copy the code

The key code, new RestTemplate (new HttpsClientRequestFactory ()); , corresponding tool class reference:

package com.minstone.apprBase.common.utils.http.rest;

import com.minstone.apprBase.common.utils.web.WebUtil;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/** ** <pre> * RestTemplate remotely calls the utility class * </pre> ** <pre> *@authorMazq * Modified Record * Modified by: Date: 2020/06/01 11:38 Modified Content: * </pre> */
@Component
public class RestTemplateUtils {


    public static RestTemplate geTemplate(a){
        return new RestTemplate(new HttpsClientRequestFactory());
    }

    /** * GET request invocation *@Author mazq
     * @Date2020/06/01 shouldest be *@Param [url, responseType, uriVariables]
     * @return org.springframework.http.ResponseEntity<T>
     */
    public static <T> ResponseEntity<T> getForEntity(String url, Class
       
         responseType, Map
        
          uriVariables)
        ,>
        {
        return geTemplate().getForEntity(url, responseType, uriVariables);
    }

    /** * How to invoke the POST request *@Author mazq
     * @Date2020/06/01 shouldest be *@Param [url, headers, body, responseType]
     * @return org.springframework.http.ResponseEntity<T>
     */
    public static <T> ResponseEntity<T> postForEntity(String url,HttpHeaders headers, Object requestBody , Class<T> responseType ){

        HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody, headers);
        return  geTemplate().postForEntity(url, requestEntity, responseType);
    }

    /** * PUT Request invocation method *@Author mazq
     * @Date2020/06/01 "*@paramUrl Request URL *@paramHeaders Request header parameter *@paramRequestBody Specifies the request parameter body *@paramResponseType Returns the object type *@paramUriVariables Variables in the URL, corresponding to keys in the Map *@returnResponseEntity Response object encapsulation class */
    public static <T> ResponseEntity<T> put(String url, HttpHeaders headers, Object requestBody, Class
       
         responseType, Map
        
          uriVariables)
        ,>
        {
        HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody, headers);
        return geTemplate().exchange(url, HttpMethod.PUT, requestEntity, responseType, uriVariables);
    }

    /** * DELETE request invocation method *@Author mazq
     * @Date 2020/06/01 13:37
     * @paramUrl Request URL *@paramHeaders Request header parameter *@paramRequestBody Specifies the request parameter body *@paramResponseType Returns the object type *@paramUriVariables Variables in the URL, corresponding to * in sequence@returnResponseEntity Response object encapsulation class */
    public static <T> ResponseEntity<T> delete(String url, HttpHeaders headers, Object requestBody, Class<T> responseType, Object... uriVariables) {
        HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody, headers);
        return geTemplate().exchange(url, HttpMethod.DELETE, requestEntity, responseType, uriVariables);
    }

    /** * General call method *@Author mazq
     * @Date 2020/06/01 13:37
     * @Param [url, method, requestEntity, responseType, uriVariables]
     * @return org.springframework.http.ResponseEntity<T>
     */
    public static <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity
        requestEntity, Class
       
         responseType, Map
        
          uriVariables)
        ,>
        {
        returngeTemplate().exchange(url, method, requestEntity, responseType, uriVariables); }}Copy the code