spring boot http client ssl 체크 패스하기
내부 테스트서버나 로컬에서 ssl 통신을 할때 유용하다.
httpClientConfig.java
@Configuration public class HttpClientConfig { @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; } }
생성후 본문에
@Autowire HttpClientConfig httpClientConfig< URI uri = URI.create(Url+ "/api/articles"); String responseString = httpClientConfig.getForObject(uri, String.class);