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?


public class TestFilter extends OncePerRequestFilter
    public TestFilter()
        System.out.println( "Started" );

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

    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:

public class MyConfig
    public TestFilter testFilter()
        return new TestFilter()

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


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:


before spring-boot-2.0.0.RELEASE:

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