{"id":1747,"date":"2023-03-26T21:22:54","date_gmt":"2023-03-26T13:22:54","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1747"},"modified":"2023-04-23T21:28:27","modified_gmt":"2023-04-23T13:28:27","slug":"spring-cloud-gateway-custom-gatewayfilterfactory","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/26\/spring-cloud-gateway-custom-gatewayfilterfactory\/","title":{"rendered":"Spring Cloud Gateway\u81ea\u5b9a\u4e49GatewayFilterFactory"},"content":{"rendered":"<h2>GatewayFilterFactory<\/h2>\n<p><code>Spring Cloud Gateway<\/code>\u63d0\u4f9b\u4e86\u5f88\u591a\u5185\u7f6e\u7684\u8fc7\u6ee4\u5668\uff0c\u90a3\u4e48\u56e0\u4e3a\u9700\u6c42\u7684\u5173\u7cfb\uff0c\u9700\u8981\u81ea\u5b9a\u4e49\u5b9e\u73b0\uff0c\u5e76\u4e14\u8981\u53ef\u914d\u7f6e<\/p>\n<p>\u5bf9\u4e8e\u81ea\u5b9a\u4e49\u7684factory\uff0c\u6211\u4eec\u53ef\u4ee5\u9009\u62e9\u53bb\u5b9e\u73b0\u63a5\u53e3\u6216\u7ee7\u627f\u5df2\u6709\u7684\u62bd\u8c61\u7c7b\uff0c\u76f8\u5173\u7684\u63a5\u53e3\u662f<code>GatewayFilterFactory<\/code>\uff0c\u800cSpring Boot\u9ed8\u8ba4\u5e2e\u6211\u4eec\u5b9e\u73b0\u7684\u62bd\u8c61\u7c7b\u662f<code>AbstractGatewayFilterFactory<\/code><\/p>\n<p><!-- more --><\/p>\n<p>\u4f7f\u7528\u53c2\u8003\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/cloud.spring.io\/spring-cloud-static\/spring-cloud-gateway\/2.0.0.RELEASE\/multi\/multi__developer_guide.html#_writing_custom_gatewayfilter_factories\">http:\/\/cloud.spring.io\/spring-cloud-static\/spring-cloud-gateway\/2.0.0.RELEASE\/multi\/multi__developer_guide.html#_writing_custom_gatewayfilter_factories<\/a><\/p>\n<blockquote>\n<p>In order to write a GatewayFilter you will need to implement GatewayFilterFactory. There is an abstract class called AbstractGatewayFilterFactory which you can extend.<\/p>\n<\/blockquote>\n<ul>\n<li><code>PreGatewayFilterFactory.java<\/code><\/li>\n<\/ul>\n<pre><code class=\"language-java\">public class PreGatewayFilterFactory extends AbstractGatewayFilterFactory&lt;PreGatewayFilterFactory.Config&gt; {\n    public PreGatewayFilterFactory() {\n        super(Config.class);\n    }\n    @Override\n    public GatewayFilter apply(Config config) {\n        \/\/ grab configuration from Config object\n        return (exchange, chain) -&gt; {\n            \/\/If you want to build a &quot;pre&quot; filter you need to manipulate the\n            \/\/request before calling change.filter\n            ServerHttpRequest.Builder builder = exchange.getRequest().mutate();\n            \/\/use builder to manipulate the request\n            return chain.filter(exchange.mutate().request(request).build());\n        };\n    }\n    public static class Config {\n        \/\/Put the configuration properties for your filter here\n    }\n}<\/code><\/pre>\n<ul>\n<li><code>PostGatewayFilterFactory.java<\/code><\/li>\n<\/ul>\n<pre><code class=\"language-java\">public class PostGatewayFilterFactory extends AbstractGatewayFilterFactory&lt;PostGatewayFilterFactory.Config&gt; {\n    public PostGatewayFilterFactory() {\n        super(Config.class);\n    }\n    @Override\n    public GatewayFilter apply(Config config) {\n        \/\/ grab configuration from Config object\n        return (exchange, chain) -&gt; {\n            return chain.filter(exchange).then(Mono.fromRunnable(() -&gt; {\n                ServerHttpReponse response = exchange.getResponse();\n                \/\/Manipulate the response in some way\n            }));\n        };\n    }\n    public static class Config {\n        \/\/Put the configuration properties for your filter here\n    }\n}<\/code><\/pre>\n<h2>\u4f7f\u7528<\/h2>\n<p>\u9996\u5148\u81ea\u5b9a\u4e49\u7684\u8fc7\u6ee4\u5668\u5de5\u5382\u7c7b\u4ee3\u7801<\/p>\n<pre><code class=\"language-java\">public class ExampleGatewayFilterFactory extends AbstractGatewayFilterFactory&lt;ExampleGatewayFilterFactory.Config&gt; {\n\n    \/**\n     * \u5b9a\u4e49\u53ef\u4ee5\u518dyaml\u4e2d\u58f0\u660e\u7684\u5c5e\u6027\u53d8\u91cf\n     *\/\n    private static final String TYPE = &quot;type&quot;;\n    private static final String OP = &quot;op&quot;;\n\n    \/**\n     * constructor\n     *\/\n    public ExampleGatewayFilterFactory(){\n        \/\/ \u8fd9\u91cc\u9700\u8981\u5c06\u81ea\u5b9a\u4e49\u7684config\u4f20\u8fc7\u53bb\uff0c\u5426\u5219\u4f1a\u62a5\u544aClassCastException\n        super(Config.class);\n    }\n\n    @Override\n    public List&lt;String&gt; shortcutFieldOrder() {\n        return Arrays.asList(TYPE, OP);\n    }\n\n    @Override\n    public GatewayFilter apply(Config config) {\n        return ((exchange, chain) -&gt; {\n            boolean root = &quot;root&quot;.equals(config.getOp());\n            if (root){\n                LogUtil.info(&quot;GatewayFilter root&quot;);\n            }\n            else {\n                LogUtil.info(&quot;GatewayFilter customer&quot;);\n            }\n            \/\/ \u5728then\u65b9\u6cd5\u91cc\u7684\uff0c\u76f8\u5f53\u4e8eaop\u4e2d\u7684\u540e\u7f6e\u901a\u77e5\n            return chain.filter(exchange).then(Mono.fromRunnable(()-&gt;{\n                \/\/ do something\n            }));\n        });\n    }\n\n    \/**\n     * \u81ea\u5b9a\u4e49\u7684config\u7c7b\uff0c\u7528\u6765\u8bbe\u7f6e\u4f20\u5165\u7684\u53c2\u6570\n     *\/\n    public static class Config {\n\n        \/**\n         * \u8fc7\u6ee4\u7c7b\u578b\n         *\/\n        private String type;\n\n        \/**\n         * \u64cd\u4f5c\n         *\/\n        private String op;\n\n        public String getType() {\n            return type;\n        }\n\n        public void setType(String type) {\n            this.type = type;\n        }\n\n        public String getOp() {\n            return op;\n        }\n\n        public void setOp(String op) {\n            this.op = op;\n        }\n    }\n}<\/code><\/pre>\n<p>\u56e0\u4e3a\u4f7f\u7528\u7684\u662fSpring Boot\uff0c\u90a3\u4e48\u9700\u8981\u5728\u542f\u52a8\u7c7b\u91cc\uff0c\u5c06\u8fd9\u4e2a\u5de5\u5382\u6ce8\u5165\u5230Spring\u5bb9\u5668\u5f53\u4e2d<\/p>\n<pre><code class=\"language-java\">@Bean\npublic ExampleGatewayFilterFactory exampleGatewayFilterFactory(){\n    return new ExampleGatewayFilterFactory();\n}<\/code><\/pre>\n<p>\u7136\u540e\u662fyaml\u7684\u914d\u7f6e\uff0c\u8fd9\u91cc\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4e4b\u524d\u6211\u4e00\u76f4\u5931\u8d25\uff0c\u5728\u8c03\u7528gateway\u4f1a\u62a5\u544a\u627e\u4e0d\u5230\u5bf9\u5e94\u7684\u8fc7\u6ee4\u5668\uff0c\u8fd9\u662f\u56e0\u4e3a\u547d\u540d\u5bfc\u81f4\u7684Spring Boot\u7ea6\u5b9a\u8fc7\u6ee4\u5668\u7684\u524d\u7f00\u4e3a\u914d\u7f6e\u7684name\uff0c\u800c\u540e\u9762\u6700\u597d\u7edf\u4e00\u90fd\u662f<code>GatewayFilterFactory<\/code><\/p>\n<pre><code class=\"language-yml\">spring:\n  application:\n    name: demo-gateway\n  cloud:\n    gateway:\n      routes:\n        - id: custom\n          uri: lb:\/\/demo-consumer\n          predicates:\n            - Path=\/account\/v1\/**\n          filters:\n            - name: Example\n              args:\n                op: root\n                type: he<\/code><\/pre>\n<p>\u5230\u8fd9\u91cc\uff0c\u8fc7\u6ee4\u5668\u7684\u7f16\u5199\u4e0e\u914d\u7f6e\u5c31\u5b8c\u6210\u4e86\uff0c\u7136\u540e\u542f\u52a8\u9879\u76ee\uff0c\u8bbf\u95ee<code>\/account\/v1\/test<\/code>\uff0c\u5c31\u4f1a\u5728\u65e5\u5fd7\u91cc\u770b\u5230\u5728\u8fc7\u6ee4\u5668\u4e2d\u6253\u5370\u7684\u4fe1\u606f<\/p>\n<p>\u6ce8\u610f\u6211\u4eec\u901a\u8fc7\u8fd9\u79cd\u5de5\u5382\u521b\u5efa\u51fa\u6765\u7684\u8fc7\u6ee4\u5668\u662f\u6ca1\u6709\u6307\u5b9a<code>order<\/code>\u7684\uff0c\u4f1a\u88ab\u9ed8\u8ba4\u8bbe\u7f6e\u4e3a\u662f<code>0<\/code>\uff0c\u914d\u7f6e\u5728yml\u6587\u4ef6\u4e2d\uff0c\u5219\u6309\u7167\u5176\u4e66\u5199\u7684\u987a\u5e8f\u6765\u6267\u884c\u3002\u5982\u679c\u60f3\u8981\u5728\u4ee3\u7801\u4e2d\u8bbe\u7f6e\u987a\u5e8f\uff0c\u5de5\u5382\u7684<code>apply<\/code>\u65b9\u6cd5\u9700\u8981\u505a\u4e00\u4e9b\u4fee\u6539<\/p>\n<pre><code class=\"language-java\">@Override\npublic GatewayFilter apply(Config config) {\n    return new InnerFilter(config);\n}\n\n\/**\n * \u521b\u5efa\u4e00\u4e2a\u5185\u90e8\u7c7b\uff0c\u6765\u5b9e\u73b02\u4e2a\u63a5\u53e3\uff0c\u6307\u5b9a\u987a\u5e8f\n *\/\nprivate class InnerFilter implements GatewayFilter, Ordered {\n\n    private Config config;\n\n    InnerFilter(Config config) {\n        this.config = config;\n    }\n\n    @Override\n    public Mono&lt;Void&gt; filter(ServerWebExchange exchange, GatewayFilterChain chain) {\n        System.out.println(&quot;  pre \u81ea\u5b9a\u4e49\u8fc7\u6ee4\u5668\u5de5\u5382 AAAA  &quot; + this.getClass().getSimpleName());\n        boolean root = &quot;root&quot;.equals(config.getOp());\n        if (root) {\n            System.out.println(&quot;  is root &quot;);\n        } else {\n            System.out.println(&quot;  is no root &quot;);\n        }\n        \/\/ \u5728then\u65b9\u6cd5\u91cc\u7684\uff0c\u76f8\u5f53\u4e8eaop\u4e2d\u7684\u540e\u7f6e\u901a\u77e5\n        return chain.filter(exchange).then(Mono.fromRunnable(() -&gt; {\n            System.out.println(&quot;  post \u81ea\u5b9a\u4e49\u8fc7\u6ee4\u5668\u5de5\u5382 AAAA &quot; + this.getClass().getSimpleName());\n        }));\n    }\n\n    @Override\n    public int getOrder() {\n        return -100;\n    }\n}\n\n\/\/ config\u7c7b\u7684\u4ee3\u7801\u540c\u4e0a\u9762\u4e00\u6837\uff0c\u4e0d\u518d\u5c55\u793a<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>GatewayFilterFactory Spring Cloud Gateway\u63d0\u4f9b\u4e86\u5f88\u591a\u5185\u7f6e\u7684\u8fc7\u6ee4\u5668\uff0c\u90a3\u4e48 [&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":[439],"class_list":["post-1747","post","type-post","status-publish","format-standard","hentry","category-spring-cloud","tag-spring-cloud-gateway"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1747","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=1747"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1747\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1747"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}