【问题标题】:Get Requesting User's IP Address Within Apache XMLRPC environment?在 Apache XMLRPC 环境中获取请求用户的 IP 地址?
【发布时间】:2012-04-22 14:31:44
【问题描述】:

我有一个非常基本的 XMLRPC Servlet 服务器正在运行 - 完全遵循 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;
}

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

非常感谢!!

【问题讨论】:

    标签: java jakarta-ee servlets xml-rpc


    【解决方案1】:

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

    另外:在常见问题解答中,您将找到 this snippet 以获取 IP 并将其存储为 ThreadLocal,以便您以后可以访问它(也许这比您想要的更多)。

    sn-p 的复制品是:

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

    【讨论】:

    • 效果很好!非常感谢!
    猜你喜欢
    • 2011-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-19
    • 1970-01-01
    • 1970-01-01
    • 2020-03-17
    相关资源
    最近更新 更多