我正在实现我们的zuul过滤器集的添加。此附加过滤器将查看标头并确定应将请求重定向到我将配置的已知位置。
这是过滤器的run方法的主体
@Override public Object run() { log.debug("Running the PreRouteTransMarkAndLoggingZuulFilter filter "); // retrieve redirect URL String redirectURL = filterConfigurationBean.getRedirectURL(); if (redirectURL.isEmpty()) { return null; } // get the white list for allowed entries Set<String> whiteList = new HashSet<>(Arrays.asList(filterConfigurationBean.getWhiteList().split(","))); RequestContext ctx = RequestContext.getCurrentContext(); // if request url is part of white list then allow String url = ctx.getRequest().getRequestURL().toString(); if (checkWhiteList(url, whiteList)) { return null; } // get headers // check if an authorization header is present if (validHeader(ctx.getRequest())) { return null; } // if it got to here then if no header then redirect request try { ctx.getResponse().sendRedirect(redirectURL); } catch (IOException e) { log.error("unable to send a redirect to the login page"); } return null; }
好的,所以我实现并测试了它(它被定义为预过滤器,因为我不希望在路由阶段发送请求。在某个地方,更进一步,它抛出异常。
2017-06-26 17:00:36.482 WARN 6267 --- [tp1303192419-23] o.s.c.n.z.filters.post.SendErrorFilter : Error during filtering
com.netflix.zuul.exception.ZuulException: Filter threw Exception
at com.netflix.zuul.FilterProcessor.processZuulFilter(FilterProcessor.java:227)
at com.netflix.zuul.FilterProcessor.runFilters(FilterProcessor.java:157)
at com.netflix.zuul.FilterProcessor.postRoute(FilterProcessor.java:92)
at com.netflix.zuul.ZuulRunner.postRoute(ZuulRunner.java:87)
at com.netflix.zuul.http.ZuulServlet.postRoute(ZuulServlet.java:107)
at com.netflix.zuul.http.ZuulServlet.service(ZuulServlet.java:88)
at org.springframework.web.servlet.mvc.ServletWrappingController.handleRequestInternal(ServletWrappingController.java:157)
at org.springframework.cloud.netflix.zuul.web.ZuulController.handleRequest(ZuulController.java:44)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:50)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:841)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1650)
at org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter.doFilter(WebSocketUpgradeFilter.java:206)
at org.eclipse