从Postman或浏览器发起请求时Zuul pre-filter被触发两次问题咨询
Hey there! I’ve dealt with this exact problem before—Zuul pre-filters firing twice for every request from Postman or browsers can be super frustrating, especially when it makes all your operations run twice. Let’s walk through the most common causes and how to fix them.
Common Causes & Solutions
1. Unintended Favicon.ico Request (Most Likely Culprit)
Browsers (and sometimes Postman) automatically send a request for /favicon.ico to load the site’s icon. This extra request triggers your pre-filter a second time without you noticing.
Fix 1: Ignore Favicon Requests in Zuul Config
Add this to your application.yml or application.properties to tell Zuul to skip favicon requests entirely:
zuul: ignored-patterns: /**/favicon.ico
Fix 2: Filter Out Favicon in Your Pre-Filter
Modify your shouldFilter() method to skip execution when the request is for favicon. Here’s how to update your example code:
@Override public boolean shouldFilter() { RequestContext ctx = RequestContext.getCurrentContext(); HttpServletRequest request = ctx.getRequest(); String requestUri = request.getRequestURI(); // Skip favicon requests to avoid double-trigger return !requestUri.equals("/favicon.ico") && !requestUri.contains("/favicon.ico"); }
2. Duplicate Filter Registration
If your ApplicationGateway filter is registered more than once (e.g., using both @Component on the class and a @Bean definition in a config class), Zuul will create two instances of the filter—each firing on every request.
Fix:
Check your code to ensure you only register the filter once:
- Either add
@Componentdirectly to yourApplicationGatewayclass, OR - Define it as a single
@Beanin your configuration class.
Don’t do both!
3. HTTP Redirects Causing Double Requests
Sometimes, if your request URL doesn’t end with a slash (e.g., http://gateway/api instead of http://gateway/api/), Zuul might automatically redirect to the correct URL. This redirect counts as a second request, triggering your filter twice.
Fix:
- Ensure your client sends requests with the correct URL format (including trailing slashes if needed).
- Or disable automatic redirects in Zuul with these configs:
spring: mvc: throw-exception-if-no-handler-found: true resources: add-mappings: false
Updated Example Filter Code
Here’s your ApplicationGateway filter with the favicon check included to prevent double triggers:
public class ApplicationGateway extends ZuulFilter { @Override public String filterType() { return "pre"; } @Override public int filterOrder() { return 1; } @Override public boolean shouldFilter() { RequestContext ctx = RequestContext.getCurrentContext(); HttpServletRequest request = ctx.getRequest(); String requestUri = request.getRequestURI(); // Skip favicon requests to avoid double execution return !requestUri.equals("/favicon.ico") && !requestUri.contains("/favicon.ico"); } @Override public Object run() throws ZuulException { RequestContext ctx = RequestContext.getCurrentContext(); HttpServletRequest request = ctx.getRequest(); // Your pre-filter logic here System.out.println("Pre-filter executed for URI: " + request.getRequestURI()); return null; } }
Start with checking the favicon issue first—it’s the most common fix for this problem. If that doesn’t work, move on to verifying your filter registration and checking for redirects.
内容的提问来源于stack exchange,提问作者rohit swami

