How make OncePerRequestFilter called when management.port is different from server port?

I have a filter that extends OncePerRequestFilter. When I the management.port=8081 and the server.port=8080 (or any differing ports), my filter is not called on any 8081 Urls.

The filter is only called on 8080 Urls.

How do I make it called on all Urls, including those on 8081?

Filter:

@Order( Ordered.LOWEST_PRECEDENCE )
public class TestFilter extends OncePerRequestFilter
{
    public TestFilter()
    {
        System.out.println( "Started" );
    }

    @Override
    protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException
    {
        System.out.println( "Checked should not filter" );
        return false;
    }

    @Override
    public void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
        throws ServletException, IOException
    {
        System.out.println( "Filtering" );

        // continue the processing
        filterChain.doFilter( request, response );
    }
}

I add it by:

@Configuration
public class MyConfig
{
    @Bean
    public TestFilter testFilter()
    {
        return new TestFilter()
    }
}

EDIT: I tried adding @ManagementContextConfiguration to my config class, but this didn’t work either.

Answer

Although I was unable to find documentation, it appears the answer is to do all of the following:

  1. Add a class that’s annotated with @ManagementContextConfiguration
  2. Put that configuration file outside the component scan (so spring boot’s normal auto-config won’t find it)
  3. Declare it in META-INF/spring.factories:

META-INF/spring.factories:

before spring-boot-2.0.0.RELEASE:
org.springframework.boot.actuate.autoconfigure.ManagementContextConfiguration=com.packageoutsidescan.MyManagementFilterConfigurationClass

after spring-boot-2.0.0.RELEASE (web subpackage):

org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration=com.packageoutsidescan.MyManagementFilterConfigurationClass