Spring Boot Adding Http Request Interceptors

Spring Boot Adding Http Request Interceptors

Since you're using Spring Boot, I assume you'd prefer to rely on Spring's auto configuration where possible. To add additional custom configuration like your interceptors, just provide a configuration or bean of WebMvcConfigurerAdapter.

Here's an example of a config class:

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

@Autowired
HandlerInterceptor yourInjectedInterceptor;

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(...)
...
registry.addInterceptor(getYourInterceptor());
registry.addInterceptor(yourInjectedInterceptor);
// next two should be avoid -- tightly coupled and not very testable
registry.addInterceptor(new YourInterceptor());
registry.addInterceptor(new HandlerInterceptor() {
...
});
}
}

NOTE do not annotate this with @EnableWebMvc, if you want to keep Spring Boots auto configuration for mvc.

Spring Boot RestController: Intercept incoming requests

Spring-boot allows us to configure custom interceptors.Usually in a spring boot application everything is auto configured and in such cases we can customize it by using the WebMvcConfigurerAdapter.Just extend WebMvcConfigurerAdapter and provide the configurations that you need in this class.

Remember to add @Configuration annotation so that this class will be picked up by spring during component scan.

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

@Autowired
HandlerInterceptor customInjectedInterceptor;

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(...)
...
registry.addInterceptor(customInjectedInterceptor).addPathPatterns("/**");
}
}

This is how you normally add interceptors to spring boot applications.Hope this might help answer your question.

From spring 5.x.x or spring-boot 2 onwards , WebMvcConfigurerAdapter is marked as deprecated.The WebMvcConfigurer interface (which is implemented by the abstract class WebMvcConfigurerAdapter), starting with Spring 5, contains default implementations for all its methods. As a result, the abstract adapter class was marked as deprecated.You can adopt it if you like as follows :

@Configuration
public WebConfig implements WebMvcConfigurer {
// ...
}

How can i use multiple interceptors for specific request such as POST, Get and PUT?

You can define all HTTP interceptors that you want, every interceptor should implement the logic of intercept an HTTP request.

@Slf4j
@Component
public class GetRequestInterceptor extends HandlerInterceptorAdapter {

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

if (request.getMethod().equals(HttpMethod.GET.name())) {
log.info("intercepting GET request {}", request.getRequestURI());

}

return true;
}
}

@Slf4j
@Component
public class PostRequestInterceptor extends HandlerInterceptorAdapter {

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

if (request.getMethod().equals(HttpMethod.POST.name())) {
log.info("intercepting POST request {}", request.getRequestURI());
}

return true;
}
}

And then you have to register them in spring.

@RequiredArgsConstructor
@Configuration
public class WebConfigurer implements WebMvcConfigurer {

private final GetRequestInterceptor getInterceptor;
private final PostRequestInterceptor postRequestInterceptor;

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

}


Related Topics



Leave a reply



Submit