как узнать apache httpclient использует какую версию SSL - PullRequest
0 голосов
/ 06 марта 2020

мы используем org. apache .httpcomponents.httpclient (v4.5.4) для подключения к другой службе. Однако теперь служба отключила TLS1.0. Как я могу узнать, какая версия SSL / TLS по умолчанию поддерживается. Вот как фрагмент кода, где создается объект httpclient:

private CloseableHttpClient getHttpClient() {
    // always create new httpclient instance when factory is not available
    // e.g. junit test suite
    if (httpClientBuilderFactory == null
            || httpClientBuilderFactory.newBuilder() == null) {
        sc = SocketConfig.custom()
                .setSoTimeout(HTTP_SOCKET_TIMEOUT_SECONDS * 1000).build();
        httpClient = HttpClients.custom().setDefaultSocketConfig(sc).build();
    }

    if (httpClient == null) {
        clientConnectionManager = new PoolingHttpClientConnectionManager(210L,TimeUnit.SECONDS);
        sc = SocketConfig.custom()
                .setSoTimeout(HTTP_SOCKET_TIMEOUT_SECONDS * 1000).build();
        clientConnectionManager.setDefaultMaxPerRoute(20);

        HttpClientBuilder httpClientBuilder = httpClientBuilderFactory
                .newBuilder();
        httpClientBuilder.setDefaultSocketConfig(sc);
        httpClientBuilder.setConnectionManager(clientConnectionManager);
        httpClientBuilder.setConnectionManagerShared(true);
        httpClient = httpClientBuilder.build();
    }

    return httpClient;
}

1 Ответ

0 голосов
/ 06 марта 2020

HttpClient 4.5.x

Чтобы получить доступ к базовому соединению и получить сессию SSL, связанную с ним, потребовался бы пользовательский перехватчик ответов.

CloseableHttpClient httpclient = HttpClients.custom()
        .addInterceptorLast(new HttpResponseInterceptor() {

            @Override
            public void process(HttpResponse response, HttpContext context) throws HttpException, IOException {
                HttpClientContext clientContext = HttpClientContext.adapt(context);
                ManagedHttpClientConnection connection = clientContext.getConnection(ManagedHttpClientConnection.class);
                SSLSession sslSession = connection.getSSLSession();
                if (sslSession != null) {
                    System.out.println("SSL protocol " + sslSession.getProtocol());
                    System.out.println("SSL cipher suite " + sslSession.getCipherSuite());
                }
            }

        })
        .build();
try {

    HttpGet httpget = new HttpGet("https://httpbin.org/");

    System.out.println("Executing request " + httpget.getRequestLine());

    HttpClientContext clientContext = HttpClientContext.create();
    CloseableHttpResponse response = httpclient.execute(httpget, clientContext);
    try {
        HttpEntity entity = response.getEntity();

        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());
        EntityUtils.consume(entity);
    } finally {
        response.close();
    }
} finally {
    httpclient.close();
}

HttpClient 5.0

Начиная с 5.0, можно извлекать сеанс SSL непосредственно из контекста выполнения HTTP.

    try (CloseableHttpClient httpclient = HttpClients.custom().build()) {
        final HttpGet httpget = new HttpGet("https://httpbin.org/");

        System.out.println("Executing request " + httpget.getMethod() + " " + httpget.getUri());

        final HttpClientContext clientContext = HttpClientContext.create();
        try (CloseableHttpResponse response = httpclient.execute(httpget, clientContext)) {
            System.out.println("----------------------------------------");
            System.out.println(response.getCode() + " " + response.getReasonPhrase());
            EntityUtils.consume(response.getEntity());
            final SSLSession sslSession = clientContext.getSSLSession();
            if (sslSession != null) {
                System.out.println("SSL protocol " + sslSession.getProtocol());
                System.out.println("SSL cipher suite " + sslSession.getCipherSuite());
            }
        }
    }
...