6

springcloud gateway 的自定义过滤器中家请求参数 requestParams 没生效,有遇到过的老...

 2 years ago
source link: https://www.v2ex.com/t/818342
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  Java

springcloud gateway 的自定义过滤器中家请求参数 requestParams 没生效,有遇到过的老哥吗

  Renco · 5 小时 48 分钟前 · 251 次点击

                URI uri = exchange.getRequest().getURI();
                StringBuilder query = new StringBuilder();
                String originalQuery = uri.getRawQuery();
                if (StringUtils.hasText(originalQuery)) {
                    query.append(originalQuery);
                    if (originalQuery.charAt(originalQuery.length() - 1) != '&') {
                        query.append('&');
                    }
                }
                params.forEach((key,value) ->{
                    query.append(key);
                    query.append('=');
                    query.append(value);
                    query.append('&');
                });

                try {
                    URI newUri = UriComponentsBuilder.fromUri(uri).replaceQuery(query.toString()).build(true).toUri();
                    ServerHttpRequest request = exchange.getRequest().mutate().uri(newUri).build();
                    return chain.filter(exchange.mutate().request(request).build());
                } catch (RuntimeException var8) {
                    throw new IllegalStateException("Invalid URI query: \"" + query.toString() + "\"");
                }

以上这种写法,参考了自带的 AddRequestParameter 过滤器的写法


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK