2017-07-25 32 views
0

下面是Application.java。它有代碼來調用攔截使Zuul代理爲Spring應用程序啓動(我們已經成立miroservices的)後,Spring MVC的攔截亙古不變的工作

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

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

    @Bean 
    public TokenValidateInterceptor tokenValidateInterceptor() { 
     TokenValidateInterceptor localeChangeInterceptor = new TokenValidateInterceptor(); 
     System.out.println("In WEB MVC Intereptor, Interceptor returned"); 
     return localeChangeInterceptor; 
    } 

    @Override 
    public void addInterceptors(InterceptorRegistry registry) { 
     System.out.println("In WEB MVC Intereptor"); 
     // registry.addInterceptor(tokenValidateInterceptor()).addPathPatterns("/"); 
     registry.addInterceptor(tokenValidateInterceptor()).addPathPatterns("/api/**"); 
     // registry.addInterceptor(new 
     // TokenValidateInterceptor()).addPathPatterns("/api/**"); 
    } 

} 

下面是攔截器代碼的片段:

@Component 
public class TokenValidateInterceptor extends HandlerInterceptorAdapter { 

    private static final Logger LOG = Logger.getLogger(TokenValidateInterceptor.class); 

    // before the actual handler will be executed .. 
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { 
     String apikey = request.getHeader("apikey"); 
     // if (LOG.isDebugEnabled()) { 
     LOG.info("#### Starting TokenValidateInterceptor.preHandle ####"); 
     LOG.info("apikey-->" + apikey); 
     // } 
     if (StringUtils.isBlank(apikey) || apikey == null || apikey.isEmpty()) { 
       return true; 
     } 
} 
} 

但電話沒有達到攔截prehandle。

回答

0

據我所知,在Zuul路由中定義的所有請求由ZuulServlet處理。 Spring MVC的不處理這些請求,因此任何春012​​不會要求這些請求。如果您需要API請求任何預處理,你應該在Zuul預過濾器或Servlet過濾器實現它。