在Apache XMLRPC环境中获取请求用户的IP地址?

时间:2012-04-10 09:44:48

标签: java java-ee servlets xml-rpc

我有一个非常基本的XMLRPC Servlet Server运行 - 字面上遵循Apache人员建议的默认值(http://ws.apache.org/xmlrpc/server.html)。

我有什么方法可以从我的XMLRPC函数中访问请求者的IP地址吗?我正在设计一种服务,通过IP地址记录从不同用户收到的请求。

例如,如果我从他们的例子中拿出Calculator类,我可能会做类似的事情,

public int add(int a, int b){
   IPAddress user = {magic incantation};
   Log.info("Summed " + a + " and " + b + " for " + user);
   return a + b;
}

(显然这是一个玩具示例,但如果我知道该怎么做,我可以在我的程序中做我想做的事情)

非常感谢!!

1 个答案:

答案 0 :(得分:1)

处理请求时,您可以访问HttpServletRequest的实例。该对象提供方法getRemoteAddr()

另请注意:在常见问题解答中,您会发现this snippet获取并将IP存储为ThreadLocal,因此您可以从此处访问它(可能这比您想要的更多)。

该片段的复制品是:

public static class ClientInfoServlet extends XmlRpcServlet {
    private static ThreadLocal clientIpAddress = new ThreadLocal();

    public static String getClientIpAddress() {
        return (String) clientIpAddress.get();
    }

    public void doPost(HttpServletRequest pRequest, HttpServletResponse pResponse)
            throws IOException, ServletException {
        clientIpAddress.set(pRequest.getRemoteAddr());
        super.doPost(pRequest, pResponse);
    }
}
相关问题