具有代理和Autentification的WS客户端

我知道这不是提出问题的正确方法,但我遇到了问题:

我有一个本地存储的wsdl,我需要创建一个Web服务客户端来调用该Web服务。 问题是服务是在防火墙后面,我必须通过代理连接到它,然后我必须validation连接到WS。

我所做的是使用Apache CXF 2.4.6生成WS客户端,然后设置系统范围的代理

System.getProperties().put("proxySet", "true"); System.getProperties().put("https.proxyHost", "10.10.10.10"); System.getProperties().put("https.proxyPort", "8080"); 

我知道这不是最好的做法,所以请提出一个更好的解决方案,如果有人能给我一个关于如何设置认证的提示我真的很感激它

用apache CXF

 HelloService hello = new HelloService(); HelloPortType helloPort = cliente.getHelloPort(); org.apache.cxf.endpoint.Client client = ClientProxy.getClient(helloPort); HTTPConduit http = (HTTPConduit) client.getConduit(); http.getClient().setProxyServer("proxy"); http.getClient().setProxyServerPort(8080); http.getProxyAuthorization().setUserName("user proxy"); http.getProxyAuthorization().setPassword("password proxy"); 

如果您使用的是Spring Java配置,要使用Apache CXF(3.xx)配置JAX-WS客户端,以下代码将起作用:

 import org.apache.cxf.endpoint.Client; import org.apache.cxf.frontend.ClientProxy; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import org.apache.cxf.transport.http.HTTPConduit; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import de.codecentric.namespace.weatherservice.WeatherService; @Configuration public class WeatherServiceConfiguration { @Bean public WeatherService weatherService() { JaxWsProxyFactoryBean jaxWsFactory = new JaxWsProxyFactoryBean(); jaxWsFactory.setServiceClass(WeatherService.class); jaxWsFactory.setAddress("http://yourserviceurl.com/WeatherSoapService_1.0"); return (WeatherService) jaxWsFactory.create(); } @Bean public Client client() { Client client = ClientProxy.getClient(weatherService()); HTTPConduit http = (HTTPConduit) client.getConduit(); http.getClient().setProxyServer("yourproxy"); http.getClient().setProxyServerPort(8080); // your proxy-port return client; } } 

这是相应的Spring XML配置:

文档: http : //cxf.apache.org/docs/client-http-transport-including-ssl-support.html

     proxy_user proxy_pass   

为了使其工作,您应该导入cxf.xml:

  

请注意,将为所有CXF客户端启用此httpConduit(如果有多个)。

您应该将管道名称配置为仅匹配您的服务管道:

 name="{http://example.com/}HelloWorldServicePort.http-conduit" 

您也可以使用java.net.Authenticator类设置代理用户名和密码,但我不确定它是否不是“系统范围”设置。

请看这里: 使用JavavalidationHTTP代理