我有一个非常基本的 XMLRPC Servlet 服务器正在运行 - 实际上遵循 Apache 人员建议的默认设置 (http://ws.apache.org/xmlrpc/server.html)。
我有什么方法可以从我的 XMLRPC 函数中访问请求者的 IP 地址吗?我正在设计一项服务,用于记录通过 IP 地址从不同用户收到的请求。
例如,如果我从他们的示例中获取计算器类,我可能会做类似的事情,
public int add(int a, int b){
IPAddress user = {magic incantation};
Log.info("Summed " + a + " and " + b + " for " + user);
return a + b;
}
(显然这是一个玩具示例,但如果我知道该怎么做,我就可以在我的程序中做我想做的事)
非常感谢!!
最佳答案
在处理请求时,您可以访问 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);
}
}
关于java - 在 Apache XMLRPC 环境中获取请求用户的 IP 地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10086411/