有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在springboot应用程序中为ApacheTomcat中的AccessLogValve设置属性requestAttributesEnabled?

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

  • 请求中存在X-forwarded-for标头:它应作为第一个字段包含在日志中
  • 请求中不存在X-forwarded-for标头:应将其替换为远程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

我们还尝试设置属性服务器。公猫访问日志。模式到

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

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

例如:

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,tomcat支持在x-forwarded-for不存在时包含远程ip地址的要求。这可以通过添加属性“requestAttributesEnabled”来实现

我们尝试添加属性 服务器公猫访问日志。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引导将requestAttributesEnabled设置为配置属性,如:

server.tomcat.accesslog.requestAttributesEnabled=true

而不是需要在我们的所有服务中使用此定制程序

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


共 (1) 个答案

  1. # 1 楼答案

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