WebService(JAX-WS、XFire、Axis三种)获取客户端ip
WebService(JAX-WS、XFire、Axis三种)获取客户端ip
JAX-WS、XFire、Axis三种webservice的获取客户端IP的简单实现过程:
1,基于JDK6 jax-ws开发的webservice获取客户端IP地址,Endpoint.publish() 轻量级HTTP服务发布,在web容器tomcat下发布;
2,基于XFire开发的webservice获取客户端IP地址;
3,基于Axis开发的webservice获取客户端IP地址;
一、基于JDK6 jax-ws开发的webservice获取客户端IP地址
1,如果以 Endpoint.publish() 的方式发布:
服务端接口实现类代码(获取的是调用webservice的服务器的IP):
package com.micmiu.jaxws.demo.impl; import java.net.InetSocketAddress; import javax.annotation.Resource; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; import javax.xml.ws.WebServiceContext; import javax.xml.ws.handler.MessageContext; import com.micmiu.jaxws.demo.HelloService; import com.sun.net.httpserver.HttpExchange; import com.sun.xml.internal.ws.developer.JAXWSProperties; @WebService() public class HelloServiceImpl implements HelloService { @Resource private WebServiceContext wsContext; @WebMethod public String sayHello(@WebParam(name = "userName") String userName) { getClientInfo();return ""; } private void getClientInfo() { try { MessageContext mc = wsContext.getMessageContext(); HttpExchange exchange = (HttpExchange) mc .get(JAXWSProperties.HTTP_EXCHANGE); InetSocketAddress isa = exchange.getRemoteAddress(); System.out.println("InetSocketAddress : " + isa); System.out.println("Hostname : " + isa.getAddress().getHostAddress() + " address: " + isa.getAddress().getHostName()); } catch (Exception e) { e.printStackTrace(); } } }
客户端调用后,服务端运行日志:
InetSocketAddress : /192.20.100.210:52462
Hostname : 192.20.100.210 address: ADMINIS-3HJ200
2,如果以web容器的方式发布(jetty 或 tomcat为例):
服务端接口实现 代码:
package com.micmiu.jaxws.demo2.impl; import javax.annotation.Resource; import javax.jws.WebService; import javax.servlet.http.HttpServletRequest; import javax.xml.ws.WebServiceContext; import javax.xml.ws.handler.MessageContext; import com.micmiu.jaxws.demo2.HelloService; @WebService(endpointInterface = "com.micmiu.jaxws.demo2.HelloService") public class HelloServiceImpl implements HelloService { @Resource private WebServiceContext wsContext; public String sayHello(String userName) { String clientIP = getClientInfo(); return "JAX-WS with IP: " + clientIP; } private String getClientInfo() { String clientIP = null; try { MessageContext mc = wsContext.getMessageContext(); HttpServletRequest request = (HttpServletRequest) (mc .get(MessageContext.SERVLET_REQUEST)); clientIP = request.getRemoteAddr(); System.out.println("client IP : " + clientIP); } catch (Exception e) { e.printStackTrace(); } return clientIP; } }
客户端调用后,服务端运行日志:
client IP : 192.20.100.210
二、基于XFire开发的webservice获取客户端IP地址
服务端接口实现类代码:
package com.micmiu.xfire.demo.base; import javax.servlet.http.HttpServletRequest; import org.codehaus.xfire.transport.http.XFireServletController; public class HelloWorldServiceImpl implements HelloWorldService{ public String sayHello(String username){ String clientIP = getClientInfo(); return ""; } public String getClientInfo() { String clientIP = null; try{ HttpServletRequest request = XFireServletController.getRequest(); System.out.println("Addr:"+request.getRemoteAddr()+" host:"+request.getRemoteHost()); clientIP = request.getRemoteAddr(); }catch(Exceptione){ e.printStackTrace(); } return clientIP; } }
客户端调用后,服务端的日志信息如下:
Addr : 192.20.100.210 host: 192.20.100.210
客户端IP :192.20.100.210
三、Axis开发的webservice获取客户端IP地址
服务端代码:
import javax.servlet.http.HttpServletRequest; import org.apache.axis.MessageContext; import org.apache.axis.transport.http.HTTPConstants; public class HelloWorld{ public String sayHello(String username){ String clientIP = getClientInfo(); return clientIP ; } private String getClientInfo() { MessageContext mc = null; HttpServletRequest request = null; String clientIP = null; try { mc = MessageContext.getCurrentContext(); request = (HttpServletRequest) mc.getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST); clientIP = request.getRemoteAddr();
System.out.println("Axis IP >>:"+clientIP); }catch (Exception e){ e.printStackTrace(); } return clientIP; } }
客户端调用后,服务端的日志信息如下:
Axis IP >>:192.20.100.210
注意:
在public class AuthFr extends HttpServlet(){}这种类中是获取不到客户端Ip,MessageContext.getCurrentContext()值为空;--待解决
当然,如果只是获取真实的客户端IP可以采用request.getRemoteAddr();
这里要获取的是A(真实客户端)通过访问B(服务器B)发布的网站再访问C(服务器C)的网站;这里服务器C上要获取服务器B的IP;
换句话理解就是只能通过服务器B才能访问到服务器C,其他IP都需要过滤;
这种情况,利用上诉方式在普通类中可以实现获取服务器B的IP,但是在public class AuthFr extends HttpServlet(){}这种类中确实null;