簡體   English   中英

如何攔截從Spring Interceptor到Node Service的請求

[英]How to intercept the request from Spring Interceptor to Node Service

這是我想要做的:

  • 我有API-GATEWAY(在spring-boot中 ),我想使用攔截器來驗證請求。 如果請求具有適當的憑據,它將調用其他服務(在Node.js中
  • API-GATEWAY正在運行於8765 我只使用這個8765調用其他服務
  • 有4個Node.js服務,我希望每個服務調用都需要在API-GATEWAY的攔截器中進行身份驗證,這是我使用registry.addInterceptor(tokenValidateInterceptor()).addPathPatterns("/**");的原因registry.addInterceptor(tokenValidateInterceptor()).addPathPatterns("/**");
  • spring-boot和node.js服務都托管在docker容器中。
  • 我在API-GATEWAY中使用zuul路由來調用其他node.js服務。
  • 調用其他服務的所有配置都正常,因為我可以通過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);


}

建議更改或告訴我如果我實施了錯誤。

嘗試以下配置。 它應該工作。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM