Skip to content

Support HTTP method-restricted interceptors in InterceptorRegistration #35272

@SRIRAM9487

Description

@SRIRAM9487

Overview

Spring MVC currently allows interceptors to be conditionally applied using path patterns via InterceptorRegistration. However, there is no built-in mechanism to restrict an interceptor's execution based on HTTP methods (e.g., only for POST and PUT requests).

Use Case

This feature would allow the configuration of interceptors that apply only to specific HTTP methods, which is useful for:

  • Applying auditing logic only to mutating requests (e.g., POST, DELETE)
  • Logging or metrics collection while ignoring idempotent requests (e.g., GET)
  • Separating concerns declaratively without polluting the interceptor body with manual checks

Current Workaround

At present, developers must implement method checks inside the interceptor itself, for example:

if (!request.getMethod().equals("POST")) {
    return true;
}

This leads to repetitive and manual handling in every interceptor where such conditional logic is needed.

Proposed Solution

Introduce an allowedMethods(HttpMethod... methods) method in InterceptorRegistration that wraps the original HandlerInterceptor with a delegating wrapper (MethodInterceptor) which conditionally invokes:

  • preHandle
  • postHandle
  • afterCompletion

Only if the request method matches one of the specified methods.

Example configuration:

registry.addInterceptor(new AuditInterceptor())
        .addPathPatterns("/api/**")
        .allowedMethods(HttpMethod.POST, HttpMethod.PUT);

This would ensure that the interceptor executes only for POST and PUT requests while skipping other methods such as GET, OPTIONS, etc., without interrupting request flow.

Status

An implementation has been prepared and tested. It includes:

  • A new MethodInterceptor class
  • Integration with InterceptorRegistration
  • Assertion to enforce that at least one HTTP method is specified
  • No breaking changes to existing behavior
  • Full test coverage

I am happy to submit a pull request for this enhancement and respond to any review feedback from the maintainers.

Please advise if this feature aligns with the direction of the framework.

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: supersededAn issue that has been superseded by another

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions