I am working on a project with Spring Data Rest and JPA and I am trying to configure an HTTP interceptor. As per the reference docs, available in Spring Web MVC Docs - Handler Mapping Interceptor, I created a component that extends HandlerInterceptorAdapter as follows:
@Component
public class DBEditorTenantInterceptor extends HandlerInterceptorAdapter {
Logger logger = LoggerFactory.getLogger(DBEditorTenantInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
logger.debug("********** INTERCEPTION SUCCESSFUL **********");
return true;
}
}
And then, registered the interceptor by extending WebMvcConfig (as explained in Spring Web MVC Docs - Config Interceptors
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Autowired
DBEditorTenantInterceptor dbEditorTenantInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(dbEditorTenantInterceptor)
.addPathPatterns("/**");
}
}
When I issue HTTP requests to any URL that is not used by Spring Data REST such as /helloworld the Interceptor works as expected, as I see the logger output
017-10-26 13:16:24.689 DEBUG 17012 --- [p-nio-80-exec-4] c.c.v.d.DBEditorTenantInterceptor : ********** INTERCEPTION SUCCESSFUL **********
However, when the URL is used by spring data rest, my interceptor is not called. This applies to all URLs like /api/{existing entity in model}
Why is my interceptor not called for Spring Data Rest URLs ? What can I do to make my interceptor work for all requests ?
Thanks a lot in advance.
Spring Handler Interceptor The HandlerInterceptor contains three main methods: prehandle() – called before the execution of the actual handler. postHandle() – called after the handler is executed. afterCompletion() – called after the complete request is finished and the view is generated.
Spring Interceptor are used to intercept client requests and process them. Sometimes we want to intercept the HTTP Request and do some processing before handing it over to the controller handler methods. That's where Spring MVC Interceptor come handy.
Ad. Jakarta Restful Web Services includes an Interceptor API that allows developers to intercept request and response processing. This allows addressing some advanced concepts like authentication, caching, and compressing without polluting application code.
Filters can modify inbound and outbound requests and responses including modification of headers, entity and other request/response parameters. Interceptors are used primarily for modification of entity input and output streams. You can use interceptors for example to zip and unzip output and input entity streams.
By declaring a bean of type MappedInterceptor and injecting it with my interceptor - which extends HandlerInterceptorAdapter, my interceptor was picked up by Spring Data Rest and now works for any URL on the application.
This translated to the following implementation (replacing the one in my original question):
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Autowired
DBEditorTenantInterceptor dbEditorTenantInterceptor;
@Bean
public MappedInterceptor dbEditorTenantInterceptor() {
return new MappedInterceptor(new String[]{"/**"}, dbEditorTenantInterceptor);
}
}
Unfortunately, I could not find any references to this on the Spring documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With