Is there a way to set custom parameters and attributes to request in a pre-filter before it reaches the service endpoint?
I tried the below, but doesn't seem to work.
ctx.setRequestQueryParams(map);
Comment From: ryanjbaxter
Could you provide more details on your Zuul configuration?
Comment From: spencergibb
What is the order of your filter?
Comment From: lowzj
@prantan7 Class HTTPRequestUtils has some handy methods for working with HTTP requests. I'm using the following code in a pre-filter(after PreDecorationFilter) to add request parameters, may be helpful for you.
Map<String, List<String>> map = HTTPRequestUtils.getInstance().getQueryParams();
if (map == null) {
RequestContext.getCurrentContext().setRequestQueryParams(new HashMap<String, List<String>>());
map = HTTPRequestUtils.getInstance().getQueryParams();
}
// add custom parameters
map.put("test", Arrays.asList("a", "b"));
Comment From: prantan7
@spencergibb - It is the last pre-filter among the filters we have configured. I see the order for the PreDecorationFilter is 5.
Comment From: spencergibb
What is the actual number you are using?
Comment From: spencergibb
Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.
Comment From: d3minem
@spencergibb - I'm having the same issue, the query params are not passing to the route URI by setting ctx.setRequestQueryParams(params).
Can you please provide some feedback? and let me know how should I set the request params through ctx.setRequestQueryParams(params). I don't want to hard code in the route mapping with urls.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
<relativePath></relativePath>
</parent>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
<version>2.20.RELEASE</version>
</dependency>
Zuul Pre Filter oder set by us: 6
@Override
public int filterOrder() {
return 6;
}
You can see the requested information for debugging:
logger.info("Zuul Pre Filter [Host Route]: " + ctx.getRouteHost());
logger.info("Zuul Pre Filter [Host Route] Query: " + ctx.getRouteHost().getQuery());
logger.info("Zuul Pre Filter [Actual Request]: " + ctx.getRequest().getRequestURL().toString());
logger.info("Zuul Pre Filter [Actual Request] Query: " + ctx.getRequest().getQueryString());
logger.info("Zuul Pre Filter Request Query Params through ctx.getRequestQueryParams(): " + ctx.getRequestQueryParams());
Logging Output:
2020-04-18 10:51:40.198 INFO 60931 --- [nio-8080-exec-2] com.baeldung.config.CustomPreZuulFilter : Zuul Pre Filter [Host Route]: http://localhost:8083/auth/realms/baeldung/protocol/openid-connect/auth
2020-04-18 10:51:40.198 INFO 60931 --- [nio-8080-exec-2] com.baeldung.config.CustomPreZuulFilter : Zuul Pre Filter [Host Route] Query: null
2020-04-18 10:51:40.218 INFO 60931 --- [nio-8080-exec-2] com.baeldung.config.CustomPreZuulFilter : Zuul Pre Filter [Actual Request]: http://localhost:8080/auth/code
2020-04-18 10:51:40.219 INFO 60931 --- [nio-8080-exec-2] com.baeldung.config.CustomPreZuulFilter : Zuul Pre Filter [Actual Request] Query: null
2020-04-18 10:51:40.219 INFO 60931 --- [nio-8080-exec-2] com.baeldung.config.CustomPreZuulFilter : Zuul Pre Filter Request Query Params through ctx.getRequestQueryParams(): {scope=[read], response_type=[code], redirect_uri=[http://localhost:8080/auth/redirect/], client_id=[newClient]}
When I changed the route mapping with hardcoded request query params defined as below.
auth-service/code:
path: /auth/code/**
sensitiveHeaders:
url: http://localhost:8083/auth/realms/baeldung/protocol/openid-connect/auth?response_type=code&scope=read&client_id=newClient&redirect_uri=http://localhost:8080/auth/redirect/
The host route mapping show the parsed query params in the pre filter.
2020-04-18 13:55:44.822 INFO 62190 --- [nio-8080-exec-4] com.baeldung.config.CustomPreZuulFilter : in zuul filter URI:/auth/code
2020-04-18 13:55:45.528 INFO 62190 --- [nio-8080-exec-4] com.baeldung.config.CustomPreZuulFilter : Zuul Pre Filter [Host Route]: http://localhost:8083/auth/realms/baeldung/protocol/openid-connect/auth?response_type=code&scope=read&client_id=newClient&redirect_uri=http://localhost:8080/auth/redirect/
2020-04-18 13:55:45.528 INFO 62190 --- [nio-8080-exec-4] com.baeldung.config.CustomPreZuulFilter : Zuul Pre Filter [Host Route] Query: response_type=code&scope=read&client_id=newClient&redirect_uri=http://localhost:8080/auth/redirect/
2020-04-18 13:55:45.528 INFO 62190 --- [nio-8080-exec-4] com.baeldung.config.CustomPreZuulFilter : Zuul Pre Filter [Actual Request]: http://localhost:8080/auth/code
2020-04-18 13:55:45.528 INFO 62190 --- [nio-8080-exec-4] com.baeldung.config.CustomPreZuulFilter : Zuul Pre Filter [Actual Request] Query: null
2020-04-18 13:55:45.528 INFO 62190 --- [nio-8080-exec-4] com.baeldung.config.CustomPreZuulFilter : Zuul Pre Filter Request Query Params through ctx.getRequestQueryParams(): null
Comment From: d3minem
@spencergibb - I solved the above issue with custom ExtendedProxyRequestHelper for SimpleHostRoutingFilter. For other's reference: Zuul Request QueryParams.