Spring RestTemplate 关闭 SSL 验证

适用于 Apache HttpClient java import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.conn.ssl.TrustStrategy; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.ssl.SSLContexts; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.boot.web.client.RestTemplateCustomizer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; import javax.net.ssl.SSLContext; import java.security.KeyManagementException; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.util.List; /** * @author zhiwen95 * @date 2022/06/16 */ @Configuration public class RestTemplateConfiguration { @Autowired private List<RestTemplateCustomizer> customizers; @Bean public RestTemplate restTemplate() { return new RestTemplateBuilder(customizers.toArray(new RestTemplateCustomizer[0])) .requestFactory(() -> { HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); try { disableSslVerification(requestFactory); } catch (Exception e) { throw new RuntimeException(e); } return requestFactory; }) .