{"id":1943,"date":"2023-04-01T10:07:24","date_gmt":"2023-04-01T02:07:24","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1943"},"modified":"2023-04-22T08:44:02","modified_gmt":"2023-04-22T00:44:02","slug":"spring-cloud-zuul-session-retention-issues","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/spring-cloud-zuul-session-retention-issues\/","title":{"rendered":"Spring Cloud Zuul \u4f1a\u8bdd\u4fdd\u6301\u95ee\u9898"},"content":{"rendered":"<h2>\u95ee\u9898\u63cf\u8ff0\uff1a<\/h2>\n<p>\u901a\u8fc7zuul\u667a\u80fd\u8def\u7531\u5230\u540e\u7aef\u670d\u52a1\uff0c\u5176\u4e2d\u6709\u4e2a\u670d\u52a1\u662f\u9700\u8981\u901a\u8fc7<code>Basic Auth<\/code>\u8ba4\u8bc1<\/p>\n<p>\u914d\u7f6e\u8def\u7531\u6ca1\u95ee\u9898\uff0c\u4f46\u8bf7\u6c42\u8fd4\u56de\u59cb\u7ec8\u662f401\uff0c\u8ba4\u8bc1\u6ca1\u901a\u8fc7<\/p>\n<p><!-- more --><\/p>\n<h2>\u539f\u56e0\u67e5\u627e<\/h2>\n<p>\u67e5\u770b\u8bf7\u6c42\u53c2\u6570\uff0c\u53d1\u73b0<code>Header<\/code>\u4fe1\u606f\u901a\u8fc7<code>Zuul<\/code>\u540e\u4e22\u5931<\/p>\n<h2>\u95ee\u9898\u5206\u6790<\/h2>\n<p><code>Zuul<\/code>\u9ed8\u8ba4\u8fc7\u6ee4\u6389\u8bf7\u6c42<code>Header<\/code>\uff0c\u4e00\u5b9a\u6709\u5bf9<code>Header<\/code>\u8fc7\u6ee4\u7684\u914d\u7f6e<\/p>\n<h2>\u89e3\u51b3\u95ee\u9898<\/h2>\n<pre><code>#\u4fdd\u7559\u8bf7\u6c42header\u4fe1\u606f\nzuul.routes.xxx.sensitive-headers=Authorization,sign,sign-type\nzuul.routes.xxx.custom-sensitive-headers=true<\/code><\/pre>\n<h2>\u4f1a\u8bdd\u4fdd\u6301\u95ee\u9898<\/h2>\n<p>\u901a\u8fc7\u8ddf\u8e2a\u4e00\u4e2aHTTP\u8bf7\u6c42\u7ecf\u8fc7<code>Zuul<\/code>\u5230\u5177\u4f53\u670d\u52a1\uff0c\u518d\u5230\u8fd4\u56de\u7ed3\u679c\u7684\u5168\u8fc7\u7a0b\u3002\u6211\u4eec\u5f88\u5bb9\u6613\u5c31\u80fd\u53d1\u73b0\uff0c\u5728\u4f20\u9012\u7684\u8fc7\u7a0b\u4e2d\uff0cHTTP\u8bf7\u6c42\u5934\u4fe1\u606f\u4e2d\u7684<code>Cookie<\/code>\u548c<code>Authorization<\/code>\u90fd\u6ca1\u6709\u88ab\u6b63\u786e\u5730\u4f20\u9012\u7ed9\u5177\u4f53\u670d\u52a1\uff0c\u6240\u4ee5\u6700\u7ec8\u5bfc\u81f4\u4f1a\u8bdd\u72b6\u6001\u6ca1\u6709\u5f97\u5230\u4fdd\u6301\u7684\u73b0\u8c61\u3002<\/p>\n<p>\u90a3\u4e48\u8fd9\u4e9b\u4fe1\u606f\u662f\u5728\u54ea\u91cc\u4e22\u5931\u7684\u5462\uff1f\u6211\u4eec\u4ece<code>Zuul<\/code>\u8fdb\u884c\u8def\u7531\u8f6c\u53d1\u7684\u8fc7\u6ee4\u5668\u4f5c\u4e3a\u8d77\u70b9\uff0c\u6765\u4e00\u63a2\u7a76\u7adf\u3002\u4e0b\u9762\u662f<code>RibbonRoutingFilter<\/code>\u8fc7\u6ee4\u5668\u7684\u5b9e\u73b0\u7247\u6bb5\uff1a<\/p>\n<pre><code class=\"language-java\">public class RibbonRoutingFilter extends ZuulFilter {\n    ...\n    protected ProxyRequestHelper helper;\n\n    @Override\n    public Object run() {\n        RequestContext context = RequestContext.getCurrentContext();\n        this.helper.addIgnoredHeaders();\n        try {\n            RibbonCommandContext commandContext = buildCommandContext(context);\n            ClientHttpResponse response = forward(commandContext);\n            setResponse(response);\n            return response;\n        }\n        ...\n        return null;\n    }\n\n        protected RibbonCommandContext buildCommandContext(RequestContext context) {\n        HttpServletRequest request = context.getRequest();\n\n        MultiValueMap&lt;String, String&gt; headers = this.helper\n                .buildZuulRequestHeaders(request);\n        MultiValueMap&lt;String, String&gt; params = this.helper\n                .buildZuulRequestQueryParams(request);\n        ...\n    }\n}<\/code><\/pre>\n<p>\u8fd9\u91cc\u6709\u4e09\u4e2a\u91cd\u8981\u5143\u7d20\uff1a<\/p>\n<ul>\n<li>\u8fc7\u6ee4\u5668\u7684\u6838\u5fc3\u903b\u8f91run\u51fd\u6570\u5b9e\u73b0\uff0c\u5176\u4e2d\u8c03\u7528\u4e86\u5185\u90e8\u51fd\u6570<code>buildCommandContext<\/code>\u6765\u6784\u5efa\u4e0a\u4e0b\u6587\u5185\u5bb9<\/li>\n<li>\u800c<code>buildCommandContext<\/code>\u4e2d\u8c03\u7528\u4e86<code>helper<\/code>\u5bf9\u8c61\u7684<code>buildZuulRequestHeaders<\/code>\u65b9\u6cd5\u6765\u5904\u7406\u8bf7\u6c42\u5934\u4fe1\u606f<\/li>\n<li><code>helper<\/code>\u5bf9\u8c61\u662f<code>ProxyRequestHelper<\/code>\u7c7b\u7684\u5b9e\u4f8b<\/li>\n<\/ul>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u518d\u770b\u770b<code>ProxyRequestHelper<\/code>\u7684\u5b9e\u73b0\uff1a<\/p>\n<pre><code class=\"language-java\">public class ProxyRequestHelper {\n\n    public MultiValueMap&lt;String, String&gt; buildZuulRequestHeaders(\n            HttpServletRequest request) {\n        RequestContext context = RequestContext.getCurrentContext();\n        MultiValueMap&lt;String, String&gt; headers = new HttpHeaders();\n        Enumeration&lt;String&gt; headerNames = request.getHeaderNames();\n        if (headerNames != null) {\n            while (headerNames.hasMoreElements()) {\n                String name = headerNames.nextElement();\n                if (isIncludedHeader(name)) {\n                    Enumeration&lt;String&gt; values = request.getHeaders(name);\n                    while (values.hasMoreElements()) {\n                        String value = values.nextElement();\n                        headers.add(name, value);\n                    }\n                }\n            }\n        }\n        Map&lt;String, String&gt; zuulRequestHeaders = context.getZuulRequestHeaders();\n        for (String header : zuulRequestHeaders.keySet()) {\n            headers.set(header, zuulRequestHeaders.get(header));\n        }\n        headers.set(HttpHeaders.ACCEPT_ENCODING, &quot;gzip&quot;);\n        return headers;\n    }\n\n    public boolean isIncludedHeader(String headerName) {\n        String name = headerName.toLowerCase();\n        RequestContext ctx = RequestContext.getCurrentContext();\n        if (ctx.containsKey(IGNORED_HEADERS)) {\n            Object object = ctx.get(IGNORED_HEADERS);\n            if (object instanceof Collection &amp;&amp; ((Collection&lt;?&gt;) object).contains(name)) {\n                return false;\n            }\n        }\n        ...\n    }\n}<\/code><\/pre>\n<p>\u4ece\u4e0a\u8ff0\u6e90\u7801\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u6784\u5efa\u5934\u4fe1\u606f\u7684\u65b9\u6cd5<code>buildZuulRequestHeaders<\/code>\u901a\u8fc7<code>isIncludedHeader<\/code>\u51fd\u6570\u6765\u5224\u65ad\u5f53\u524d\u8bf7\u6c42\u7684\u5404\u4e2a\u5934\u4fe1\u606f\u662f\u5426\u5728\u5ffd\u7565\u7684\u5934\u4fe1\u606f\u6e05\u5355\u4e2d\uff0c\u5982\u679c\u662f\u7684\u8bdd\u5c31\u4e0d\u7ec4\u7ec7\u5230\u6b64\u6b21\u8f6c\u53d1\u7684\u8bf7\u6c42\u4e2d\u53bb\u3002\u90a3\u4e48\u8fd9\u4e9b\u9700\u8981\u5ffd\u7565\u7684\u5934\u4fe1\u606f\u662f\u5728\u54ea\u91cc\u521d\u59cb\u5316\u7684\u5462\uff1f\u5728PRE\u9636\u6bb5\u7684<code>PreDecorationFilter<\/code>\u8fc7\u6ee4\u5668\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u627e\u5230\u7b54\u6848\uff1a<\/p>\n<pre><code class=\"language-java\">public class PreDecorationFilter extends ZuulFilter {\n    ...\n    public Object run() {\n        RequestContext ctx = RequestContext.getCurrentContext();\n        final String requestURI = this.urlPathHelper.getPathWithinApplication(ctx.getRequest());\n        Route route = this.routeLocator.getMatchingRoute(requestURI);\n        if (route != null) {\n            String location = route.getLocation();\n            if (location != null) {\n                ctx.put(&quot;requestURI&quot;, route.getPath());\n                ctx.put(&quot;proxy&quot;, route.getId());\n                   \/\/ \u5904\u7406\u5ffd\u7565\u5934\u4fe1\u606f\u7684\u90e8\u5206\n                if (!route.isCustomSensitiveHeaders()) {\n                    this.proxyRequestHelper.addIgnoredHeaders(\n                        this.properties.getSensitiveHeaders()\n                        .toArray(new String[0]));\n                } else {\n                    this.proxyRequestHelper.addIgnoredHeaders(\n                        route.getSensitiveHeaders()\n                        .toArray(new String[0]));\n                }\n        ...\n}<\/code><\/pre>\n<p>\u4ece\u4e0a\u8ff0\u6e90\u7801\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u6709\u4e00\u6bb5<code>if\/else<\/code>\u5757\uff0c\u901a\u8fc7\u8c03\u7528<code>ProxyRequestHelper<\/code>\u7684<code>addIgnoredHeaders<\/code>\u65b9\u6cd5\u6765\u6dfb\u52a0\u9700\u8981\u5ffd\u7565\u7684\u4fe1\u606f\u5230\u8bf7\u6c42\u4e0a\u4e0b\u6587\u4e2d\uff0c\u4f9b\u540e\u7eed<code>ROUTE<\/code>\u9636\u6bb5\u7684\u8fc7\u6ee4\u5668\u4f7f\u7528\u3002\u8fd9\u91cc\u7684<code>if\/else<\/code>\u5757\u5206\u522b\u7528\u6765\u5904\u7406\u5168\u5c40\u8bbe\u7f6e\u7684\u654f\u611f\u5934\u4fe1\u606f\u548c\u6307\u5b9a\u8def\u7531\u8bbe\u7f6e\u7684\u654f\u611f\u5934\u4fe1\u606f\u3002\u800c\u5168\u5c40\u7684\u654f\u611f\u5934\u4fe1\u606f\u5b9a\u4e49\u4e8e<code>ZuulProperties<\/code>\u4e2d\uff1a<\/p>\n<pre><code class=\"language-java\">@Data\n@ConfigurationProperties(&quot;zuul&quot;)\npublic class ZuulProperties {\n    private Set&lt;String&gt; sensitiveHeaders = new LinkedHashSet&lt;&gt;(\n            Arrays.asList(&quot;Cookie&quot;, &quot;Set-Cookie&quot;, &quot;Authorization&quot;));\n    ...\n}<\/code><\/pre>\n<p>\u6240\u4ee5\u89e3\u51b3\u8be5\u95ee\u9898\u7684\u601d\u8def\u4e5f\u5f88\u7b80\u5355\uff0c\u6211\u4eec\u53ea\u9700\u8981\u901a\u8fc7\u8bbe\u7f6e<code>sensitiveHeaders<\/code>\u5373\u53ef\uff0c\u8bbe\u7f6e\u65b9\u6cd5\u5206\u4e3a\u4e24\u79cd\uff1a<\/p>\n<p>\u5168\u5c40\u8bbe\u7f6e\uff1a<\/p>\n<pre><code>zuul.sensitive-headers=<\/code><\/pre>\n<p>\u6307\u5b9a\u8def\u7531\u8bbe\u7f6e\uff1a<\/p>\n<pre><code>zuul.routes.&lt;routeName&gt;.sensitive-headers=\nzuul.routes.&lt;routeName&gt;.custom-sensitive-headers=true<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u95ee\u9898\u63cf\u8ff0\uff1a \u901a\u8fc7zuul\u667a\u80fd\u8def\u7531\u5230\u540e\u7aef\u670d\u52a1\uff0c\u5176\u4e2d\u6709\u4e2a\u670d\u52a1\u662f\u9700\u8981\u901a\u8fc7Basic Auth\u8ba4\u8bc1 \u914d\u7f6e\u8def\u7531\u6ca1\u95ee\u9898\uff0c\u4f46 [&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-1943","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\/1943","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=1943"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1943\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1943"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1943"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1943"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}