具有代理和身份验证的WS客户端

时间:2013-06-20 13:31:29

标签: java web-services jax-ws webservices-client jax-ws-customization

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

我在本地存储了一个wsdl,我需要创建一个Web服务客户端来调用该Web服务。问题是服务是在防火墙后面,我必须通过代理连接到它,然后我必须验证连接到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");

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

4 个答案:

答案 0 :(得分:19)

使用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");

答案 1 :(得分:5)

如果您使用Spring Java配置,要使用Apache CXF(3.x.x)配置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;
    }
}

答案 2 :(得分:4)

以下是相应的Spring XML配置:

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

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
  xmlns:sec="http://cxf.apache.org/configuration/security"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
                      http://www.springframework.org/schema/beans/spring-beans.xsd 
                      http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd
                      http://cxf.apache.org/configuration/security http://cxf.apache.org/schemas/configuration/security.xsd">

<http-conf:conduit name="*.http-conduit"> 
    <http-conf:client ProxyServer="proxy" ProxyServerPort="8080"/>

    <http-conf:proxyAuthorization> 
        <sec:UserName>proxy_user</sec:UserName> 
        <sec:Password>proxy_pass</sec:Password> 
    </http-conf:proxyAuthorization> 
</http-conf:conduit>

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

<import resource="classpath:META-INF/cxf/cxf.xml"/>

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

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

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

答案 3 :(得分:0)

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

看这里: Authenticated HTTP proxy with Java