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
This commit is contained in:
@@ -30,77 +30,82 @@ import com.netflix.zuul.http.ZuulServlet;
|
||||
@ConditionalOnExpression("${zuul.enabled:true}")
|
||||
public class ZuulConfiguration {
|
||||
|
||||
@Autowired(required=false)
|
||||
private TraceRepository traces;
|
||||
@Autowired(required = false)
|
||||
private TraceRepository traces;
|
||||
|
||||
@Autowired
|
||||
private DiscoveryClient discovery;
|
||||
@Autowired
|
||||
private DiscoveryClient discovery;
|
||||
|
||||
@Autowired
|
||||
private ZuulProperties zuulProperties;
|
||||
@Autowired
|
||||
private ZuulProperties zuulProperties;
|
||||
|
||||
@Autowired
|
||||
private Map<String, ZuulFilter> filters;
|
||||
@Bean
|
||||
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
|
||||
public FilterInitializer zuulFilterInitializer() {
|
||||
return new FilterInitializer(filters);
|
||||
}
|
||||
@Autowired
|
||||
private Map<String, ZuulFilter> filters;
|
||||
|
||||
// pre filters
|
||||
@Bean
|
||||
public DebugFilter debugFilter() {
|
||||
return new DebugFilter();
|
||||
}
|
||||
@Bean
|
||||
public FilterInitializer zuulFilterInitializer() {
|
||||
return new FilterInitializer(filters);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PreDecorationFilter preDecorationFilter() {
|
||||
return new PreDecorationFilter(routes(), zuulProperties);
|
||||
}
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Servlet30WrapperFilter servlet30WrapperFilter() {
|
||||
return new Servlet30WrapperFilter();
|
||||
}
|
||||
// pre filters
|
||||
@Bean
|
||||
public DebugFilter debugFilter() {
|
||||
return new DebugFilter();
|
||||
}
|
||||
|
||||
// route filters
|
||||
@Bean
|
||||
public RibbonRoutingFilter ribbonRoutingFilter() {
|
||||
RibbonRoutingFilter filter = new RibbonRoutingFilter();
|
||||
if (traces!=null) {
|
||||
filter.setTraces(traces);
|
||||
}
|
||||
return filter;
|
||||
}
|
||||
@Bean
|
||||
public PreDecorationFilter preDecorationFilter() {
|
||||
return new PreDecorationFilter(routes(), zuulProperties);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SimpleHostRoutingFilter simpleHostRoutingFilter() {
|
||||
return new SimpleHostRoutingFilter();
|
||||
}
|
||||
@Bean
|
||||
public Servlet30WrapperFilter servlet30WrapperFilter() {
|
||||
return new Servlet30WrapperFilter();
|
||||
}
|
||||
|
||||
// post filters
|
||||
@Bean
|
||||
public SendResponseFilter sendResponseFilter() {
|
||||
return new SendResponseFilter();
|
||||
}
|
||||
// route filters
|
||||
@Bean
|
||||
public RibbonRoutingFilter ribbonRoutingFilter() {
|
||||
RibbonRoutingFilter filter = new RibbonRoutingFilter();
|
||||
if (traces != null) {
|
||||
filter.setTraces(traces);
|
||||
}
|
||||
return filter;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SendErrorFilter sendErrorFilter() {
|
||||
return new SendErrorFilter();
|
||||
}
|
||||
@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:
|
||||
|
||||
Reference in New Issue
Block a user