我们有一个独立的 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/