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;
@ConditionalOnExpression("${zuul.enabled:true}")
public class ZuulConfiguration {
@Autowired(required=false)
private TraceRepository traces;
@Autowired
private DiscoveryClient discovery;
@Autowired
private ZuulProperties zuulProperties;
@Autowired
private Map<String, ZuulFilter> filters;
@Bean
public RouteLocator routes(){
return new RouteLocator(discovery, zuulProperties);
}
@Bean
public ZuulController zuulController() {
return new ZuulController();
}
@Bean
public ZuulHandlerMapping zuulHandlerMapping() {
return new ZuulHandlerMapping(routes(), zuulController(), zuulProperties);
}
@Bean
public FilterInitializer zuulFilterInitializer() {
return new FilterInitializer(filters);
}
// pre filters
@Bean
public DebugFilter debugFilter() {
return new DebugFilter();
}
@Bean
public PreDecorationFilter preDecorationFilter() {
return new PreDecorationFilter(routes(), zuulProperties);
}
@Bean
public Servlet30WrapperFilter servlet30WrapperFilter() {
return new Servlet30WrapperFilter();
}
// route filters
@Bean
public RibbonRoutingFilter ribbonRoutingFilter() {
RibbonRoutingFilter filter = new RibbonRoutingFilter();
if (traces!=null) {
filter.setTraces(traces);
}
return filter;
}
@Bean
public SimpleHostRoutingFilter simpleHostRoutingFilter() {
return new SimpleHostRoutingFilter();
}
// post filters
@Bean
public SendResponseFilter sendResponseFilter() {
return new SendResponseFilter();
}
@Bean
public SendErrorFilter sendErrorFilter() {
return new SendErrorFilter();
}
@Autowired(required = false)
private TraceRepository traces;
@Autowired
private DiscoveryClient discovery;
@Autowired
private ZuulProperties zuulProperties;
@Bean
public RouteLocator routes() {
return new RouteLocator(discovery, zuulProperties);
}
@Bean
public ZuulController zuulController() {
return new ZuulController();
}
@Bean
public ZuulHandlerMapping zuulHandlerMapping() {
return new ZuulHandlerMapping(routes(), zuulController(), zuulProperties);
}
@Configuration
protected static class ZuulFilterConfiguration {
@Autowired
private Map<String, ZuulFilter> filters;
@Bean
public FilterInitializer zuulFilterInitializer() {
return new FilterInitializer(filters);
}
}
// pre filters
@Bean
public DebugFilter debugFilter() {
return new DebugFilter();
}
@Bean
public PreDecorationFilter preDecorationFilter() {
return new PreDecorationFilter(routes(), zuulProperties);
}
@Bean
public Servlet30WrapperFilter servlet30WrapperFilter() {
return new Servlet30WrapperFilter();
}
// route filters
@Bean
public RibbonRoutingFilter ribbonRoutingFilter() {
RibbonRoutingFilter filter = new RibbonRoutingFilter();
if (traces != null) {
filter.setTraces(traces);
}
return filter;
}
@Bean
public SimpleHostRoutingFilter simpleHostRoutingFilter() {
return new SimpleHostRoutingFilter();
}
// post filters
@Bean
public SendResponseFilter sendResponseFilter() {
return new SendResponseFilter();
}
@Bean
public SendErrorFilter sendErrorFilter() {
return new SendErrorFilter();
}
}
......@@ -3,10 +3,13 @@ package org.springframework.cloud.netflix.zuul;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.netflix.zuul.ZuulFilter;
@SpringBootApplication
@RestController
@EnableZuulProxy
......@@ -38,6 +41,28 @@ public class SampleZuulProxyApplication {
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) {
SpringApplication.run(SampleZuulProxyApplication.class, args);
}
......
......@@ -38,6 +38,7 @@ public class SampleZuulProxyApplicationTests {
ResponseEntity<String> result = new TestRestTemplate().exchange("http://localhost:" + port + "/self/1",
HttpMethod.DELETE, new HttpEntity<Void>((Void) null), String.class);
assertEquals(HttpStatus.OK, result.getStatusCode());
assertEquals("Deleted!", result.getBody());
}
}
......@@ -6,6 +6,9 @@ spring:
eureka:
server:
enabled: false
client:
registerWithEureka: false
fetchRegistry: false
#error:
# path: /myerror
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