lbvirgo
lbvirgo

Reputation: 404

Interceptor not getting initialized and invoked with SpringBoot

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 !

Upvotes: 11

Views: 19154

Answers (4)

Satya Alapati
Satya Alapati

Reputation: 31

if you are using java 8, the recommended implementation is WebMvcConfigurationSupport. WebMvcConfigurerAdapter is deprecated.

Upvotes: 3

Vish
Vish

Reputation: 346

I had same issue. @SpringBootApplication scans all the component from the current package only. In order to scan other package, we need to specify package in @ComponentScan. For Example

package com.main;
@SpringBootApplication
public class Application {
     //Code goes here.
}

Now i want interceptor to be register, which is neither in com.main not com.main.**, its available in com.test package.

package com.test.interceptor
@Configuration
public class InterceptorConfig extends WebMvcConfigurerAdapter {
    @Autowired
    HeaderInterceptor headerInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(headerInterceptor);
    }
}

In above case, Spring Boot won't register HeaderInterceptor in context. In order to register, we must explicitly scan that package.

package com.main;
@SpringBootApplication
@ComponentScan("com.*") //Which takes care all the package which starts with com.
@ComponentScan({"com.main.*","com.test.*"}) //For specific packages
public class Application {
     //Code goes here.
}

Upvotes: 3

lbvirgo
lbvirgo

Reputation: 404

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.

Upvotes: 6

kuhajeyan
kuhajeyan

Reputation: 11077

@Configuration
@EnableWebMvc 
public class AppConfig extends WebMvcConfigurerAdapter  {  
    // @Bean resolvers , etc

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(new DGVProxySvcRequestInterceptor()).addPathPatterns("/controller/action/*");
    }
} 

Upvotes: 2

Related Questions