void customizeHttpClient(HttpClient httpClient) throws URISyntaxException { URI proxyURI = new URI("http://proxyhost:proxyport"); AuthenticationStore auth = httpClient.getAuthenticationStore(); // Proxy credentials. auth.addAuthentication(new BasicAuthentication( proxyURI, "Websense Content Gateway", // 认证域 "username", // 认证用户 "passwd")); // 认证密码 ProxyConfiguration proxyConfig = httpClient.getProxyConfiguration(); HttpProxy proxy = new HttpProxy(proxyURI.getHost(), proxyURI.getPort()); proxyConfig.getProxies().add(proxy); }
其中认证域的值是407 Proxy Authorization Required响应头中Proxy-Authenticate: Basic realm="Websense Content Gateway"的引号部分。java
如上配置后,就能够用jetty的HttpClient经过代理发送http请求了。ui
而在使用jetty-proxy模块作透明代理时,因为AbstractProxyServlet在调用createHttpClient()方法的最后,清空了ProtocolHandler(9.2.10.v20150310版本例外),致使若是jetty-proxy向upstream的请求需经过代理的话,那么以上的配置方式不起做用, jetty-proxy不能自动配置代理认证,提示请求须要代理认证。代理
因此还须要在调用 createHttpClient ()方法后,添加一个ProxyAuthenticationProtocolHandler。code
httpClient.getProtocolHandlers().put(new ProxyAuthenticationProtocolHandler(httpClient));
代码适用jetty.version=9.4.6.v20170531get