Interface HandlerInterceptor
public interface HandlerInterceptor
Runs around every controller method, for cross-cutting work that is not security: timing, auditing, tenant selection, request-scoped cleanup.
Declare one as a bean -- a @Component class implementing this interface, or
a @Bean method returning one -- and the build hands it to every generated
router. Several run in @Order order, and the ones without an order last in
bean-name order.
Where it runs
- The security filter chain: authentication, CSRF, the URL rules. A request it refuses never reaches an interceptor.
- Route selection. Only a request that matched a controller route is intercepted; a 404 is not.
preHandle(HttpServer.Request, HandlerMethod), in order.- Binding, validation and the controller method, with its method security.
- Exception handlers, when 3 or 4 threw.
postHandle(HttpServer.Request, HandlerMethod, HttpServer.Response), in reverse order, only when the controller method returned normally.afterCompletion(HttpServer.Request, HandlerMethod, HttpServer.Response, Throwable), in reverse order, for every interceptor whosepreHandle(HttpServer.Request, HandlerMethod)let the request through -- whatever happened after it.
Because the chain sits inside the security layer, an interceptor cannot
admit a request security refused, and the failures security owns --
AccessDeniedException, AuthenticationException and their subclasses,
CSRF included -- pass every interceptor and exception handler untouched on
their way back to it.
-
Method Summary
Modifier and TypeMethodDescriptiondefault voidafterCompletion(HttpServer.Request request, HandlerMethod handler, HttpServer.Response response, Throwable failure) Called once the request is finished with, on success, on a refusal by a later interceptor, on a binding or validation failure and on a failure nothing handled.default voidpostHandle(HttpServer.Request request, HandlerMethod handler, HttpServer.Response response) Called after the controller method returned normally; not after a refusal, and not after an exception, handled or not.default HttpServer.ResponsepreHandle(HttpServer.Request request, HandlerMethod handler) Called before binding.
-
Method Details
-
preHandle
default HttpServer.Response preHandle(HttpServer.Request request, HandlerMethod handler) throws Exception Called before binding. Return null to let the request through, or a response to answer with instead -- the controller method is then not called, andafterCompletion(HttpServer.Request, HandlerMethod, HttpServer.Response, Throwable)runs only for the interceptors before this one.- Parameters:
request- the requesthandler- the route it matched- Returns:
- null to proceed, or the response to answer with
- Throws:
Exception- routed to the exception handlers like the controller's own
-
postHandle
default void postHandle(HttpServer.Request request, HandlerMethod handler, HttpServer.Response response) throws Exception Called after the controller method returned normally; not after a refusal, and not after an exception, handled or not.- Parameters:
request- the requesthandler- the route it matchedresponse- the response about to be written- Throws:
Exception- fails the request
-
afterCompletion
default void afterCompletion(HttpServer.Request request, HandlerMethod handler, HttpServer.Response response, Throwable failure) Called once the request is finished with, on success, on a refusal by a later interceptor, on a binding or validation failure and on a failure nothing handled. This is the place for cleanup. A throw from here is logged and does not change the response.- Parameters:
request- the requesthandler- the route it matchedresponse- the response, or null when the request failed unhandledfailure- what failed and was not handled, or null
-