简体   繁体   English

如何在Jersey和Apache Http客户端上使用代理身份验证?

[英]How to use Proxy authentication with Jersey and Apache Http client?

I am using jersey client with ApacheConnection Provider. 我正在将Jersey客户端与ApacheConnection Provider一起使用。

    Builder builder = RequestConfig.custom().setConnectTimeout(timeout);
    List<Proxy> proxies = ProxyManager.getInstance().select(baseUrl.toURI());
    if (useProxy) {
        ...
        builder.setProxy(new HttpHost(proxyUri.getHost(), proxyUri.getPort()));
    }
    RequestConfig requestConfig = builder.build();

    final ClientConfig clientConfig = new ClientConfig();
    clientConfig.property(ApacheClientProperties.REQUEST_CONFIG, requestConfig);
    clientConfig.connectorProvider(new ApacheConnectorProvider());

    client = ClientBuilder.newBuilder().withConfig(clientConfig).sslContext(getSSLContext()).build();
    client.property(ClientProperties.CONNECT_TIMEOUT, 5000);

But how to add username and password for Proxy authentication? 但是,如何为代理身份验证添加用户名和密码?

Seems like apache connection provider does not use the standard java proxy selector mechanisms. 似乎apache连接提供程序未使用标准的Java代理选择器机制。

I finally found the solution by myself. 我终于找到了解决方案。 Unfortunately this is documented nowhere: 不幸的是,这没有记载:

HttpHost proxyhost = new HttpHost(host,pw);

CredentialsProvider credsProvider = new BasicCredentialsProvider();

credsProvider.setCredentials(new AuthScope(proxyhost), new UsernamePasswordCredentials(user, pw));
clientConfig.property(ApacheClientProperties.CREDENTIALS_PROVIDER, credsProvider);

builder.setProxy(proxyhost);

I think you should add few more lines of code 我认为您应该再添加几行代码

builder.setProxy(proxyhost).setDefaultCredentialsProvider(credsProvider)
                            .setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy());

otherwise it wont really authenticate the proxy host I feel. 否则,它不会真正对我感觉到的代理主机进行身份验证。 In your case it might be bypassing the proxy. 在您的情况下,它可能会绕过代理。 ?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM