7

Tôi biết điều này là không chính xác cách chính xác để đặt một câu hỏi, nhưng tôi đang gặp một vấn đề:WS khách hàng với Proxy và Autentification

Tôi có một wsdl lưu trữ cục bộ, và tôi cần phải tạo ra một Web Khách hàng dịch vụ gọi dịch vụ web đó. Vấn đề là dịch vụ nằm sau tường lửa và tôi phải kết nối với nó thông qua proxy và sau đó tôi phải xác thực để kết nối với WS.

gì tôi đã làm là tạo ra khách hàng WS với Apache CXF 2.4.6 sau đó thiết lập một hệ thống proxy rộng

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

Tôi biết điều này không phải là một thực hành tốt nhất, vì vậy hãy đề nghị một giải pháp tốt hơn, còn nếu bất cứ ai có thể cho tôi một lời khuyên về cách thiết lập xác thực I'dd thực sự đánh giá cao nó

Trả lời

16

với 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

Cảm ơn rất nhiều. Một mẹo hữu ích thực sự –

0

Bạn cũng có thể đặt tên proxy và mật khẩu sử dụng java.net.Authent icator lớp, nhưng tôi không chắc chắn nếu nó không phải là "hệ thống rộng" thiết lập.

Look đây: Authenticated HTTP proxy with Java

4

Đây là tương ứng với cấu hình Spring XML:

Tài liệu: 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> 

Để cho tiện làm việc bạn nên nhập cxf.xml:

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

Lưu ý rằng httpConduit này sẽ được kích hoạt cho tất cả các máy khách CXF của bạn (nếu có).

Bạn nên cấu hình tên ống dẫn của bạn để phù hợp với chỉ Conduit dịch vụ của bạn:

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

proxy host = 'ProxyServer' property, port =' ProxyServerPort' tài sản, có vẻ như tôi không hiểu câu hỏi của bạn – yunandtidus

4

Nếu you're sử dụng cấu hình Spring Java, để cấu hình một Client JAX-WS với Apache CXF (3.xx), các mã sau đây sẽ hoạt động:

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

nó làm việc cho tôi quá! cảm ơn bạn! – ncowboy