Java Apache httpclient interceptor for Spring MVC Configuration
--
If you want to add an HTTP interceptor with the Spring MVC Configuration this is just the right code for you.
1. Add the Class “SecurityWebInterceptor.class” file with:
import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;public class SecurityWebInterceptor extends HandlerInterceptorAdapter {@Overridepublic boolean preHandle(HttpServletRequest requestServlet, HttpServletResponse responseServlet, Object handler) throws Exception{System.out.println("MINIMAL: INTERCEPTOR PREHANDLE CALLED");return true;}@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception{System.out.println("MINIMAL: INTERCEPTOR POSTHANDLE CALLED");}@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) throws Exception{System.out.println("MINIMAL: INTERCEPTOR AFTERCOMPLETION CALLED");}}
2. Frist add in the configuration the following:
public class WebConfiguration implements WebMvcConfigurer {
... @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new SecurityWebInterceptor()); } ...}
Now for each request made in your backend the Interceptor should be called and you should find a console log. Now you can modify every request you want in the SecurityWebInterceptor class.