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 !
Let us create a Spring Boot Application that implements the Interceptor. Step 1: Create a maven project from Spring Initializr. Step 2: Give the group name, I am giving com.pixeltrice. Step 3: Fill the artifact field, spring-boot-application-with-interceptor. Step 4 : Add the Spring web dependency.
Spring MVC Handler In order to understand how a Spring interceptor works, let's take a step back and look at the HandlerMapping. The purpose of HandlerMapping is to map a handler method to a URL. That way, the DispatcherServlet will be able to invoke it when processing a request.
As the name implies, the interceptor invokes preHandle () before handling a request. By default, this method returns true to send the request further to the handler method. However, we can tell Spring to stop the execution by returning false. We can use the hook to log information about the request's parameters, like where the request comes from.
To work with interceptor, you need to create @Component class that supports it and it should implement the HandlerInterceptor interface. The following are the three methods you should know about while working on Interceptors −
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.
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.
}
@Configuration
@EnableWebMvc
public class AppConfig extends WebMvcConfigurerAdapter {
// @Bean resolvers , etc
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new DGVProxySvcRequestInterceptor()).addPathPatterns("/controller/action/*");
}
}
if you are using java 8, the recommended implementation is WebMvcConfigurationSupport. WebMvcConfigurerAdapter is deprecated.
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