복붙노트

[SPRING] 나머지 템플릿을 사용하여 클라이언트에 TLS1.2 적용 방법

SPRING

나머지 템플릿을 사용하여 클라이언트에 TLS1.2 적용 방법

나는 post 메소드를 호출하여 Spring 3.0 restTemplate을 사용하여 json 웹 서비스를 사용하고있다.

        MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
        headers.add("Content-Type", MediaType.APPLICATION_JSON_VALUE);      
        HttpEntity<Object> entity = new HttpEntity<Object>(requestAsString, headers);
        postForObject = restTemplate.postForObject(url, entity, responseClass );

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

TLS1.1 또는 TLS 1.2를 사용하도록 restTempate를 적용하는 방법

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

해결법

  1. ==============================

    1.사용자 정의 ClientHttpRequestFactory를 사용하도록 RestTemplate을 구성 할 수 있습니다. 특히 Spring 3.0을 사용하고 있기 때문에 CommonsClientHttpRequestFactory가있다. 그러면 Commons HTTP를 자세하게 구성 할 수 있으며 RestTemplate은 요청을 실행하기 위해 RestTemplate을 사용합니다.

    사용자 정의 ClientHttpRequestFactory를 사용하도록 RestTemplate을 구성 할 수 있습니다. 특히 Spring 3.0을 사용하고 있기 때문에 CommonsClientHttpRequestFactory가있다. 그러면 Commons HTTP를 자세하게 구성 할 수 있으며 RestTemplate은 요청을 실행하기 위해 RestTemplate을 사용합니다.

    Spring의 최신 버전에서 실제 구현 클래스가 변경되었다는 것을 기억하십시오. (여전히 3.0을 사용하고 있다면 업데이트를 고려해야합니다). 구현 클래스에서 3.1부터는 HttpComponentsClientHttpRequestFactory라고합니다.

  2. ==============================

    2.Spring> 3.1 :

    Spring> 3.1 :

    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);
    .....
    
  3. ==============================

    3.@ abhishekhp 귀하의 질문은 아직있다면.

    @ 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));
    
  4. from https://stackoverflow.com/questions/39161542/how-to-enforce-tls1-2-to-rest-client-using-rest-template by cc-by-sa and MIT license