Solon Cloud Gateway 开发:Route 的过滤器与定制

RouteFilterFactory 是专为路由过滤拦截处理设计的接口。对应路由配置 filters

1、内置的路由过滤器

过滤器工厂 本置前缀 说明与示例
AddRequestHeaderFilterFactory AddRequestHeader= 添加请求头 (AddRequestHeader=Demo-Ver,1.0)
AddResponseHeaderFilterFactory AddResponseHeader= 添加响应头 (AddResponseHeader=Demo-Ver,1.0)
PrefixPathFilterFactory PrefixPath= 附加路径前缀 (PrefixPath=/app)
RedirectToFilterFactory RedirectTo= 跳转到 (RedirectTo=302,http://demo.org/a,true)
RemoveRequestHeaderFilterFactory RemoveRequestHeader= 移除请求头 (RemoveRequestHeader=Demo-Ver,1.0)
RemoveResponseHeaderFilterFactory RemoveResponseHeader= 移除响应头 (RemoveResponseHeader=Demo-Ver,1.0)
StripPrefixFilterFactory StripPrefix= 移除路径前缀段数 (StripPrefix=1)

2、定制示例

  • StripPrefix 过滤器定制示例(配置例:StripPrefix=1
java 复制代码
@Component
public class StripPrefixFilterFactory implements RouteFilterFactory {

    @Override
    public String prefix() {
        return "StripPrefix";
    }

    @Override
    public ExFilter create(String config) {
        return new StripPrefixFilter(config);
    }

    public static class StripPrefixFilter implements ExFilter {
        private int parts;

        public StripPrefixFilter(String config) {
            if (Utils.isBlank(config)) {
                throw new IllegalArgumentException("StripPrefixFilter config cannot be blank");
            }

            this.parts = Integer.parseInt(config);
        }

        @Override
        public Completable doFilter(ExContext ctx, ExFilterChain chain) {
            //目标路径重组
            List<String> pathFragments = Arrays.asList(ctx.newRequest().getPath().split("/", -1));
            String newPath = "/" + String.join("/", pathFragments.subList(parts + 1, pathFragments.size()));
            ctx.newRequest().path(newPath);

            return chain.doFilter(ctx);
        }
    }
}
相关推荐
任子菲阳23 分钟前
学Java第四十五天——斗地主小游戏创作
java·开发语言·windows
czhc114007566342 分钟前
Java1112 基类 c#vscode使用 程序结构
android·java·数据库
嫂子的姐夫42 分钟前
23-MD5+DES+Webpack:考试宝
java·爬虫·python·webpack·node.js·逆向
缪懿1 小时前
JavaEE:多线程基础,多线程的创建和用法
java·开发语言·学习·java-ee
Chan161 小时前
Java 集合面试核心:ArrayList/LinkedList 底层数据结构,HashMap扩容机制详解
java·数据结构·spring boot·面试·intellij-idea
Boop_wu1 小时前
[Java EE] 多线程 -- 初阶(2)
java·开发语言·jvm
q***98521 小时前
Spring Boot(快速上手)
java·spring boot·后端
IT_Beijing_BIT1 小时前
Rust入门
开发语言·后端·rust
凌凌01 小时前
macOS安装SDKMAN
java
百***92021 小时前
Spring Boot 多数据源解决方案:dynamic-datasource-spring-boot-starter 的奥秘(上)
java·spring boot·后端