java – 支持Spring的JAX-WS Web Service如何获取客户端ip

我使用
Spring 3.1 SimpleJaxWsServiceExporter来发布这样的webservice:

<bean class="org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter" >
    <property name="baseAddress" value="http://192.168.1.8:8888/" /></bean>
    <bean id="webServiceEndpoint" class="com.test.remoting.jaxws.WebServiceEndpoint">
</bean>

然后我尝试去获取客户端IP,但我的请求为空,请告诉我有没有错误?非常感谢!!

@Resource  
WebServiceContext wsContext; 

@WebMethod
public String Test(){
    MessageContext mc = wsContext.getMessageContext();
    HttpServletRequest req = (HttpServletRequest)mc.get(MessageContext.SERVLET_REQUEST); //here is always null
    return "aa";
}

最佳答案

private String getIP(){
    MessageContext mc = wsContext.getMessageContext();
    HttpExchange exchange = (HttpExchange)mc.get("com.sun.xml.internal.ws.http.exchange");
    System.out.print(exchange.getRemoteAddress().getAddress().getHostAddress());
    return exchange.getRemoteAddress().getAddress().getHostAddress();
}
点赞