Spring MVC Interceptor Configuration
The SpringMVCConfig class, which implements WebMvcConfigurer, has an addInterceptors() function that has to be overridden for the Spring Boot project. Our own implementation is represented by the SpringMVCConfig class.
Java
@Configuration public class SpringMVCConfig implements WebMvcConfigurer { @Autowired EmployeeSecurityInterceptor employeeSecurityInterceptor; @Override public void addInterceptors( final InterceptorRegistry registry) { registry.addInterceptor(employeeSecurityInterceptor); } } |
Spring interceptor XML configuration
You can add route patterns on which the interceptor will be called with the aid of XML configuration. As an alternative, we may set up the interceptor to be called in response to every web request.
XML
<!-- Configures Interceptors --> < mvc:interceptors > <!-- This XML will intercept all URIs --> < bean class = "org.w3wiki.interceptor.DemoInterceptor" ></ bean > <!-- This XML will apply interceptor to only configured URIs --> <!-- <mvc:interceptor> <mvc:mapping path="/users"></mvc:mapping> <bean class="org.w3wiki.interceptor.DemoInterceptor"></bean> <mvc:interceptor> --> </ mvc:interceptors > |
Spring MVC – HandlerInterceptor
Spring interceptor is functional, so let’s take a step back and examine the handler mapping. The HandlerInterceptor interface is a handy class offered by Spring. We may override only the relevant methods out of the three by extending this. The HandlerInterceptor interface is implemented by the HandlerInterceptor class, or it can be extended by the Spring Interceptor class.