How to set requestAttributesEnabled property for AccessLogValve in Apache Tomcat in spring-boot application?

We have a stand-alone spring-boot application where we want to set the access log template so that

  • The X-forwarded-for request header exists in the request: it must be included in the logs as the first field
  • the X-forwarded-for header does NOT exist in the request: it should be replaced with the remote IP address

When we launch our application with the following settings, we only get the remote IP address

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 

eg:

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

We also tried to set the server.tomcat.accesslog.pattern property to

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

then we get both the remote IP address and the value of the X-forwarded-for header.

eg:

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

However, based on the https://tomcat.apache.org/tomcat-7.0-doc/config/valve.html link, tomcat supports this requirement to enable the remote IP address when x-forwarded-for doesn’t exist. This can be achieved by adding the property "requestAttributesEnabled"

We tried to add the server.tomcat.accesslog.requestAttributesEnabled property, but there was no effect.

It doesn't seem to be implemented as it is not present here: http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

We applied a workaround using the EmbeddedServletContainerCustomizer implementation, as described in How to configure the location and name of the tomcat access log in spring-boot? where we added:

 accessLogValve.setRequestAttributesEnabled(true); 

and it worked as expected.

However, we would prefer to be able to set requestAttributesEnabled as a configuration property via spring-boot, for example:

 server.tomcat.accesslog.requestAttributesEnabled=true 

instead of using this customizer in all our services.

Is there a better solution to this problem, is there another property that will be used, or is it a function that can be expected in the near future?

+5
source share
1 answer

You are right, this property is not directly affected, and what you did is the recommended method. Having said that, I created # 7367 to discuss whether to add this as an inline property. Please see this issue for further updates. Thanks!

+3
source

Source: https://habr.com/ru/post/1258520/


All Articles