2016-08-26 3 views
5

post 메서드를 호출하여 Spring3.0 restTemplate을 사용하는 json webservice를 사용하고 있습니다.나머지 템플릿을 사용하여 TLS1.2를 나머지 클라이언트에 적용하는 방법

우리의 응용 프로그램은 WAS 서버에 배포되고 TLS1.0과 소켓 연결을 생성하여 제작자 연결을 시도합니다. 그러나 이제 제작자는 TLS1.1 및 TLS1.2 만 지원합니다.

TLS1.1 또는 TLS 1.2를 사용하려면 restTempate를 적용하는 방법.

일반적으로 apache httpclient 코드의 경우 사용자 정의 ProtocolSocketFactory를 작성하고 createSocket 메소드를 대체하십시오. 그러나, RestTemplate의 경우, 어떻게 달성하기 위해.

+1

TLS 2.0이 없으므로 '지원하기'어렵습니다. –

+1

질문에 답변 해 주신 것에 대해 감사드립니다. – Panther

답변

4

ClientHttpRequestFactory 사용자 지정을 사용하도록 RestTemplate을 구성 할 수 있습니다. 특히 (스프링 3.0을 사용하고 있으므로) CommonsClientHttpRequestFactory이 있습니다. 그러면 Commons HTTP를 자세하게 구성 할 수 있으며 RestTemplate은 요청을 실행하기 위해 RestTemplate을 사용합니다.

실제 구현 클래스는 이후 버전의 스프링에서 변경되었다는 점에 유의하십시오 (아직 3.0을 사용하고 있다면 업데이트를 고려해야합니다). 구현 클래스에서 3.1부터는 HttpComponentsClientHttpRequestFactory이라고합니다. 봄> 3.1

11

:

import javax.net.ssl.SSLContext; 
import org.apache.http.impl.client.CloseableHttpClient; 
import org.apache.http.impl.client.HttpClientBuilder; 
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; 
import org.springframework.web.client.RestTemplate; 

SSLContext context = SSLContext.getInstance("TLSv1.2"); 
context.init(null, null, null); 

CloseableHttpClient httpClient = HttpClientBuilder 
     .create() 
     .setSSLContext(context) 
     .build(); 
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient); 
RestTemplate restTemplate = new RestTemplate(factory); 
..... 
+1

완벽한 예를 제공하므로이 답변이 수용되어야합니다. 이것은 밖으로 작동합니다! – membersound

+0

Spring 3.0.x에서는 어떻게해야합니까? – abhishekhp

+1

@ memememberound 대답은 더 나은 대답이 아니라 OP와 함께 작동하는 것에 관한 것이지만 어쨌든 질문은 봄 3.0에 대해 명시 적입니다. 나는이 대답을 감사하고 upvoted있다. – Panther

0

@abhishekhp 질문가 계속됩니다.

RestTemplate restTemplate = new RestTemplate(); 
    DefaultHttpClient httpClient = new DefaultHttpClient(); 
    // We're going to try and load and enable TLS version 1.2 standard communication context from JSSE Providers 
    // This is enabled only for download media Mirakl as some merchants don't accept communication with TLS versions prior to 1.1 
    try { 
     SSLContext context; 
     context = SSLContext.getInstance("TLSv1.2"); 
     context.init(null, null, null); 

     SSLSocketFactory ssf = new SSLSocketFactory(context); 
     ClientConnectionManager ccm = httpClient.getConnectionManager(); 
     SchemeRegistry sr = ccm.getSchemeRegistry(); 
     sr.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); 
     sr.register(new Scheme("https", 443, ssf)); 

    } catch (NoSuchAlgorithmException | KeyManagementException e) { 
     LOGGER.warn("Could not load the TLS version 1.2 due to => ", e); 
    } 

    restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient)); 
관련 문제