如何拦截从Spring拦截器到节点服务的请求

这是我正在做的事情:

  • 我有API-GATEWAY(这是在春季启动 ),我想要使用拦截器来validation请求。 如果请求具有正确的凭据,则会调用其他服务( Node.js中的服务
  • API-GATEWAY正在运行在8765 。 我打电话只使用这个8765其他服务
  • 有4个Node.js服务,我希望每个服务调用都需要在API-GATEWAY的拦截器中进行身份validation,这是我使用registry.addInterceptor(tokenValidateInterceptor()).addPathPatterns("/**");
  • docker容器内托pipespring-boot和node.js服务。
  • 我正在使用API​​-GATEWAY中的zuul路由来调用其他node.js服务。
  • 所有调用其他服务的configuration工作正常,因为我可以通过API-GATEWAY访问其他服务。

对我来说,showstopper是API-GATEWAY中的拦截器。我在这里观察到一个奇怪的场景,我想提到

如果Node.js服务停止拦截器工作正常。但是,如果Node.js服务正在运行它甚至不执行拦截器 ,但是调用正在通过API-GATEWAY,我得到了Node.js服务所需的响应。

这是我的代码片段:

 @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("/**"); } 

拦截器

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

build议更改或告诉我如果我正在执行某些错误。