I noticed that some of my custom filters are not applied to Actuator endpoints when a different management port is defined.
Even using :
@ManagementContextConfiguration
public class MicroServiceManagementAutoConfiguration {
@Bean
public FilterRegistrationBean<TestFilter> testFilter() {
final FilterRegistrationBean<TestFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new TestFilter());
registrationBean.addUrlPatterns("/*");
return registrationBean;
}
}
// registered in META-INF/spring.factories#org.springframework.boot.actuate.autoconfigure.ManagementContextConfiguration
The TestFilter is successfully applied when management.server.port == server.port but never applied when management.server.port != server.port.
Comment From: wilkinsona
9560 is related to this. We have an inconsistent story around what is and isn't applied and configured when a separate management port has been configured that we'd like to straighten out. This is another part of that.
Comment From: dnsymphony
This is a pretty big deal. That linked ticket is from 2017. It doesn't seem like this is getting the attention it deserves. Having the actuator endpoints on a different port is a critical security requirement for us (I work with Thibault). Not being able to configure our standard filters that add tracing, logging, auditing, etc. on these endpoints is a serious problem.
Comment From: wilkinsona
I would expect it to work with @ManagementContextConfiguration. I'll take a look and see why that's apparently not the case.
Comment From: wilkinsona
A filter defined in a @ManagementContextConfiguration class works for me when a separate management port has been configured. Can you please provide a minimal sample that reproduces the behaviour you're seeing where it's only effective when the server and management ports are the same?
Comment From: dnsymphony
@ManagementContextConfiguration was the first approach I had tried but it didn't work. I went back and looked at it again since you said it was working and realized that my class with that annotation was still being scanned by the main application context scanner even though I thought I had excluded it. Once I fixed the exclusion, it worked. Thanks for verifying it was working with two different ports and forcing me to go back (yet again) and revisit my code. You can close this ticket. Thanks for the help.
Comment From: wilkinsona
Great, thanks for following up. We'll leave the issue open as we'd like to offer an alternative that doesn't require the use of @ManagementContextConfiguration.
Comment From: dnsymphony
OK. Any idea what I need to do to get my custom ErrorController to work for the management port?
Comment From: chenmal
For Spring Boot 3:
- Exclude the filter from
@ComponentScan. Move the filter and the management filter configuration files out of the 'com.demo.dog' and 'com.demo.utils' packages to another package, namely 'com.demo.management'. Here, you can exclude them in@ComponentScan.
package com.demo.dog;
@SpringBootApplication
@ComponentScan(basePackages = {"com.demo.dog", "com.demo.utils"}
)
public class DogApplication { ... }
- Next, create a ManagementFilterConfiguration, and register the filter to the management context configuration using
@ManagementContextConfiguration.
package com.demo.management;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration;
@ManagementContextConfiguration
public class ManagementFilterConfiguration {
@Bean
public FilterRegistrationBean<DogFilter> managementFilter() {
FilterRegistrationBean<DogFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new DogFilter());
registrationBean.setEnabled(true);
registrationBean.addUrlPatterns("/*");
return registrationBean;
}
}
- Now, create a file at the following path: 'src/main/resources/META-INF/spring/org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration.imports'.
4.Inside this file, include the class 'com.demo.management.ManagementFilterConfiguration'.
Here an example: https://github.com/spring-projects/spring-boot/blob/v2.7.4/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
Comment From: jaeyeolkim
Is there a way to register a ManagementFilterConfiguration without creating a ManagementContextConfiguration.imports file?