代码之家  ›  专栏  ›  技术社区  ›  Bob Santos

Spring集成:强制Webservice出站网关使用TLSv1

  •  1
  • Bob Santos  · 技术社区  · 6 年前

    我正在尝试与外部webservice集成,它使用TLS 1.0协议的证书。

    我试过设置 -Dhttps.protocols=TLSv1,TLSv1.1,TLSv1.2,SSLv3 但出站网关仍使用 TLSv1.2 . 如果我将选项限制为 TLSv1 但我不能这样做,因为我正在整合许多外部服务。

    如何在出站网关本身上设置协议?

    我试着用这段代码做到这一点,但遇到了一个异常:

    SimpleWebServiceOutboundGateway handler = new SimpleWebServiceOutboundGateway(url);
    HttpComponentsMessageSender sender = new HttpComponentsMessageSender();
    SSLContext sslContext = SSLContext.getDefault();
    sslContext.getDefaultSSLParameters().setProtocols(new String[]{"TLSv1"});
    CloseableHttpClient httpClient = HttpClients.custom().setSSLContext(sslContext).build();
    sender.setHttpClient(httpClient);
    handler.setMessageSender(sender);
    

    例外情况:

    Caused by: org.apache.http.client.ClientProtocolException: null
        at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186)
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
        at org.springframework.ws.transport.http.HttpComponentsConnection.onSendAfterWrite(HttpComponentsConnection.java:121)
        at org.springframework.ws.transport.AbstractWebServiceConnection.send(AbstractWebServiceConnection.java:48)
        at org.springframework.ws.client.core.WebServiceTemplate.sendRequest(WebServiceTemplate.java:658)
        at org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:606)
        at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:555)
        ... 91 common frames omitted
    Caused by: org.apache.http.ProtocolException: Content-Length header already present
        at org.apache.http.protocol.RequestContent.process(RequestContent.java:97)
        at org.apache.http.protocol.ImmutableHttpProcessor.process(ImmutableHttpProcessor.java:133)
        at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:182)
        at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
        at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
        at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
        ... 99 common frames omitted
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Bob Santos    6 年前

    我查看了HttpComponentsMessengerSender的相关文档,发现 HttpComponentsMessageSender.RemoveSoapHeadersInterceptor

    HttpClient HttpRequestInterceptor实现,从请求中删除内容长度和传输编码头。这是必要的,因为一些SAAJ和其他SOAP实现自己设置了这些头,如果设置了这些头,HttpClient会抛出异常。

    解决方案是将其添加到 HttpComponentsMessengerSender .

    发送方的最终版本为:

    SSLContext sslContext = SSLContexts.custom().useProtocol("TLSv1").build();
    CloseableHttpClient httpClient = HttpClients.custom()
                .setSSLContext(sslContext)
                .addInterceptorFirst(new 
    HttpComponentsMessageSender.RemoveSoapHeadersInterceptor())
                .build();
    
    HttpComponentsMessageSender sender = new HttpComponentsMessageSender(httpClient);