【问题标题】:How to use Apache Httpclient with Jersey 2.3 client如何将 Apache Httpclient 与 Jersey 2.3 客户端一起使用
【发布时间】:2013-10-04 09:52:48
【问题描述】:

我想使用带有 Jersey 2.3 客户端的 Apache 连接器进行 HTTPS 连接。

我尝试了以下方法:

ClientConfig clientConfig = new ClientConfig();
clientConfig.property(ApacheClientProperties.CONNECTION_MANAGER, new PoolingClientConnectionManager());

ApacheConnector connector = new ApacheConnector(clientConfig);  
clientConfig.connector(connector);  

Client client = ClientBuilder.newBuilder()
    .withConfig(clientConfig)
    .sslContext(sslContext)
    .hostnameVerifier(getHostnameVerifier())
    .build();

但是,似乎 sslContext 被忽略,因为服务器的证书被拒绝为不受信任(sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效证书路径)

如果我删除“.withConfig(clientConfig)”部分,SSL 连接工作正常,但显然没有 Apache 连接器。有没有办法将我自己的 ClientConfig 与 Apache 连接器以及我自己的 SSLContext 一起使用?

【问题讨论】:

    标签: java apache ssl jersey


    【解决方案1】:

    您需要为 apache 连接器配置 SSL。

    ClientConfig clientConfig = new ClientConfig();
    clientConfig.property(ApacheClientProperties.CONNECTION_MANAGER, new PoolingClientConnectionManager());
    //config your ssl for apache connector
    SslConfigurator sslConfig = SslConfigurator.newInstance();
    clientConfig.property(ApacheClientProperties.SSL_CONFIG, sslConfig);
    
    ApacheConnector connector = new ApacheConnector(clientConfig);  
    clientConfig.connector(connector);  
    
    Client client = ClientBuilder.newBuilder()
    .withConfig(clientConfig)
    .hostnameVerifier(getHostnameVerifier())
    .build();
    

    【讨论】:

    • 行得通!对我来说唯一的问题是此类 org.glassfish.jersey.SslConfigurator 不允许选择使用的密码。我需要类似 sslConfigurator.setEnabledCipherSuites(new String[] { "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", "TLS_RSA_WITH_AES_128_CBC_SHA256", "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", "TLS_RSA_WITH_AES_128_CBC_SHA" });
    【解决方案2】:

    不确定你为什么使用 apache 连接器。但是您可以像这样使用 jersey 和 https 建立客户端连接:

    ClientConfig config = new DefaultClientConfig();
    SSLContext ctx = SSLContext.getInstance("SSL");
    ctx.init(null, myTrustManager, null);
    config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties(hostnameVerifier, ctx));
    Client client = Client.create(config);
    

    【讨论】:

    • 我会试试的,谢谢!我使用 Apache 连接器的原因是我想支持使用带有用户名和密码的 HTTP 代理。我没有找到没有 Apache 连接器的方法。
    • 看起来你建议的是旧的 Jersey 1.0 语法,而不是 2.x
    • 一个原因可能是jersey.java.net/apidocs/2.23/jersey/org/glassfish/jersey/client/… 在 IBM 的 JVM 上不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-23
    • 1970-01-01
    • 2019-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-23
    相关资源
    最近更新 更多