【问题标题】:Spring-enabled JAX-WS Web Service how to get client ip启用 Spring 的 JAX-WS Web 服务如何获取客户端 ip
【发布时间】:2012-02-12 13:19:45
【问题描述】:

我使用 Spring 3.1 SimpleJaxWsServiceExporter 来发布这样的 web 服务:

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

【问题讨论】:

    标签: java spring


    【解决方案1】:
    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();
    }
    

    【讨论】:

    • 这个是否依赖SUN JDK,如果应用运行在OpenJDK上呢?
    • 不幸的是,这在我在 Jetty 上运行的独立应用程序中不起作用,HttpExchange 为空。
    【解决方案2】:

    如果 MessageContext.SERVLET_REQUESTHttpExchange 不起作用。您可以使用 j2e 过滤器或 RequestListener

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

    【讨论】:

    • 赏金已结束,但我仍然需要尝试一下。谢谢。
    猜你喜欢
    • 2012-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-05
    • 1970-01-01
    • 1970-01-01
    • 2011-01-10
    相关资源
    最近更新 更多