gpt4 book ai didi

java - 为 CXF httpConduit 设置 ProxyAuthorizationPolicy 不起作用

转载 作者:行者123 更新时间:2023-11-30 07:06:14 41 4
gpt4 key购买 nike

我正在使用CXF 3.1.5,我正在尝试使其与代理一起使用。如果代理没有用户名和密码,则它可以工作;如果代理有用户名和密码,则它不起作用。这是我的代码:

//to create my own http conduit
bus.setExtension(new TLSAndProxySupportedHTTPConduitFactory(settings, HTTPConduitFactory.class);
//to get wsdl definition
Definition definition = bus.getExtension(WSDLManager.class).getDefinition(uri);

TLSAndProxySupportedHTTPConduitFactory 实现了 HTTPConduitFactory,并且会创建一个继承 URLConnectionHTTPConduit 的 TLSAndProxySupportedHTTPConduit,在 TLSAndProxySupportedHTTPConduit 中,代理设置的相关代码如下:

            //HTTPClientPolicy settings works
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
httpClientPolicy.setProxyServer(proxy.getHostName());
httpClientPolicy.setProxyServerPort(proxy.getPort());

this.setClient(httpClientPolicy);

if (proxy.getUserName() != null) {
//ProxyAuthorizationPolicy settings doesn't work
this.getProxyAuthorization().setUserName(proxy.getUserName());
this.getProxyAuthorization().setPassword(proxy.getPassword());

}

请记住,如果代理没有用户名和密码,一切都会正常工作。如果加载 WSDL 定义的目标 URL 以 https 开头(我需要 https),则代理将不起作用。如果它以http开头,那么带有用户名和密码的代理就可以正常工作。

最佳答案

寻找解决方案:
原因是

The Java Secure Socket Extension (JSSE) library from Sun Microsystems lets you access a secure Web server from behind a firewall via proxy tunneling. To do this, the JSSE application needs to set the https.ProxyHost and https.ProxyPort system properties. The tunneling code in JSSE checks for "HTTP 1.0" in the proxy's response. If your proxy, like many, returns "HTTP 1.1", you will get an IOException. In this case, you need to implement your own HTTPS tunneling protocol.

引用:http://www.javaworld.com/article/2077475/core-java/java-tip-111--implement-https-tunneling-with-jsse.html
https://community.oracle.com/thread/1534538


然后你可以覆盖 URLConnectionHTTPConduit 的 setupConnection 方法。

@Override
protected void setupConnection(Message message, Address address, HTTPClientPolicy csPolicy) throws IOException {
super.setupConnection(message, address, csPolicy);
HttpURLConnection connection = (HttpURLConnection) message.get(KEY_HTTP_CONNECTION);
decorateHttpsURLConnection((HttpsURLConnection) connection);\
message.put(KEY_HTTP_CONNECTION, connection);
}

在方法decorateHttpsURLConnection中:

httpsConnection.setSSLSocketFactory(new SSLTunnelSocketFactory(getProxy(), sslContext.getSocketFactory()));

关于java - 为 CXF httpConduit 设置 ProxyAuthorizationPolicy 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40079374/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com