|
26 | 26 | import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
27 | 27 | import org.springframework.http.server.reactive.ServerHttpRequest;
|
28 | 28 | import org.springframework.util.Assert;
|
| 29 | +import org.springframework.util.LinkedMultiValueMap; |
| 30 | +import org.springframework.util.MultiValueMap; |
29 | 31 | import org.springframework.web.server.ServerWebExchange;
|
30 | 32 | import org.springframework.web.util.UriComponentsBuilder;
|
| 33 | +import org.springframework.web.util.UriUtils; |
31 | 34 |
|
32 | 35 | import static org.springframework.cloud.gateway.support.GatewayToStringStyler.filterToStringCreator;
|
| 36 | +import static org.springframework.util.CollectionUtils.unmodifiableMultiValueMap; |
33 | 37 |
|
34 | 38 | /**
|
35 | 39 | * @author Fredrich Ombico
|
@@ -59,14 +63,25 @@ public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
59 | 63 | ServerHttpRequest req = exchange.getRequest();
|
60 | 64 |
|
61 | 65 | UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUri(req.getURI());
|
62 |
| - if (req.getQueryParams().containsKey(config.getName())) { |
63 |
| - uriComponentsBuilder.replaceQueryParam(config.getName(), config.getReplacement()); |
| 66 | + |
| 67 | + MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>(req.getQueryParams()); |
| 68 | + if (queryParams.containsKey(config.getName())) { |
| 69 | + queryParams.remove(config.getName()); |
| 70 | + queryParams.add(config.getName(), config.getReplacement()); |
64 | 71 | }
|
65 | 72 |
|
66 |
| - URI uri = uriComponentsBuilder.build().toUri(); |
67 |
| - ServerHttpRequest request = req.mutate().uri(uri).build(); |
| 73 | + try { |
| 74 | + MultiValueMap<String, String> encodedQueryParams = UriUtils.encodeQueryParams(queryParams); |
| 75 | + URI uri = uriComponentsBuilder.replaceQueryParams(unmodifiableMultiValueMap(encodedQueryParams)) |
| 76 | + .build(true) |
| 77 | + .toUri(); |
68 | 78 |
|
69 |
| - return chain.filter(exchange.mutate().request(request).build()); |
| 79 | + ServerHttpRequest request = req.mutate().uri(uri).build(); |
| 80 | + return chain.filter(exchange.mutate().request(request).build()); |
| 81 | + } |
| 82 | + catch (IllegalArgumentException ex) { |
| 83 | + throw new IllegalStateException("Invalid URI query: \"" + queryParams + "\""); |
| 84 | + } |
70 | 85 | }
|
71 | 86 |
|
72 | 87 | @Override
|
|
0 commit comments