【问题标题】:unit test apache http client proxy 407单元测试apache http客户端代理407
【发布时间】:2015-01-26 15:00:39
【问题描述】:

我正在尝试构建一个需要联系外部服务器的 Junit/Integration 测试。但是,我无法通过代理。我收到 407 空白身份验证页面错误。

我使用的测试设置

@Before
public void onSetUp() throws Exception {

    System.setProperty("http.proxyHost", "webproxy-nl.test.com");
    System.setProperty("http.proxyPort", "8080");
    System.setProperty("https.proxyHost", "webproxy-nl.test.com");
    System.setProperty("https.proxyPort", "8080");

    System.setProperty("http.proxyUser", "DOM\\lalal");
    System.setProperty("http.proxyPassword", "tesssst");

    System.setProperty("https.proxyUser", "DOM\\lalala");
    System.setProperty("https.proxyPassword", "sldjsdkl");
}

现在所有代理设置都是 100% 正确的。我还添加了一些非代理主机。

我不知道我还能在这里配置什么。

返回信息是:

Http request failed: HTTP/1.1 407 BlankAuthenticationPage [status code 407]

更新

我构建了一个使用 CloseableHttpClient 的测试存根。这仍然给了我 http 407 错误。

private CloseableHttpClient httpClient;

public IDealHttpClientStub() {

    LOG.debug("Creating IDealHttpClientStub");

    System.setProperty("http.proxyHost", "webproxy-nl.test.com");
    System.setProperty("http.proxyPort", "8080");
    System.setProperty("https.proxyHost", "webproxy-nl.test.com");
    System.setProperty("https.proxyPort", "8080");

    System.setProperty("http.proxyUser", "DOM\\lalal");
    System.setProperty("http.proxyPassword", "tesssst");

    System.setProperty("https.proxyUser", "DOM\\lalala");
    System.setProperty("https.proxyPassword", "sldjsdkl");

    this.httpClient = HttpClients.custom().useSystemProperties().build();
}

【问题讨论】:

    标签: apache junit4 apache-httpclient-4.x http-proxy


    【解决方案1】:

    除非明确配置,否则 HttpClient 不会使用系统属性。

    CloseableHttpClient client1 = HttpClients.createSystem();
    CloseableHttpClient client2 = HttpClients.custom()
            .useSystemProperties()
            .build();
    

    【讨论】:

    • 那行不通。我围绕 CloseableHttpClient 构建了测试。仍然得到相同的 407 错误。查看我更新的问题
    • 这可能有很多原因。服务器配置为支持哪些身份验证方案? HC选择什么方案进行认证?是NTLM吗?看起来信用包含一个 NT 域。
    【解决方案2】:

    我已经使用以下 impl 完成了它。

    private CloseableHttpClient httpClient;
    
    public HttpClientStub() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
    
        // Trust all certs
        SSLContext sslcontext = buildSSLContext();
    
        // Allow TLSv1 protocol only
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null,
                SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    
        HttpHost proxy = new HttpHost("someproxy", 8080, "http");
        DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
    
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
    
        credsProvider.setCredentials(
                new AuthScope("someproxy", 8080, AuthScope.ANY_HOST, "ntlm"), new NTCredentials(
                        "itsme", "xxxx", "", "MYDOMAIN"));
    
        this.httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider)
                .setRoutePlanner(routePlanner).setSSLSocketFactory(sslsf).build();
    
    }
    
    private static SSLContext buildSSLContext() throws NoSuchAlgorithmException, KeyManagementException,
            KeyStoreException {
        SSLContext sslcontext = SSLContexts.custom().setSecureRandom(new SecureRandom())
                .loadTrustMaterial(null, new TrustStrategy() {
    
                    @Override
                    public boolean isTrusted(java.security.cert.X509Certificate[] chain, String authType)
                            throws java.security.cert.CertificateException {                        
                        return true;
                    }
                }).build();
        return sslcontext;
    }
    

    然后你的代理配置了 HTTP(测试)客户端。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多