The CXF client uses java.net.URLConnection to connect to the service. URLConnection can be configured to select a local IP address (see How to specify a local address on java.net.URLConnection? )
URL url = new URL(yourUrlHere); Proxy proxy = new Proxy(Proxy.Type.DIRECT, new InetSocketAddress( InetAddress.getByAddress( new byte[]{your, ip, interface, here}), yourTcpPortHere)); URLConnection conn = url.openConnection(proxy);
I checked the cxf-rt-rs-client and cxf-rt-transports-http artifact code to see how CXF creates the connection. In ProxyFactory , this is the code to create the Proxy object required by UrlConnection
private Proxy createProxy(final HTTPClientPolicy policy) { return new Proxy(Proxy.Type.valueOf(policy.getProxyServerType().toString()), new InetSocketAddress(policy.getProxyServer(), policy.getProxyServerPort())); }
As you can see, there is no way to configure the IP address, so I'm afraid that the answer to the question is, you cannot configure the source IP address using CXF
But, I think it would not be so difficult to change the source code to allow setting the source IP address
HTTPClientPolicy
Add the following code to org.apache.cxf.transports.http.configuration.HTTPClientPolicy in cxf-rt-transports-http
public class HTTPClientPolicy { protected byte[] sourceIPAddress; protected int port; public boolean isSetSourceIPAddress(){ return (this.sourceIPAddress != null); }
Proxyfactory
Change the following code to org.apache.cxf.transport.http.ProxyFactory to cxf-rt-transports-http
//added || policy.isSetSourceIPAddress() //getProxy() calls finally to createProxy public Proxy createProxy(HTTPClientPolicy policy, URI currentUrl) { if (policy != null) { // Maybe the user has provided some proxy information if (policy.isSetProxyServer() || policy.isSetSourceIPAddress()) && !StringUtils.isEmpty(policy.getProxyServer())) { return getProxy(policy, currentUrl.getHost()); } else { // There is a policy but no Proxy configuration, // fallback on the system proxy configuration return getSystemProxy(currentUrl.getHost()); } } else { // Use system proxy configuration return getSystemProxy(currentUrl.getHost()); } } //Added condition to set the source IP address (is set) //Will work also with a proxy private Proxy createProxy(final HTTPClientPolicy policy) { if (policy.isSetSourceIPAddress()){ Proxy proxy = new Proxy(Proxy.Type.DIRECT, new InetSocketAddress( InetAddress.getByAddress( policy.getSourceIPAddress(), policy.getPort())); } else { return new Proxy(Proxy.Type.valueOf(policy.getProxyServerType().toString()), new InetSocketAddress(policy.getProxyServer(), policy.getProxyServerPort())); } }
Using
Client client = ClientProxy.getClient(service); HTTPConduit http = (HTTPConduit) client.getConduit(); HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy(); httpClientPolicy.setSourceIPAddress(new byte[]{your, ip, interface, here})); httpClientPolicy.setPort(yourTcpPortHere); http.setClient(httpClientPolicy);