WS client with credentials and authentication

I know this is not quite the right way to ask a question, but I have a problem:

I have wsdl stored locally and I need to create a web service client to call this web service. The problem is that the service is behind the firewall, and I have to connect to it through a proxy server, and after that I have to authenticate to connect to WS.

What I did was generate WS Client with Apache CXF 2.4.6, and then install the system proxy

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

I know this is not the best practice, so please offer a better solution, also if anyone can give me advice on how to set up authentication, I really appreciate that.

+7
source share
4 answers

With 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"); 
+16
source

Here is the corresponding Spring XML configuration:

Documentation: 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> 

For this to work, you must import cxf.xml:

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

Please note that this httpConduit will be enabled for all of your CXF clients (if several).

You must configure your channel name so that it matches only your service.

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

If you use the Spring Java configuration to configure the JAX-WS client with Apache CXF (3.xx), the following code will work:

 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; } } 
+4
source

You can also specify the username and password of the proxy server using the java.net.Authenticator class, but I'm not sure that it is not "system".

Take a look here: Simple HTTP Proxy with Java

0
source

All Articles