How to configure spring interceptor to call with every request

I want to configure spring interceptor so that every request is called.

  • I use interceptor in API-GATEWAY (Spring-Boot)
  • From API GATEWAY I call other microservices.
  • Calling other microservices from API-GATEWAY is working fine.
  • Other services that I call are Node.js Service, on the other hand, my API gateway is in spring boot.
  • All services (Node.js + Spring-Boot) run on the Docker Console .

I am facing a problem in Interceptor. I want to configure it in such a way that at each request it would be necessary to name it preHandle()and perform the operations that I wrote in it.

I noticed one problem that I want to mention here.

If the services that I call are stopped (not working), Interceptor is working correctly and gives me an answer such as somename-service was not found. If the same services are started at this time, the Interceptor is not executed.

Here is my code snippet

@EnableEurekaClient
@SpringBootApplication
@EnableZuulProxy
@Configuration
public class Application extends WebMvcConfigurerAdapter {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Autowired
    private TokenValidateInterceptor tokenValidateInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(tokenValidateInterceptor).addPathPatterns("/**");


    }

Interceptor

@Component
public class TokenValidateInterceptor extends HandlerInterceptorAdapter {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        LOG.info("#### Starting TokenValidateInterceptor.preHandle ####");

        String apiKey = null;
        try {
            apiKey = request.getHeader("apikey");

            LOG.info("The request come with apikey ======" + apiKey);

            LOG.info("Actual apikey ======" + azureApikey);


}
+8
source share
2 answers

You should add this interceptor to your XML manager file:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" p:interceptors-ref="tokenInterceptor" />

    <bean id="tokenInterceptor" class="yourpackage.TokenValidateInterceptor" />

</beans>

There are some good examples here:

0
source

First create the WebMvc Config class as shown below

@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor());
    }

Create a request interceptor, i.e. MyInterceptor by extending HandlerInterceptorAdapter

public class MyInterceptor extends HandlerInterceptorAdapter{
    @Override
    public void postHandle(HttpServletRequest request, 
    HttpServletResponse response, 
    Object handler, 
    ModelAndView modelAndView) throws Exception {
        ......Write your business logic here...........}
}

0

All Articles