Commit 70c3d9e7 by Dave Syer

Ensure user-suppplied ZuulFilters are added properly

Bean instantiation ordering was causing external (user-supplied) filter beans to replace the defaults, not append to them. Separating out the autowired map of beans into a spearate class was enough to fix it. Fixes gh-78
parent 9add6f1e
...@@ -30,77 +30,82 @@ import com.netflix.zuul.http.ZuulServlet; ...@@ -30,77 +30,82 @@ import com.netflix.zuul.http.ZuulServlet;
@ConditionalOnExpression("${zuul.enabled:true}") @ConditionalOnExpression("${zuul.enabled:true}")
public class ZuulConfiguration { public class ZuulConfiguration {
@Autowired(required=false) @Autowired(required = false)
private TraceRepository traces; private TraceRepository traces;
@Autowired @Autowired
private DiscoveryClient discovery; private DiscoveryClient discovery;
@Autowired @Autowired
private ZuulProperties zuulProperties; private ZuulProperties zuulProperties;
@Autowired @Bean
private Map<String, ZuulFilter> filters; public RouteLocator routes() {
return new RouteLocator(discovery, zuulProperties);
@Bean }
public RouteLocator routes(){
return new RouteLocator(discovery, zuulProperties); @Bean
} public ZuulController zuulController() {
return new ZuulController();
@Bean }
public ZuulController zuulController() {
return new ZuulController(); @Bean
} public ZuulHandlerMapping zuulHandlerMapping() {
return new ZuulHandlerMapping(routes(), zuulController(), zuulProperties);
@Bean }
public ZuulHandlerMapping zuulHandlerMapping() {
return new ZuulHandlerMapping(routes(), zuulController(), zuulProperties); @Configuration
} protected static class ZuulFilterConfiguration {
@Bean @Autowired
public FilterInitializer zuulFilterInitializer() { private Map<String, ZuulFilter> filters;
return new FilterInitializer(filters);
} @Bean
public FilterInitializer zuulFilterInitializer() {
// pre filters return new FilterInitializer(filters);
@Bean }
public DebugFilter debugFilter() {
return new DebugFilter(); }
}
// pre filters
@Bean @Bean
public PreDecorationFilter preDecorationFilter() { public DebugFilter debugFilter() {
return new PreDecorationFilter(routes(), zuulProperties); return new DebugFilter();
} }
@Bean @Bean
public Servlet30WrapperFilter servlet30WrapperFilter() { public PreDecorationFilter preDecorationFilter() {
return new Servlet30WrapperFilter(); return new PreDecorationFilter(routes(), zuulProperties);
} }
// route filters @Bean
@Bean public Servlet30WrapperFilter servlet30WrapperFilter() {
public RibbonRoutingFilter ribbonRoutingFilter() { return new Servlet30WrapperFilter();
RibbonRoutingFilter filter = new RibbonRoutingFilter(); }
if (traces!=null) {
filter.setTraces(traces); // route filters
} @Bean
return filter; public RibbonRoutingFilter ribbonRoutingFilter() {
} RibbonRoutingFilter filter = new RibbonRoutingFilter();
if (traces != null) {
@Bean filter.setTraces(traces);
public SimpleHostRoutingFilter simpleHostRoutingFilter() { }
return new SimpleHostRoutingFilter(); return filter;
} }
// post filters @Bean
@Bean public SimpleHostRoutingFilter simpleHostRoutingFilter() {
public SendResponseFilter sendResponseFilter() { return new SimpleHostRoutingFilter();
return new SendResponseFilter(); }
}
// post filters
@Bean @Bean
public SendErrorFilter sendErrorFilter() { public SendResponseFilter sendResponseFilter() {
return new SendErrorFilter(); return new SendResponseFilter();
} }
@Bean
public SendErrorFilter sendErrorFilter() {
return new SendErrorFilter();
}
} }
...@@ -3,10 +3,13 @@ package org.springframework.cloud.netflix.zuul; ...@@ -3,10 +3,13 @@ package org.springframework.cloud.netflix.zuul;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import com.netflix.zuul.ZuulFilter;
@SpringBootApplication @SpringBootApplication
@RestController @RestController
@EnableZuulProxy @EnableZuulProxy
...@@ -38,6 +41,28 @@ public class SampleZuulProxyApplication { ...@@ -38,6 +41,28 @@ public class SampleZuulProxyApplication {
return "Hello world"; return "Hello world";
} }
@Bean
public ZuulFilter sampleFilter() {
return new ZuulFilter() {
@Override
public String filterType() {
return "pre";
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() {
return null;
}
@Override
public int filterOrder() {
return 0;
}
};
}
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(SampleZuulProxyApplication.class, args); SpringApplication.run(SampleZuulProxyApplication.class, args);
} }
......
...@@ -38,6 +38,7 @@ public class SampleZuulProxyApplicationTests { ...@@ -38,6 +38,7 @@ public class SampleZuulProxyApplicationTests {
ResponseEntity<String> result = new TestRestTemplate().exchange("http://localhost:" + port + "/self/1", ResponseEntity<String> result = new TestRestTemplate().exchange("http://localhost:" + port + "/self/1",
HttpMethod.DELETE, new HttpEntity<Void>((Void) null), String.class); HttpMethod.DELETE, new HttpEntity<Void>((Void) null), String.class);
assertEquals(HttpStatus.OK, result.getStatusCode()); assertEquals(HttpStatus.OK, result.getStatusCode());
assertEquals("Deleted!", result.getBody());
} }
} }
...@@ -6,6 +6,9 @@ spring: ...@@ -6,6 +6,9 @@ spring:
eureka: eureka:
server: server:
enabled: false enabled: false
client:
registerWithEureka: false
fetchRegistry: false
#error: #error:
# path: /myerror # path: /myerror
management: management:
......
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