使用具有多个NIC的Axis2服务器获取客户端绑定的IP地址

时间:2013-07-30 09:48:27

标签: java axis2

我通过存根连接到Axis 2服务器:

MyServiceStub fcc = new MyServiceStub(url);

建立连接后,由于我在不同子网上的计算机上配置了不同的IP地址,我想知道与服务器建立连接的地址是什么。

例如,如果我有一台具有2个NIC和IP地址的机器以这种方式配置:

eth0 ---> 192.165.0.1
eth1 ---> 10.0.3.1

我在IP:10.0.3.33上有一个Axis服务器,一旦建立连接,我怎么知道有界IP地址?

如何获取连接的客户端IP地址(在这种情况下:10.0.3.1)?

2 个答案:

答案 0 :(得分:0)

也许你可以试试这样的东西

request.getHeader("url");
String ipAddress = request.getHeader("X-FORWARDED-FOR");
   if (ipAddress == null) {
        ipAddress = request.getRemoteAddr();
   }

答案 1 :(得分:0)

如果您使用https协议来调用axis2 Web服务,您可以像这样实现自己的SecureProtocolSocketFactory:

public class CustomProtocolSocketFactory implements SecureProtocolSocketFactory {
    private static final Logger logger = Logger.getLogger("trace");
    InetAddress localAddress=null;
    SSLContext ctx=null;
    Socket socket=null;
    public CustomProtocolSocketFactory(SSLContext ctx) {
        this.ctx = ctx;
    }
    public CustomProtocolSocketFactory(SSLContext ctx, InetAddress localAddress) {
        this.ctx = ctx;
        this.localAddress=localAddress;
    }
    public CustomProtocolSocketFactory(SSLContext ctx, InetAddress localAddress, Socket socket) {
        this.ctx = ctx;
        this.localAddress=localAddress;
        this.socket=socket;
    }
    @Override
    public Socket createSocket(String host, int port, InetAddress localAddress, int localPort)
            throws IOException, UnknownHostException {
        logger.debug("createSocket(String host, int port, InetAddress localAddress, int localPort)");
        return ctx.getSocketFactory().createSocket(host, port, localAddress, localPort);
    }
    @Override
    public Socket createSocket(String host, int port, InetAddress localAddress, int localPort,
            HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
        logger.debug("createSocket(String host, int port, InetAddress localAddress, int localPort,HttpConnectionParams params)");
        if (params == null) {
            throw new IllegalArgumentException("Parameters may not be null");
        }
        int timeout = params.getConnectionTimeout();
        SocketFactory socketfactory = ctx.getSocketFactory();
        if (timeout == 0) {
            if(this.localAddress!=null){
                return socketfactory.createSocket(host, port, this.localAddress, localPort);
            }
            else{
                return socketfactory.createSocket(host, port, localAddress, localPort);
            }
        } else {
            Socket socket = socketfactory.createSocket();
            if(this.localAddress!=null){
                SocketAddress localaddr = new InetSocketAddress(this.localAddress, localPort);
                SocketAddress remoteaddr = new InetSocketAddress(host, port);
                socket.bind(localaddr);
                socket.connect(remoteaddr, timeout);
                return socket;
            }
            else{
                SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
                SocketAddress remoteaddr = new InetSocketAddress(host, port);
                socket.bind(localaddr);
                socket.connect(remoteaddr, timeout);
                return socket;
            }
        }
    }
    @Override
    public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
        if(localAddress!=null){
            logger.debug("createSocket(String host, int port) localAddress not null");
            return createSocket(host, port, localAddress, 0);
        }
        else{
            logger.debug("createSocket(String host, int port)");
            return ctx.getSocketFactory().createSocket(host, port);
        }
    }
    @Override
    public Socket createSocket(Socket socket, String host, int port, boolean autoClose)
            throws IOException, UnknownHostException {
        logger.debug("createSocket(Socket socket, String host, int port, boolean autoClose)");
        return ctx.getSocketFactory().createSocket(socket, host, port, autoClose);
    }
}

以某种方式可以将套接字与InetAddress localAddress绑定并使用如下:

proxy._getServiceClient().getOptions().setProperty(HTTPConstants.CUSTOM_PROTOCOL_HANDLER,
                            new Protocol("https",(ProtocolSocketFactory)new CustomProtocolSocketFactory(context,localAddress),port));