Shelly Chen
Shelly Chen

Reputation: 85

Spring-enabled JAX-WS Web Service how to get client ip

I use Spring 3.1 SimpleJaxWsServiceExporter to publish a webservice like this:

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

then I try go get client ip, but my request is null, please tell me is there anythings wrong? thanks very much!!

@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";
}

Upvotes: 4

Views: 3001

Answers (2)

benbenw
benbenw

Reputation: 753

If the MessageContext.SERVLET_REQUEST or HttpExchange don't work. You can use a j2e filter or RequestListener

See https://stackoverflow.com/a/139169/2257772

Upvotes: 0

Shelly Chen
Shelly Chen

Reputation: 85

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();
}

Upvotes: 2

Related Questions