1. 程式人生 > >使用restTemplate來訪問https

使用restTemplate來訪問https

@Configuration
public class RestTemplateConfig {

	@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;
	}
}

https://www.cnblogs.com/yuxifly828/p/9857521.html