java - 如何在 spring-boot 应用程序中为 Apache Tomcat 中的 AccessLogValve 设置属性 requestAttributesEnabled?

标签 java spring tomcat spring-boot

我们有一个独立的 spring-boot 应用程序,我们希望在其中设置访问日志模式

  • X-forwarded-for header 存在于请求中:它应该作为第一个字段包含在日志中
  • X-forwarded-for header DOESN't exists in request: 它应该被 remote ip 地址替换

当我们使用以下设置运行我们的应用程序时,我们只会获得远程 ip 地址

server.tomcat.accesslog.directory=<path_to_log_director>
server.tomcat.accesslog.enabled=true
server.tomcat.accesslog.pattern=%h %l %u %t "%r" %s %b
server.tomcat.accesslog.prefix=access_log
server.tomcat.accesslog.suffix=.log

例如:

192.168.25.265 - - - [12/Sep/2016:10:20:56 +0200] "GET /myapp HTTP/1.1" 200 125922

我们还尝试将属性 server.tomcat.accesslog.pattern 设置为

%h %{X-Forwarded-For}i %l %u %t "%r" %s %b

然后我们得到远程 ip 地址和 X-forwarded-for header 值。

例如:

192.168.25.265 192.168.21.65 - - - [12/Sep/2016:10:20:56 +0200] "GET /myapp HTTP/1.1" 200 125922

但是,基于链接https://tomcat.apache.org/tomcat-7.0-doc/config/valve.html ,当 x-forwarded-for 不存在时,tomcat 支持包含远程 ip 地址的要求。这可以通过添加属性“requestAttributesEnabled”来实现

我们尝试添加属性 服务器.tomcat.accesslog.requestAttributesEnabled 但是没有效果。

它似乎没有实现,因为它不在此处:http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

我们使用 EmbeddedServletContainerCustomizer 的实现实现了一个解决方法,如 How do I configure the location and name of tomcat access log in spring-boot? 中所述, 我们添加的地方:

accessLogValve.setRequestAttributesEnabled(true);

它按预期工作。

但是我们更希望能够通过 spring-boot 将 requestAttributesEnabled 设置为配置属性,例如:

server.tomcat.accesslog.requestAttributesEnabled=true

而不是需要在我们所有的服务中使用这个定制器。

这个问题是否有更好的解决方案,是否有其他属性可以使用,或者它是一个可以预期在不久的将来交付的功能?

最佳答案

你是对的,这个属性没有直接公开,你所做的是推荐的方法。话虽如此,我已经创建了 #7367讨论我们是否应该将其添加为内置属性。请注意该问题以获取进一步更新。谢谢!

关于java - 如何在 spring-boot 应用程序中为 Apache Tomcat 中的 AccessLogValve 设置属性 requestAttributesEnabled?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40150891/

相关文章:

java - Jasperreports - 隐藏报告中的重复字段

spring - 找不到 SaajSoapMessage 的端点映射

tomcat - 服务器崩溃的原因有哪些?

java - 需要将 ArrayList 中的整数存储在尽可能少的容器中

java - 比较两个 throwable 的正确方法是什么?

java - 如何在 Spring Boot 中正确创建非持久存储库?

java - Web服务客户端身份验证导致Tomcat异常

java - 在 BeanInstantiationException (Tomcat) 之后禁止 Spring 启动

java - 歧义错误 - varargs 和相同类型的参数

java - Spring-boot Web 和公共(public)资源