How to intercept a request from Spring Interceptor to a Node service

Here is what I am trying to do:

  • I have an API-GATEWAY (which is located in spring-boot ) where I want to use an interceptor to authenticate a request. If the request has the correct credentials, it is called by another service (which is located in Node.js )
  • API GATEWAY works on 8765. I call other services using only8765
  • There are 4 Node.js. services And I want every service call to be authenticated in an API-GATEWAY interceptor as the reason I useregistry.addInterceptor(tokenValidateInterceptor()).addPathPatterns("/**");
  • Both spring-boot and Node.js services are hosted inside the docker container.
  • I use zuul routes in the GATEWAY API to call other Node.js services.
  • The whole configuration for calling other services works fine, since I can use other services through the API-GATEWAY.

For me, showstopper is an interceptor in the GATEWAY API. I noticed here one strange scenario that I want to mention

If the Node.js service is stopped, the interceptor is working fine. But if the Node.js services are running, it does not even execute the Interceptor , but the call passes through the API -GATEWAY, and I get the required response from the Node.js service.

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);


}

Suggest changes or tell me if I'm implementing something wrong.

+4
source share
1 answer

Try the configuration below. That should work.

@Configuration
public class MyConfiguration extends WebMvcConfigurerAdapter{
    @Override
    public void addInterceptors(InterceptorRegistry registry){
        registry.addInterceptor(new MyCustomInterceptor()).addPathPatterns("/**");
    }
}
0

Source: https://habr.com/ru/post/1677920/


All Articles