Unverified Commit e87d9432 by Spencer Gibb

Merge pull request #883 from aivans/master

* pull883: Send 404 in zuul servlet when no route is found.
parents cef37773 d0841f79
...@@ -87,8 +87,8 @@ public class ZuulProxyConfiguration extends ZuulConfiguration { ...@@ -87,8 +87,8 @@ public class ZuulProxyConfiguration extends ZuulConfiguration {
@Bean @Bean
public PreDecorationFilter preDecorationFilter(RouteLocator routeLocator) { public PreDecorationFilter preDecorationFilter(RouteLocator routeLocator) {
return new PreDecorationFilter(routeLocator, return new PreDecorationFilter(routeLocator,
this.zuulProperties.isAddProxyHeaders(), this.server.getServletPrefix(),
this.zuulProperties.isRemoveSemicolonContent()); this.zuulProperties);
} }
// route filters // route filters
......
...@@ -90,6 +90,9 @@ public class SimpleRouteLocator implements RouteLocator { ...@@ -90,6 +90,9 @@ public class SimpleRouteLocator implements RouteLocator {
log.debug("servletPath=" + this.dispatcherServletPath); log.debug("servletPath=" + this.dispatcherServletPath);
log.debug("zuulServletPath=" + this.zuulServletPath); log.debug("zuulServletPath=" + this.zuulServletPath);
log.debug("RequestUtils.isDispatcherServletRequest()=" + RequestUtils.isDispatcherServletRequest());
log.debug("RequestUtils.isZuulServletRequest()=" + RequestUtils.isZuulServletRequest());
String adjustedPath = adjustPath(path); String adjustedPath = adjustPath(path);
...@@ -173,12 +176,14 @@ public class SimpleRouteLocator implements RouteLocator { ...@@ -173,12 +176,14 @@ public class SimpleRouteLocator implements RouteLocator {
&& StringUtils.hasText(this.dispatcherServletPath)) { && StringUtils.hasText(this.dispatcherServletPath)) {
if (!this.dispatcherServletPath.equals("/")) { if (!this.dispatcherServletPath.equals("/")) {
adjustedPath = path.substring(this.dispatcherServletPath.length()); adjustedPath = path.substring(this.dispatcherServletPath.length());
log.debug("Stripped dispatcherServletPath");
} }
} }
else if (RequestUtils.isZuulServletRequest()) { else if (RequestUtils.isZuulServletRequest()) {
if (StringUtils.hasText(this.zuulServletPath) if (StringUtils.hasText(this.zuulServletPath)
&& !this.zuulServletPath.equals("/")) { && !this.zuulServletPath.equals("/")) {
adjustedPath = path.substring(this.zuulServletPath.length()); adjustedPath = path.substring(this.zuulServletPath.length());
log.debug("Stripped zuulServletPath");
} }
} }
else { else {
......
...@@ -21,6 +21,8 @@ import java.net.URL; ...@@ -21,6 +21,8 @@ import java.net.URL;
import org.springframework.cloud.netflix.zuul.filters.Route; import org.springframework.cloud.netflix.zuul.filters.Route;
import org.springframework.cloud.netflix.zuul.filters.RouteLocator; import org.springframework.cloud.netflix.zuul.filters.RouteLocator;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.cloud.netflix.zuul.util.RequestUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.util.UrlPathHelper; import org.springframework.web.util.UrlPathHelper;
...@@ -37,13 +39,19 @@ public class PreDecorationFilter extends ZuulFilter { ...@@ -37,13 +39,19 @@ public class PreDecorationFilter extends ZuulFilter {
private boolean addProxyHeaders; private boolean addProxyHeaders;
private String dispatcherServletPath;
private String zuulServletPath;
private UrlPathHelper urlPathHelper = new UrlPathHelper(); private UrlPathHelper urlPathHelper = new UrlPathHelper();
public PreDecorationFilter(RouteLocator routeLocator, boolean addProxyHeaders, public PreDecorationFilter(RouteLocator routeLocator,
boolean removeSemicolonContent) { String dispatcherServletPath, ZuulProperties zuulProperties) {
this.routeLocator = routeLocator; this.routeLocator = routeLocator;
this.addProxyHeaders = addProxyHeaders; this.addProxyHeaders = zuulProperties.isAddProxyHeaders();
this.urlPathHelper.setRemoveSemicolonContent(removeSemicolonContent); this.urlPathHelper.setRemoveSemicolonContent(zuulProperties.isRemoveSemicolonContent());
this.dispatcherServletPath = dispatcherServletPath;
this.zuulServletPath = zuulProperties.getServletPath();
} }
@Override @Override
...@@ -121,7 +129,27 @@ public class PreDecorationFilter extends ZuulFilter { ...@@ -121,7 +129,27 @@ public class PreDecorationFilter extends ZuulFilter {
} }
else { else {
log.warn("No route found for uri: " + requestURI); log.warn("No route found for uri: " + requestURI);
ctx.set("forward.to", requestURI);
String fallBackUri = requestURI;
String fallbackPrefix = dispatcherServletPath; //default fallback servlet is DispatcherServlet
if (RequestUtils.isZuulServletRequest()) {
//remove the Zuul servletPath from the requestUri
log.debug("zuulServletPath=" + zuulServletPath);
fallBackUri = fallBackUri.replaceFirst(zuulServletPath, "");
log.debug("Replaced Zuul servlet path:" + fallBackUri);
} else {
//remove the DispatcherServlet servletPath from the requestUri
log.debug("dispatcherServletPath=" + dispatcherServletPath);
fallBackUri = fallBackUri.replaceFirst(dispatcherServletPath, "");
log.debug("Replaced DispatcherServlet servlet path:" + fallBackUri);
}
if (!fallBackUri.startsWith("/")) {
fallBackUri = "/" + fallBackUri;
}
String forwardURI = fallbackPrefix + fallBackUri;
forwardURI = forwardURI.replaceAll("//", "/");
ctx.set("forward.to", forwardURI);
} }
return null; return null;
} }
......
...@@ -52,10 +52,10 @@ public class PreDecorationFilterTests { ...@@ -52,10 +52,10 @@ public class PreDecorationFilterTests {
@Before @Before
public void init() { public void init() {
initMocks(this); initMocks(this);
this.properties = new ZuulProperties();
this.routeLocator = new DiscoveryClientRouteLocator("/", this.discovery, this.routeLocator = new DiscoveryClientRouteLocator("/", this.discovery,
this.properties); this.properties);
this.filter = new PreDecorationFilter(this.routeLocator, true, this.filter = new PreDecorationFilter(this.routeLocator, "/", this.properties);
this.properties.isRemoveSemicolonContent());
RequestContext ctx = RequestContext.getCurrentContext(); RequestContext ctx = RequestContext.getCurrentContext();
ctx.clear(); ctx.clear();
ctx.setRequest(this.request); ctx.setRequest(this.request);
...@@ -139,6 +139,105 @@ public class PreDecorationFilterTests { ...@@ -139,6 +139,105 @@ public class PreDecorationFilterTests {
getHeader(ctx.getOriginResponseHeaders(), "x-zuul-serviceid")); getHeader(ctx.getOriginResponseHeaders(), "x-zuul-serviceid"));
} }
@Test
public void routeNotFound() throws Exception {
this.properties.setPrefix("/api");
this.properties.setStripPrefix(true);
this.routeLocator.addRoute(
new ZuulRoute("foo", "/foo/**", null, "forward:/foo", true, null, null));
this.request.setRequestURI("/api/bar/1");
this.filter.run();
RequestContext ctx = RequestContext.getCurrentContext();
assertEquals("/api/bar/1", ctx.get("forward.to"));
}
@Test
public void routeNotFoundDispatcherServletSpecialPath() throws Exception {
this.properties.setPrefix("/api");
this.properties.setStripPrefix(true);
this.properties.setAddProxyHeaders(true);
this.routeLocator.addRoute(
new ZuulRoute("foo", "/foo/**", null, "forward:/foo", true, null, null));
this.filter = new PreDecorationFilter(this.routeLocator,
"/special", this.properties);
this.request.setRequestURI("/api/bar/1");
this.filter.run();
RequestContext ctx = RequestContext.getCurrentContext();
assertEquals("/special/api/bar/1", ctx.get("forward.to"));
}
@Test
public void routeNotFoundZuulRequest() throws Exception {
setTestRequestContext();
RequestContext ctx = RequestContext.getCurrentContext();
ctx.getCurrentContext().setZuulEngineRan();
this.request.setRequestURI("/zuul/api/bar/1");
ctx.setRequest(this.request);
this.properties.setPrefix("/api");
this.properties.setStripPrefix(true);
this.properties.setServletPath("/zuul");
this.routeLocator.addRoute(
new ZuulRoute("foo", "/foo/**", null, "forward:/foo", true, null, null));
this.filter.run();
assertEquals("/api/bar/1", ctx.get("forward.to"));
}
@Test
public void routeNotFoundZuulRequestDispatcherServletSpecialPath() throws Exception {
setTestRequestContext();
RequestContext ctx = RequestContext.getCurrentContext();
ctx.getCurrentContext().setZuulEngineRan();
this.request.setRequestURI("/zuul/api/bar/1");
ctx.setRequest(this.request);
this.properties.setPrefix("/api");
this.properties.setStripPrefix(true);
this.properties.setServletPath("/zuul");
this.properties.setAddProxyHeaders(true);
this.routeLocator.addRoute(
new ZuulRoute("foo", "/foo/**", null, "forward:/foo", true, null, null));
this.filter = new PreDecorationFilter(this.routeLocator,
"/special", this.properties);
this.filter.run();
assertEquals("/special/api/bar/1", ctx.get("forward.to"));
}
@Test
public void routeNotFoundZuulRequestZuulHomeMapping() throws Exception {
setTestRequestContext();
RequestContext ctx = RequestContext.getCurrentContext();
ctx.getCurrentContext().setZuulEngineRan();
this.request.setRequestURI("/api/bar/1");
ctx.setRequest(this.request);
this.properties.setPrefix("/api");
this.properties.setStripPrefix(true);
this.properties.setServletPath("/");
this.properties.setAddProxyHeaders(true);
this.routeLocator.addRoute(
new ZuulRoute("foo", "/foo/**", null, "forward:/foo", true, null, null));
this.filter = new PreDecorationFilter(this.routeLocator,
"/special", this.properties);
this.filter.run();
assertEquals("/special/api/bar/1", ctx.get("forward.to"));
}
private Object getHeader(List<Pair<String, String>> headers, String key) { private Object getHeader(List<Pair<String, String>> headers, String key) {
String value = null; String value = null;
for (Pair<String, String> pair : headers) { for (Pair<String, String> pair : headers) {
...@@ -150,4 +249,10 @@ public class PreDecorationFilterTests { ...@@ -150,4 +249,10 @@ public class PreDecorationFilterTests {
return value; return value;
} }
private void setTestRequestContext() {
RequestContext context = new RequestContext();
RequestContext.testSetCurrentContext(context);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment