7

我知道這不完全是問一個問題的正確方法,但我有一個問題:WS客戶端與代理服務器和Autentification

我有本地存儲的WSDL,我需要創建一個Web服務客戶端調用該Web服務。問題是服務在防火牆後面,我必須通過代理連接到它,然後我必須驗證連接到WS。

我做了與Apache CXF生成WS客戶端2.4.6然後設置一個全系統的代理

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

我知道這是不是最好的做法,所以請提出一個更好的解決辦法,如果還任何人都可以給我如何設置認證I'dd小費真的很感激

回答

16

與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"); 
+0

非常感謝。一個真的有用的提示 –

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"/> 

請注意,此httpConduit將爲您的所有CXF客戶端(如果有幾個)啓用。

您應該配置您的管道的名字只匹配您的服務管道:

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

代理主機='ProxyServer'屬性,端口='ProxyServerPort'屬性,似乎我不明白你的問題 – yunandtidus

4

如果you're使用Spring Java配置,配置JAX-WS客戶端與Apache CXF(3.xx的)時,以下代碼將起作用:

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; 
    } 
} 
+0

它也適用於我!謝謝! – ncowboy