{"id":1944,"date":"2023-04-01T10:08:08","date_gmt":"2023-04-01T02:08:08","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1944"},"modified":"2023-04-22T08:43:43","modified_gmt":"2023-04-22T00:43:43","slug":"zuul-uses-filter-to-modify-request-parameters-request-headers-and-response-headers","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/zuul-uses-filter-to-modify-request-parameters-request-headers-and-response-headers\/","title":{"rendered":"Zuul\u4f7f\u7528Filter\u4fee\u6539\u8bf7\u6c42\u53c2\u6570\u3001\u8bf7\u6c42\u5934\u548c\u54cd\u5e94\u5934"},"content":{"rendered":"<h2>\u4fee\u6539\u8bf7\u6c42\u53c2\u6570<\/h2>\n<pre><code class=\"language-java\">RequestContext ctx = RequestContext.getCurrentContext();\nHttpServletRequest request = ctx.getRequest();\nMap&lt;String, List&lt;String&gt;&gt; requestQueryParams = ctx.getRequestQueryParams();\n\nif (requestQueryParams == null) {\n    requestQueryParams = new HashMap&lt;&gt;();\n}\n\n\/\/\u5c06\u8981\u65b0\u589e\u7684\u53c2\u6570\u6dfb\u52a0\u8fdb\u53bb,\u88ab\u8c03\u7528\u7684\u5fae\u670d\u52a1\u53ef\u4ee5\u76f4\u63a5 \u53bb\u53d6,\u5c31\u60f3\u666e\u901a\u7684\u4e00\u6837,\u6846\u67b6\u4f1a\u76f4\u63a5\u6ce8\u5165\u8fdb\u53bb\nArrayList&lt;String&gt; arrayList = new ArrayList&lt;&gt;();\narrayList.add(&quot;1&quot;);\nrequestQueryParams.put(&quot;test&quot;, arrayList);\n\nctx.setRequestQueryParams(requestQueryParams);<\/code><\/pre>\n<p><!-- more --><\/p>\n<h2>\u4fee\u6539\u8bf7\u6c42\u5934<\/h2>\n<pre><code class=\"language-java\">RequestContext ctx = RequestContext.getCurrentContext();  \nctx.addZuulRequestHeader(&quot;original_requestURL&quot;,request.getRequestURL().toString());<\/code><\/pre>\n<h2>\u4fee\u6539\u54cd\u5e94\u5934<\/h2>\n<pre><code class=\"language-java\">RequestContext ctx = RequestContext.getCurrentContext();\nHttpServletResponse response = ctx.getResponse();\nString info = response.getHeader(&quot;info&quot;);\nString infoSize = response.getHeader(&quot;info_size&quot;);\n\/**\n * \u8bbe\u7f6e\u54cd\u5e94\u5934\uff0c\u4f7f\u8bf7\u6c42\u53d8\u4e3a\u6587\u4ef6\u4e0b\u8f7d\n *\/\nctx.addZuulResponseHeader(&quot;Content-Type&quot;, &quot;application\/octet-stream&quot;);\nctx.addZuulResponseHeader(&quot;Content-Disposition&quot;, &quot;attachment;fileName=&quot; + info);\nctx.addZuulResponseHeader(&quot;Content-Length&quot;, &quot;&quot; + infoSize);<\/code><\/pre>\n<h2>\u91cd\u5199\u8bf7\u6c42<\/h2>\n<pre><code class=\"language-java\">private void rewriteRequestParams(RoutConfig.Service service) throws Exception {\n    RequestContext ctx = RequestContext.getCurrentContext();\n    HttpServletRequest request = ctx.getRequest();\n    Map&lt;String, Object&gt; requestMap = getParams();\n\n    \/\/ \u8bf7\u6c42\u4e3aGet\n    if (HttpMethod.GET.name().equalsIgnoreCase(request.getMethod())) {\n        ctx.setRequest(new HttpServletRequestWrapper(request) {\n            @Override\n            public String getMethod() {\n                return HttpMethod.GET.name();\n            }\n\n        });\n        Map&lt;String, List&lt;String&gt;&gt; r = wrapGetRequestParams();\n        ctx.setRequestQueryParams(r);\n    }\n\n    \/\/ \u8bf7\u6c42\u4e3aPost\n    if (HttpMethod.POST.name().equalsIgnoreCase(request.getMethod())){\n        \/\/byte[] body = JSON.toJSONBytes(requestMap);\n        byte[] body = wrapPostRequestBody();\n        log.info(&quot;Post body: {}&quot;, new String(body));\n        ctx.setRequest(new HttpServletRequestWrapper(request) {\n            @Override\n            public ServletInputStream getInputStream() throws IOException {\n                return new ServletInputStreamWrapper(body);\n            }\n            @Override\n            public int getContentLength() {\n                return body.length;\n            }\n\n            @Override\n            public long getContentLengthLong() {\n                return body.length;\n            }\n\n            @Override\n            public String getMethod() {\n                return HttpMethod.POST.name();\n            }\n\n        });\n    }\n}\n\nprivate Map&lt;String, List&lt;String&gt;&gt; wrapGetRequestParams() {\n    Map&lt;String, List&lt;String&gt;&gt; r = new LinkedHashMap&lt;&gt;();\n    r.put(&quot;name&quot;, Collections.singletonList(&quot;Joe.Ye&quot;));\n    return r;\n}\n\nprivate byte[] wrapPostRequestBody() {\n    Map&lt;String, Object&gt; requestBody = new LinkedHashMap();\n    requestBody.put(&quot;name&quot;, &quot;Joe.Ye&quot;);\n    return JSON.toJSONBytes(requestBody);\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4fee\u6539\u8bf7\u6c42\u53c2\u6570 RequestContext ctx = RequestContext.getCurrentCo [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[250],"class_list":["post-1944","post","type-post","status-publish","format-standard","hentry","category-spring-cloud","tag-zuul"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1944","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1944"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1944\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1944"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1944"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1944"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}