Thursday, 25 May 2017

How to intercept the request from Spring Interceptor to Node Service

Here is what I am trying to do:

  • I have API-GATEWAY (Which is in spring-boot) where I want to use an interceptor to authenticate the request. If the request is with proper credentials it will call the other service (Which is in Node.js)
  • API-GATEWAY is running on 8765. I am calling other services using this 8765 only
  • There are 4 Node.js services, And I want every service call need to be authenticated in API-GATEWAY's interceptor as a reason I am using registry.addInterceptor(tokenValidateInterceptor()).addPathPatterns("/**");
  • Both spring-boot and node.js services are hosted inside docker container.
  • I am using zuul routes in API-GATEWAY to call other node.js services.
  • All the configuration for calling other services are working fine as I am able to hit other services through API-GATEWAY.

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

If the Node.js service is stopped interceptor is working fine.But if the Node.js services are running it's not even executing the Interceptor, But the call is going through API-GATEWAY and I am getting a required response from 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 the changes or tell me If I am implementing something wrong.



via Anand Deshmukh

No comments:

Post a Comment