2016-08-26 30 views
5

postメソッドを呼び出すことによってSpring3.0 restTemplateを使用してjson webserviceを使用しています。Restテンプレートを使用して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

カスタムを使用するようにRestTemplateを設定することができますClientHttpRequestFactory。特に(あなたがSpring 3.0を使っているので)、CommonsClientHttpRequestFactoryがあります。これにより、コモンズHTTPを詳細に設定できるようになり、RestTemplateは要求を実行するためにRestTemplateを使用します。

実際の実装クラスはSpringのそれ以降のバージョンで変更されていることに注意してください(まだ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

@ memembersoundの回答はより良い答えではなく、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)); 
関連する問題