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,7 +30,7 @@ import com.netflix.zuul.http.ZuulServlet; ...@@ -30,7 +30,7 @@ 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
...@@ -39,11 +39,8 @@ public class ZuulConfiguration { ...@@ -39,11 +39,8 @@ public class ZuulConfiguration {
@Autowired @Autowired
private ZuulProperties zuulProperties; private ZuulProperties zuulProperties;
@Autowired
private Map<String, ZuulFilter> filters;
@Bean @Bean
public RouteLocator routes(){ public RouteLocator routes() {
return new RouteLocator(discovery, zuulProperties); return new RouteLocator(discovery, zuulProperties);
} }
...@@ -57,11 +54,19 @@ public class ZuulConfiguration { ...@@ -57,11 +54,19 @@ public class ZuulConfiguration {
return new ZuulHandlerMapping(routes(), zuulController(), zuulProperties); return new ZuulHandlerMapping(routes(), zuulController(), zuulProperties);
} }
@Configuration
protected static class ZuulFilterConfiguration {
@Autowired
private Map<String, ZuulFilter> filters;
@Bean @Bean
public FilterInitializer zuulFilterInitializer() { public FilterInitializer zuulFilterInitializer() {
return new FilterInitializer(filters); return new FilterInitializer(filters);
} }
}
// pre filters // pre filters
@Bean @Bean
public DebugFilter debugFilter() { public DebugFilter debugFilter() {
...@@ -82,7 +87,7 @@ public class ZuulConfiguration { ...@@ -82,7 +87,7 @@ public class ZuulConfiguration {
@Bean @Bean
public RibbonRoutingFilter ribbonRoutingFilter() { public RibbonRoutingFilter ribbonRoutingFilter() {
RibbonRoutingFilter filter = new RibbonRoutingFilter(); RibbonRoutingFilter filter = new RibbonRoutingFilter();
if (traces!=null) { if (traces != null) {
filter.setTraces(traces); filter.setTraces(traces);
} }
return filter; return filter;
......
...@@ -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