记录 Spring Security 过滤器链之前的请求头信息

huangapple go评论139阅读模式
英文:

Logging a request header before Spring Security filter chain

问题

我希望尽早记录所给进来的请求头的内容。

我了解诸如 CommonsRequestLoggingFilter 或日志记录 HandlerInterceptor 之类的方法,然而这些似乎只会在Spring执行了许多其他代码(如Spring安全过滤器链)之后才进行记录。

我想在Spring执行任何操作之前进行记录,根据一个单一要求尽早进行记录:日志消息需要能够从HTTP请求中提取一个头。

有没有办法实现这个?

英文:

I want to log the contents of a given incoming request header as early as possible.

I know about approaches like CommonsRequestLoggingFilter or a logging HandlerInterceptor, however these seem to only log after Spring has executed a lot of other code, such as the Spring Security filter chain.

I want to log before Spring has done any of that, as early as possible based on a single requirement: the log message needs to be able to extract a header from the HTTP request.

Is there a way to do this?

答案1

得分: 0

我已找到一种使用嵌入式Tomcat实现此目标的方法。由于Tomcat在Spring之前接收请求,您可以在此处捕获整个分派的请求。

public class CustomLoggerValve extends ValveBase {
    private static final Logger logger = LoggerFactory.getLogger(CustomLoggerValve.class);

    @Override
    public void invoke(Request request, Response response) throws IOException, ServletException {
        try {
            MDC.put("requestId", UUID.randomUUID().toString());
            logger.info("Received request");
            getNext().invoke(request, response);
        } finally {
            MDC.remove("requestId");
        }
    }
}

由于我正在使用非Spring Boot的Spring,我可以直接将这个添加到我的Tomcat中:

Tomcat tomcat = // ... 你的Tomcat设置
tomcat.getService().getContainer().getPipeline().addValve(new CustomLoggerValve());

我尚未尝试过,但在Spring Boot中似乎也可以相当容易地添加这个功能

据推测,类似的方法也适用于嵌入式Jetty或其他JVM Web服务器。

英文:

I have found a way to do this using the embedded Tomcat. Since this receives the request before Spring does, you can capture the entire dispatched request from here.

public class CustomLoggerValve extends ValveBase {
    private static final Logger logger = LoggerFactory.getLogger(CustomLoggerValve.class);

    @Override
    public void invoke(Request request, Response response) throws IOException, ServletException {
        try {
            MDC.put("requestId", UUID.randomUUID().toString());
            logger.info("Received request");
            getNext().invoke(request, response);
        } finally {
            MDC.remove("requestId");
        }
    }
}

Since I'm using Spring without Spring Boot, I can just add this to my Tomcat directly:

Tomcat tomcat = // ... your tomcat setup
tomcat.getService().getContainer().getPipeline().addValve(new CustomLoggerValve());

I haven't tried, but it looks like you could add this quite easily in Spring Boot too.

Presumably a similar approach would work with embedded Jetty/other JVM web servers.

huangapple
  • 本文由 发表于 2020年9月9日 02:06:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63799378.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定