【问题标题】:WS Client with Proxy and Autentification具有代理和身份验证的 WS 客户端
【发布时间】:2013-06-17 09:20:26
【问题描述】:

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

我有一个本地存储的 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");

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 非常感谢。一个非常有用的提示
    【解决方案2】:

    如果您使用 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;
        }
    }
    

    【讨论】:

      【解决方案3】:

      这里是对应的 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"/>
      

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

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

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

      【讨论】:

      • proxy host = ProxyServer property, port = ProxyServerPort property,好像没看懂你的问题
      【解决方案4】:

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

      看这里: Authenticated HTTP proxy with Java

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-11
        • 2012-01-03
        • 2017-08-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多