如何从ContainerResponseFilter内部获取源地址/IP [英] How to get source address / ip from inside ContainerResponseFilter

查看:119
本文介绍了如何从ContainerResponseFilter内部获取源地址/IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个日志记录过滤器,该日志记录器记录了在Jersey中运行的Web应用程序的所有HTTP请求/响应. ContainerResponseFilter似乎是一个简单的解决方案,我设法使其正常工作.

I'm writing a logging filter that logs all HTTP requests / responses for a web app running in Jersey. ContainerResponseFilter seems to a straight forward solution and I've managed to get it to work.

下一步是记录请求的IP.有没有一种方法可以从ContainerResponseFilter内部执行此操作?

Next step is to log the IP of the requests. Is there a way to do that from inside the ContainerResponseFilter ?

推荐答案

简短答案:

@Provider
public class YourContextFilter implements ContainerRequestFilter {

    @Context
    private HttpServletRequest sr;

    @Override
    public synchronized void filter(ContainerRequestContext request) throws IOException {
        /*
         * Returns the Internet Protocol (IP) address of the client or 
         * last proxy that sent the request. For HTTP servlets, same as 
         * the value of the CGI variable REMOTE_ADDR.
         */
        String ip = sr.getRemoteAddr();
        // ... log it ...
    }

}


编辑
(希望获得更详细的答案)


EDIT
(regarding the wish for a more detailed answer)

Afaig:

@Context批注允许注入特定于 JAX-RS 的组件(可能会说您能够注入上下文信息对象). JAX-RS本身是基于HTTP的RESTful Web服务的基于Java的规范.这样我们就可以注入以下内容:

javax.ws.rs.core.UriInfo
javax.ws.rs.core.Request
javax.ws.rs.core.SecurityContext

The @Context annotation allows to inject JAX-RS–specific components (one might say you are able to inject contextual information objects). JAX-RS itself is a Java based specification for RESTful Web Services over HTTP protocol. So we are able to inject stuff like:

javax.ws.rs.core.UriInfo
javax.ws.rs.core.Request
javax.ws.rs.core.SecurityContext

还有
javax.servlet.http.HttpServletRequest

在Jersey文档的IOC章节中,您将找到以下说明:

In the IOC Chapter of the Jersey docs, you will find these notes:

[...] Jersey实现允许您直接将Ht​​tpServletRequest实例注入到JAX-RS组件中[...]- [...]特定请求对象存在例外,该请求对象甚至可以注入到构造函数或类字段中.对于这些对象,运行时将注入能够同时处理更多请求的代理.这些请求对象是HttpHeaders,Request,UriInfo,SecurityContext.可以使用@Context注释注入这些代理. [...]

[...] The exception exists for specific request objects which can injected even into constructor or class fields. For these objects the runtime will inject proxies which are able to simultaneously server more request. These request objects are HttpHeaders, Request, UriInfo, SecurityContext. These proxies can be injected using the @Context annotation. [...]

[...]当使用servlet部署JAX-RS应用程序时,可以使用@Context使用ServletConfig,ServletContext,HttpServletRequest和HttpServletResponse. [...]

[...] When deploying a JAX-RS application using servlet then ServletConfig, ServletContext, HttpServletRequest and HttpServletResponse are available using @Context. [...]

如果这样做,实际上会注入一个名为org.apache.catalina.connector.RequestFacade的代理(链接).

And if you do so, you inject in fact a Proxy named org.apache.catalina.connector.RequestFacade (link). This proxy functioned as your direct hotline to your Coyote (HTTP Connector) and thereby to the Coyote request object (link).

希望这对您有所帮助:)-祝您有美好的一天.

Hope this was helpful somehow :) - Have a nice day.

这篇关于如何从ContainerResponseFilter内部获取源地址/IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆