Interceptor not getting initialized and invoked with SpringBoot

interceptor, spring, spring-boot

Solution

Found the fix. Had to use the `ant` like url pattern to match the requests:

registry.addInterceptor(dgvProxySvcRequestInterceptor()).addPathPatterns("/**");

My original configuration was all good; did not require any change except for the above url pattern.

Problem

I've a Spring-Boot application that is packaged as a war with tomcat dependency as provided (so I've both options - use the packaged .war to run within embedded container after launching it via java -jar command AND also to run on a standalone servlet container). Below is my App main class ``` package com.mycompany.edsa.dgv.proxysvc; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.support.SpringBootServletInitializer; import org.springframework.context.annotation.Bean; //import org.springframework.context.annotation.Import; import org.springframework.context.annotation.ImportResource; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import com.mycompany.edsa.dgv.proxysvc.interceptor.DGVProxySvcRequestInterceptor; //import com.mycompany.edsa.dgv.proxysvc.config.ConfigExtension; @SpringBootApplication @ImportResource("classpath:dgv-proxy-svc-spring-ctx.xml") //@Import(ConfigExtension.class) public class DGVProxySvcAppMain extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(DGVProxySvcAppMain.class, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(DGVProxySvcAppMain.class); } @Bean public DGVProxySvcRequestInterceptor dgvProxySvcRequestInterceptor() { DGVProxySvcRequestInterceptor dgvProxySvcReqInterceptor = new DGVProxySvcRequestInterceptor(); return dgvProxySvcReqInterceptor; } @Bean public WebMvcConfigurerAdapter adapter() { return new WebMvcConfigurerAdapter() { @Override public void addInterceptors(InterceptorRegistry registry) { System.out.println("Adding interceptors"); registry.addInterceptor(dgvProxySvcRequestInterceptor()).addPathPatterns("/*"); super.addInterceptors(registry); } }; } ``` } My Interceptor class below: ``` package com.mycompany.edsa.dgv.proxysvc.interceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Component; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; @Component public class DGVProxySvcRequestInterceptor extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { /* Put in the code here to validate user principal before passing control to the controller */ //BXPPrincipal principal = (BXPPrincipal)SecurityContextHolder.getContext().getAuthentication().getPrincipal(); //Map<String, ?> result = principal.getAttributes(); System.out.println("Inside DGVProxySvcRequestInterceptor..."); return super.preHandle(request, response, handler); } } ``` However, on launching the application (I launch through `spring-boot:run` maven goal and using Eclipse IDE), I do not see my interceptor getting registered in the console logs. I tried launching it in debug mode with a breakpoint at the `public void addInterceptors(InterceptorRegistry registry)` method and I see that control comes at this point and the sysout message `"Adding interceptors"` get logged on console and also the `registry` contains my `DGVProxySvcRequestInterceptor` in its encapsulated ArrayList but not sure why there is no message on console mentioning anything about initialization of this interceptor bean. Moreover, on invoking my service, I do not see sysout message `"Inside DGVProxySvcRequestInterceptor..."` that has been put into my interceptor class' `preHandle` method (which confirms that the interceptor didn't get invoked). Can someone pls help me out in finding what configuration mistake I might be doing? Pls note - I tried extending my main class with `WebMvcConfigurerAdapter` instead of `SpringBootServletInitializer` and overriden the `addInterceptors` method to add my interceptor. In that case my interceptor gets initialized well (I can see on console logs) and also gets invoked when I invoke my service uri. So this tells me that something is not correct with my configuration when I try to use `SpringBootServletInitializer` (I've to use this initializer as I need to package my code as a war that can run on a standalone servlet container). Thanks in advance for any suggestions/pointers !

Original source