1

I would like my HandlerInterceptor, in my spring boot application, not to run on requests coming in on the management port.

management.port = 9091

I can think of 2 ways to do it, but i'm looking for a more standard way.

One will be to check the port from within the HandlerInterceptor:

    @Override
    public boolean preHandle(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull Object handler) {
        if(request.getLocalPort() == managementPort){
           return true;
        }
        else{
             ....
        }
}

The second will be to exclude all paths, related to the managementPort when registering the interceptor:

@Override
public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myInterceptor).excludePathPatterns(managementPortEndpoints);
}

This can easily get out of sync.

Is there a more native way to achieve it? (spring configuration of some sort)

user12396421
  • 175
  • 1
  • 10

2 Answers2

0

I choose javax.servlet.Filter for filtering requests.

import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class BlockFilter implements Filter {
    private int managementPort = 9091;

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        if (request.getRemotePort() == managementPort) {
            chain.doFilter(request, response);
        } else {
            ((HttpServletResponse) response).setStatus(401);
        }
    }
}
Ismail Durmaz
  • 2,521
  • 1
  • 6
  • 19
0

Once all accesses to management.port are requests made for URLs that start with /actuator - spring will not run the filters and interceptors on the requests.

So, that seems like the way to go about it

user12396421
  • 175
  • 1
  • 10